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

2019/11/14

How to make a ball bounce back from a object?

ouyoood ouyoood

2019/11/14

#
Hi,I encountered a few problems with my homework recently. I simulate two movable players playing a ball and tried to make the ball bounce back after hitting a player. I used turn (Greenfoot.getRandomNumber(90)-45) to allow it rebouncing back at a random angle. But the result does not seem like what I expected, the ball didn't always bounce back, it sometimes will go up, down or keep forward. Another problem is that when it hits the player, it will rotate in that place for less than a second. What should I do to make it more logical?
Actor Player_A = getOneIntersectingObject(PlayerA.class);

if (Player_A!=null)
      {
          move(10);
          turn(Greenfoot.getRandomNumber(90)-45);
       }
Thanks!
danpost danpost

2019/11/15

#
Try this:
move(10);
int dir = 1-2*((getRotation()+90)/180);
int region = getX()/(getWorld().getWidth/3)-1;
if (isTouching(Player.class) && dir == region) setRotation(135+90*(1-dir)+Greenfoot.getRandomNumber(91));
You need to login to post a reply.