This site requires JavaScript, please enable it in your browser!
Greenfoot back
Roshan123
Roshan123 wrote ...

2020/10/22

Collision

1
2
Roshan123 Roshan123

2020/10/23

#
danpost wrote...
Roshan123 wrote...
Finally made!!!!!! @Danpost please go through this Link and give some feedbacks Plz try this......
You are allowing multiple movements per act cycle. That is, if more than one movement key is pressed, the tank will move for each one. So, the rotation value may not correspond directly with the move and collision checking will not quite run correctly. This is causing the tank to glide backwards along a row of stones. Determine a single direction after checking ALL movement keys; then, if moving, set rotation and move one time only. Collision checking will then react properly will needing to move back off a stone.
Now its better I used else statements.....
danpost danpost

2020/10/23

#
Roshan123 wrote...
Now its better I used else statements.....
That would help; but has drawbacks. Some keys are now biased over others and that makes the "feel" on the keys awkward. That is why:
danpost wrote...
Determine a single direction after checking ALL movement keys;
Roshan123 Roshan123

2020/10/24

#
Plz help me to improve the algorithms for collision I m not able to improve the algorithms
danpost danpost

2020/10/24

#
Roshan123 wrote...
Plz help me to improve the algorithms for collision I m not able to improve the algorithms
To translate key presses to directions, we do along x-axis and along y-axis separately:
int dx = 0; // direction along x-axis
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
Note that dx will only be non-zero when ONLY one of the two keys are down. Next, we do the same for along y-axis with the other keys:
int dy = 0; // direction along y-axis
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
Now, the actor should only move when one, and ONLY one, of dx and dy is non-zero. So: we continue with:
if (dx*dy == 0 && dx+dy != 0)
{
    // next steps
}
The product is zero only if one of the two is zero. The sum is then not zero only if the other one is not zero. The next step is to translate dx and dy into a rotational value:
int rotation = 90*(dx == 0 ? 2-dy : 1-dx);
from which we can continue:
setRotation(rotation);
move(2);
if (isTouching(Actor.class)) move(-2);
Roshan123 Roshan123

2020/10/24

#
To translate key presses to directions, we do along x-axis and along y-axis separately: Note that dx will only be non-zero when ONLY one of the two keys are down. Next, we do the same for along y-axis with the other keys:
Thanks a lot for teaching me U wrote it in a very simple way Sometimes i need simultaneous reading for ur answer to understand it well but this time their is no need of reading it again and again.... ;)
Roshan123 Roshan123

2020/10/24

#
int rotation = 90*(dx == 0 ? 2-dy : 1-dx);
I noticed that u wrote 90* If i m not wrong then the algorithms are only for only 90° rotation and not for all degrees of rotation I gave u the link plz go through It
danpost danpost

2020/10/24

#
Roshan123 wrote...
I noticed that u wrote 90* If i m not wrong then the algorithms are only for only 90° rotation and not for all degrees of rotation
I guess you are looking for something more along these lines:
public void moveANDturn()
{
    int dr = 0;
    if (Greenfoot.isKeyDown("left")) dr--;
    if (Greenfoot.isKeyDown("right")) dr++;
    if (dr != 0)
    {
        turn(dr*2);
        if (isTouching(Actor.class)) turn(-dr*2);
    }
    int ds = 0;
    if (Greenfoot.isKeyDown("up")) ds++;
    if (Greenfoot.isKeyDown("down")) ds--;
    if (ds != 0)
    {
        move(ds*3);
        if (isTouching(Actor.class)) move(-ds*3);
    }
}
You need to login to post a reply.
1
2