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

2014/11/9

Bouncing off of walls?

JDSmooth JDSmooth

2014/11/9

#
So I have this simple game where you control a red ball with the mouse, and blue balls will spawn, move and bounce off the walls. You just have to not get hit by the blue balls. I have it set now so that when the blue ball actor gets close to the edge of the world it picks a random number and turns in that direction. I think it would be a lot better, though, to have it actually bounce off the wall with an angle like a normal ball would. Does anyone have any suggestions on how I would go about this?
danpost danpost

2014/11/9

#
What code do you currently have for the class of the balls (the ones you want to have bounce off the edges of the world)?
JDSmooth JDSmooth

2014/11/9

#
public class blueBall extends Ball { public void act() { move(); turn(); } public void turn() { if (isAtEdge()) { turn(Greenfoot.getRandomNumber(90)); } } }
danpost danpost

2014/11/9

#
The only thing that determines which direction the ball is moving is its rotation. You will need to give it a random rotation when created and then adjust it in your 'turn' method depending on which edge the ball is at. Because you need to know which edge is touched, using the 'isAtEdge' method seems inappropriate as you would have to duplicate the checks it performs because it does not return any value to inform the calling method which edge, if any, is touched (it only tells you whether one is touched or not by returning a boolean, or true/false value). The documentation of the 'isAtEdge' method tells you what kind of checks it performs.
JDSmooth JDSmooth

2014/11/9

#
Oh okay that makes sense. Is there another method already in the documentation that would be better or will I have to create one to find which specific edge it is?
danpost danpost

2014/11/9

#
JDSmooth wrote...
Oh okay that makes sense. Is there another method already in the documentation that would be better or will I have to create one to find which specific edge it is?
Just use the 'getX' and 'getY' methods and compare the returned values to that of the edges of the world: left edge: getX() == 0 right edge: getX() == getWorld().getWidth()-1 top edge: getY() == 0 bottom edge: getY() == getWorld().getHeight()-1 The change in rotation of the ball will be different for each edge, so code each bounce individually (use a separate 'if' block for each edge).
JDSmooth JDSmooth

2014/11/9

#
Ohhh okay yeah I see how I can do this now. Thank you so much for the help!
danpost danpost

2014/11/9

#
On further consideration, the change in rotation for opposite edges would be similar and, therefore, you can reduce the four 'if' block' to two of them ORing (using ' || ' ) the checks for opposite edges. For example, for the top and bottom edges (when getY() is in question), you can use:
1
2
3
4
if (getY() == 0 || getY() == getWorld().getHeight()-1)
{
    setRotation(360-getRotation());
}
The change for the other two sides is a bit different. See if you can figure it out.
JDSmooth JDSmooth

2014/11/9

#
1
2
3
4
if (getX()==0 || getX() == getWorld().getWidth()-1)
       {
           setRotation(180-getRotation());
       }
That seems to be working, it was really simple but took some time.
jimboweb jimboweb

2014/11/10

#
You might want to use the DriftMove class which I have created. It allows you to make an object drift on the screen and features a BounceAtEdge method that reflects at the angle that it hits the edge, just like a regular ball. It also allows you to set a random speed on spawning, and a number of other methods that might help. You can find it here: http://mrstewartslessons.com/useful_class_driftmove.html Select-click on the 'Download Class' and save it in your project folder. Close your project and open it again, and the class will be in there. Double-click on the class and change 'Source Code' to 'Documentation' to see very clear explanations of how to use the class. Hope it helps!
You need to login to post a reply.