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

2019/1/3

Bounce off a round player

1
2
3
4
Randomness Randomness

2019/1/3

#
Hey! :) I've a problem I'm thinking about for quite a while with no solution. This is my project: The ball is moving left to player (blue). I want that the ball bounces off the player but of course in the right angle (not just turn(180)).. I found no solution because you can turn and move the player so the rotation will change but it should still bounce off the same way althought the rotation changed. How can I manage this? Thanks :)
danpost danpost

2019/1/3

#
Randomness wrote...
<< Image Omitted >> The ball is moving left to player (blue). I want that the ball bounces off the player but of course in the right angle (not just turn(180)).. you can turn and move the player so the rotation will change but it should still bounce off the same way althought the rotation changed. How can I manage this?
The rotation will have nothing to do with the direction of the bounce -- only the angle between the two actors when the collision occurs is important.
Randomness Randomness

2019/1/3

#
alright thanks! but how does this work? I have absolutely no idea how to bounce off a round object
danpost danpost

2019/1/3

#
Randomness wrote...
alright thanks! but how does this work? I have absolutely no idea how to bounce off a round object
You get the differences between the two sets of location coordinates (dx, dy) and get the angle between the two actors using Math.atan2. Then take the moving direction of the actor (which could be the same as its rotation, by the way) and add 180 degrees and add or subtract twice the angle (in degrees) between the two. Whether you add or subtract depends on the order you used to acquire the angle between the two actorss.
Randomness Randomness

2019/1/3

#
I don't get how to get the angle using the difference of the two sets of location coordinates.. dx2-dx1, dy2-dy1 and how should I get an angle with that and Math.atan2? Sorry :(
danpost danpost

2019/1/3

#
Randomness wrote...
how to get the angle using the difference of the two sets of location coordinates.. dx2-dx1, dy2-dy1 and how should I get an angle with that and Math.atan2?
Actor player = getOneIntersectingObject(Player.class); // class name may need adjusted
int dx = player.getX()-getX();
int dy = player.getY()-getY();
double angle = Math.atan2(dy, dx)*180/Math.PI; // converted to degrees
setRotation((int)(2*angle-getRotation()+180); // assuming rotation is moving direction
If undesired results from this, try 'Math.atan2(-dy, -dx)'.
Randomness Randomness

2019/1/3

#
danpost wrote...
Randomness wrote...
how to get the angle using the difference of the two sets of location coordinates.. dx2-dx1, dy2-dy1 and how should I get an angle with that and Math.atan2?
Actor player = getOneIntersectingObject(Player.class); // class name may need adjusted
int dx = player.getX()-getX();
int dy = player.getY()-getY();
double angle = Math.atan2(dy, dx)*180/Math.PI; // converted to degrees
setRotation((int)(2*angle-getRotation()+180); // assuming rotation is moving direction
If undesired results from this, try 'Math.atan2(-dy, -dx)'.
this works! well.. somehow ;) when I move the player (or sometimes without moving) the ball gets stuck in the player and after 1-2 seconds it bounces . how can I fix this?
danpost danpost

2019/1/4

#
Randomness wrote...
this works! well.. somehow ;) when I move the player (or sometimes without moving) the ball gets stuck in the player and after 1-2 seconds it bounces . how can I fix this?
Probably need to move the ball off the player before bouncing. Also should probably use getObjectsInRange as well. Try this:
Actor player = getOneIntersectingObject(Player.class);
if (player != null)
{
    int dist = (player.getImage().getWidth()+getImage().getWidth())/2;
    if (getObjectsInRange(dist, Player.class).contains(player))
    {
        while (getObjectsInRange(dist, Player.class).contains(player)) move(-1);
        int dx = player.getX()-getX();
        int dy = player.getY()-getY();
        double angle = Math.atan2(dy, dx)*180/Math.PI;
        setRotation((int)(2*angle-getRotation()+180));
    }
}
Randomness Randomness

2019/1/4

#
well my code looks like this now but it's very buggy, definitly worse than before. ball is teleporting and the program also freezes (endless loop)
danpost danpost

2019/1/4

#
Randomness wrote...
well my code looks like this now << Image Omitted >> but it's very buggy, definitly worse than before. ball is teleporting and the program also freezes (endless loop)
Actually, I just tested it and it seems to be quite fair. No teleporting, or endless loop freezing. You should probably show the entire class code to give the bigger picture of things. Please copy/paste code text and use code tags so that the lines are numbered. The only issue I see is the crudeness of the bounce.. Using smooth moving and rotating would certainly help that.
Randomness Randomness

2019/1/4

#
I just tried it in a new project with two balls, one moving to the left to the right one but it happens the same.. I have the act method in ball2
public void act()
{
    move(-3);
    bounce();
}
public void bounce()
{
    Actor ball1 = getOneIntersectingObject(ball1.class);
    if (ball1 != null) {
        int dist = (ball1.getImage().getWidth() + getImage().getWidth()) / 2;
        if (getObjectsInRange(dist, ball1.class).contains(ball1)) {
            while (getObjectsInRange(dist, ball1.class).contains(ball1)) {
                move(-1);
            }
            int dx = ball1.getX() - getX();
            int dy = ball1.getY() - getY();
            double angle = Math.atan2(dy, dx) * 180 / Math.PI;
            setRotation((int)(2 * angle - getRotation() + 180));
        }
    }
}
that's the only code I have in the new project
danpost danpost

2019/1/4

#
One thing I noticed is that you have the ball2 object moving backward (opposite the angle of rotation). Try changing line 8 to:
move(1);
It would be better however, to set its rotation to 180 to begin with and then move(3) in the act.
Randomness Randomness

2019/1/4

#
well this works. What I noticed is that move(3) works like 90% of the time but when the player moves too fast (6) then it doesn't work and it seems like the ball bounces off randomely. Is there a way to fix this?
danpost danpost

2019/1/4

#
Randomness wrote...
well this works. What I noticed is that move(3) works like 90% of the time but when the player moves too fast (6) then it doesn't work and it seems like the ball bounces off randomely. Is there a way to fix this?
Maybe just keeping the player off the ball would be enough:
// in player1 class
move(speed);
while (isTouching(ball1.class)) move(-1);
With this, you may have to set an act order that the player acts before the ball.
Randomness Randomness

2019/1/4

#
this really doesn't solve the problem. it looks veeery buggy and doesn't work very often. Is there a better way to solve this so I can move the player as fast as I want and the ball will still bounce?
There are more replies on the next page.
1
2
3
4