2d ball bouncing

Blitz3D Forums/Blitz3D Beginners Area/2d ball bouncing

chwaga(Posted 2007) [#1]
How can i bounce balls in 2d, if someone could put this inside a function or something, and give me an explanation for how this works, I'd appreciate it. (been searching the forums, can't find anything that quite cuts it)


Rabies(Posted 2007) [#2]
Have you tried looking throught the sample games?
I'd try the code from "Kong" in Game Programming for teens.
I think this is a collision thing.
I'd be more detailed but I don't have time, sorry.


chwaga(Posted 2007) [#3]
err...what???


Terry B.(Posted 2007) [#4]
 
BallX=BallX+BallMoveX
If BallX > 100 Then BallMoveX=BallMoveX*-1



Off the top of my head, BallMoveX is how much the ball moves. Just opposite it when it hits a boundary.
Do the same thing for the Y vaules and you have a bounce effect.


chwaga(Posted 2007) [#5]
I've already done something like that, except that only works for 4-directional bouncing, it won't bounce of diagnal surfaces.


Pongo(Posted 2007) [#6]



Pongo(Posted 2007) [#7]
Oops,... just saw your last post,... didn't know you were looking for angles. I don't have time to make that work tonight. Edit,... did a bit more on it anyways

How are you drawing your angles? You will need to find the angle of the line, and how it intersects the velocity line of the ball. Then you change the velocity to the new value. Same concept as above, but instead of detecting the screen edge you are detecting the angled lines.

Edit: ok, I grabbed some code from the archives for the line collision. It is missing some of the collisions because the collision response is only changing the Y velocity. You will need to determine the proper angle to give the correct x and y velocities.





Stevie G(Posted 2007) [#8]
See my revised code near the bottom. This does correct reflection in 2d. You could adapt it to work with Pongo's code above.

http://www.blitzbasic.com/Community/posts.php?topic=65410#730520