# How to make a ball bounce at the proper angle

This topic is 3563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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.

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009398
• ### Who's Online (See full list)

There are no registered users currently online

×