# correct bouncing angle calculation in 2D

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

## Recommended Posts

Hi. this is my first thread here. Could someone describe, show an example or post a link how to write a collision detection and calculate the angle for bounce from a wall (or something else)? the speed of the moving object is grater than 1 pixel per frame. I wrote a collision detection that works (I think) but I think it is a quite complicated method and needs too much of calculations. And I am looking for an optimal algorythm. I have also tryed to write a code that calculates the bouncing angle but it works not correct to 100%. Sometimes my object moves throug walls. And something else: My solutions are based on an object that is a simple pixel. But it would be fine if I could move and let bounce grater abjects, too. If you want to see my code, I can try to post it here. Thanks.

##### Share on other sites
I you have horizontal and vertical walls collision is easy, just multiply x velocity by -1 if it is a horizontal wall or multiply the y velocity by -1 if it's a vertical wall.

##### Share on other sites
Yes, if I had only vertical and horizontal walls, it would be easy.

I checked out the posted link (why did this post disappeared?). This "tutorial" explains how to manage collision based on geometrical objects like circles or squares that are displayed in pixels on the screen (that is how I understand that).

But in my case all objects are these pixels. This means a circle is not really a circle but it is a group of pixels.

Think of the game "Worms" where the landscape is made of pixels and is randomly formed my explosions etc. If you throw a granade against the wall, it bounces in the correct angle from it. Olthough pixels always have vertical and horizontal "walls", if you throw a granade on a 45° wall with the granade angle of -45°, it bounces in an angle of -45°+180°.

There must be a way to analyze the amount and position of "wall"-pixels in front of the moving object to calculate the correct bouncing angle.

an example of a possible situation:

######..........
######..........
#####...........
#####...........
####............
####............
###........##...
###.......#..#..
##.........##...
##..............
#...............
#...............

a "circle" moves in the direction of the "wall" every # is a pixel.
paste it in a text editor where every character has the same length.

##### Share on other sites

Vr = Vi - 2(Vi.N)*N

Vi = direction vector of ball at impact
Vr = relection vector
N = normal of surface ball hit

##### Share on other sites
convert the wall to your 0 or 90 degree angle and then use the -1 multiplication. (get the angle between the wall vector and the velocity angle and then multiply velocity_y by -sin(theta) and velocity_x by -cos(theta)).

For collision detection use a parametric equation to describe your wall at points and parametic to describe the position of the ball and then use a formula to find the minimum distance between a line and a point.
http://astronomy.swin.edu.au/~pbourke/geometry/pointline/

##### Share on other sites
Someone else might be able to explain how to avoid moving through walls.

But let's say you solve that problem and are able to find collision points properly. Now you need to figure out the surface normal. And since you're working with pixels only, you'll need a way to approximate that based on the pixels around the impact pixel, not just the impact pixel itself.

I don't know if this would work, but here's an idea:

Given vectors "old", "new" (positions of the center of the moving object before the impact and during the impact, and "impact" (pixel position where the collision occurrs):

1. find an "oldimpact" vector: "oldimpact = impact - new + old;"
2. get the positions of all wall pixels in a small square grid centered on the impact point.
3. For each wall pixel in the grid, find the vector difference from that point to the "oldimpact" point. Add all of these vectors together, and then normalize it to yield the local surface normal.

You will have to experiment a lot with the size of that square grid to see what works best. You might also want to assign different weights to the vectors from pixels in the grid that are further from the impact point.

Alternatively, if you have enough free memory and aren't doing any deformable terrain, you can precompute the exact surface normal for every wall pixel.

##### Share on other sites
So your real problem is how to get a close enough value of the angle from a bunch of pixels. Here's a method i tried once in an otherwise really baaaad pinball game (but the angles were good :) ):

Once you know a collision is happening (using one pixel) calculate the angle from a box of pixels centered on that collision pixel. Set the box to an odd size (3,5,7). For every pixel add up the offset of the center to DX and DY values:

..........XXX..XXXXXXXXXX

The collision in the image happens at the center pixel. Now add up the DX and DY values for every set pixel in the collision mask.

Pixel at (-2,0) (two pixels left of the center): Add -2 to DX, 0 to DY
Pixel at (-1,0): Add -1 to DX, 0 to DY
Pixel at (0,0): Add 0 to DX, 0 to DY
Pixel at (-2,1): Add -2 to DX, 1 to DY

The end values for the sample above should come to DX = -3 and DY = 15.

From these two values you can calculate the angle (i'm not entirely sure how to do that, i think it was as simple as using atan, but with DX and DY switched:

atan( DX / DY )
or in C: atan2( DX, DY )

##### Share on other sites
Thanks. I will try it out.
I like the idea with adding the collision vectors.

##### Share on other sites
Quote:
 Original post by LensflareI checked out the posted link (why did this post disappeared?).

Ah sorry, I posted that link (Hecker collision response stuff) then realised it probably wasn't applicable for pixels (as you say). :)

You'll probably have to fabricate a normal angle by examining the surrounding pixels, as Endurion says.

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

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633311
• Total Posts
3011313
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!