Sign in to follow this  
yaazz

How to make a ball bounce at the proper angle

Recommended Posts

Hi there, I am trying to make a simple game where a ball can bounce through a maze and into a goal section. When I hit a wall, what I would like to happen is that the ball bounces off with an angle equal to the angle of incidence. In theory i know to do this. We figure out whether we are colliding vertically or horizontally, then Make X a negative if we have a vertical collision or Y negative if we have a horizontal collision. How do I determine what kind of collision I have? The blocks I collide with are just simple blocks set up in a system of tiles. They do not rotate, and they do not move. Because of this, I currently use a simple bounding box system for collision. I check to see if one box overlaps the other, and if so we have a collision. To move the ball, I use the formula double tempX=X+(speed)*cos((PI/180)*(angle)); double tempY=Y+(speed)*sin((PI/180)*(angle)); Where X & Y are the initial coordinates, Speed is the length of the movement vector, and angle is the direction of the movement vector.

Share this post


Link to post
Share on other sites
Hi Yaazz,

If you already know for sure that there will only be horizontal and vertical walls in your game, it should de sufficient to deal with ball-to-wall collisions the way you do.

Could you simply add a direction variable to your wall, set to either horizontal or vertical and then find the correct formula with an if - then statement?

If you want a more interesting and physically correct behaviour, you might want to add real impulse-based movement. It isn't as hard as you may think, and I wouldn't mind helping along with a few links and code snippets.

Cheers,
Mike

Share this post


Link to post
Share on other sites
you will have a much easier time using vectors. here is an example of the 'reflect velocity' method.

as for determining which side two box hits, here's another discussion on the topic.

when testing two blocks, you find out the mtd (minimum translation vector), thatn you can use to give you the direction of the collision.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this