# Elastic Collision Reponse in a Game

#### black_em2

I am trying to program the elastic collision response between a ball and an immovable obstacle (such as a rock with infinite mass). However I can't seem to figure out the angle at which the ball would bounce off of the object. Both objects are to be considered as circles, with their positions given as their centers, and their acceleration, velocity, and position are stored in X and Y components.

Based on this image, the angle that the ball would reflect would be equal to the orthogonal of the line between their centers and parallel to the normal surface between them. Is this true?

I am having a very hard time trying to determine the angle after the collision. Is there a way to do this using only vectors and no trig? My group members think so, but I don't know how you wouldn't use trig for this. Right now I am using C++ but will later be incorporated in objective-C. Please help!
Thank You

Last edited by a moderator:
Related Programming and Computer Science News on Phys.org

#### AlephZero

Homework Helper
Your question says one disk is fixed, but your graphics show both disks moving.

If both disks can move, the easiest way to do this is to rearrange the equations in Newton's laws of motion, which gives the general result that

For an elastic collision with no friction between the objects:

(1) The velocity vector of the center of mass of the two disks is unchanged by the collision. (Same speed and same direction before and after the collision).
(2) Relative to the center of mass, the velocity vector of each disk is reversed by the collision.

Note, you can still have a perfectly elastic collision even if the friction is non-zero (if you assume the friction is big enough so there is no slipping at the contact point), but you then have to take account of the rotational speed of the disks before and after the collision as well. Think about playing snooker or golf, and putting spin on the ball. That is significantly more complcated.

However the above method does NOT work if one object is rigidly fixed. In that case

(1) resolve the velocity before the collision into the normal and tangential components
(2) the tangential component is unchanged
(3) the normal component has the same magnitude but the direction is reversed.

You probably don't need to explicitly use trig functions to do this, because you know the vectors representing the directions. The normal direction is the vector between the centers of the two objects. The tangential direction is the normal direction rotated through a right angle, i.e. direction [a, b] goes to [b, -a]. You can then find the magnitude of the vectors by equating the x and y components and solving a 2x2 system of equations, instead of using trig.

#### black_em2

Yes one obstacle will always be static. The image (from wikipedia) was something I was using to ask if I could assume that the new velocity vector is orthogonal to the vector between the centers of the objects, which I think you are agreeing to in your post.

I'm sorry but I'm just not understanding how I could implement the rotation of the normal vector and give it the magnitude of the velocity vector. Wouldn't the case where the ball's velocity vector lined up with the center of the rock (like a head on collision), cause the normal/velocity to be reversed, 180 degrees instead of 90 degrees?

But from what you are saying I think I can use trig & just put up with any performance issues with the calculations.

[PLAIN]http://img337.imageshack.us/img337/6627/collssion.png [Broken]
(not to scale)

So
V_mag = magnitude of the velocity
distance = distance between the centers of the two objects (the normal vector which should always be constant due to the size of the images used)
theta = 90 - (the angle between the normal and the velocity vectors)
Velocity_X = V_mag * cos(theta)
Velocity_Y = V_mag * sin(theta)

But wait, how can I find the angle between the normal & velocity vectors if I don't know which vector will be the hypotenuse?? Or am I on the wrong track. Dang now I'm confused again

Last edited by a moderator:

#### Edgardo

The picture you have drawn applies only to two moving objects. The situation looks different for one object moving and the other being static. For example, if you have a ball bouncing off from a spherical rock then this is similar to a ball hitting a curved wall. The original velocity vector is then just reflected (as opposed to the picture you have drawn).

Hence, I suggest first tackling another problem, namely a ball hitting a wall. For this, have a look at http://www.dpfiles.com/dpfileswiki/index.php?title=Tricks_of_the_Windows_Game_Programming_Gurus%2C_Chapter_13:_Playing_God:_Basic_Physics_Modeling#Basic_Ad_Hoc_Collision_Response" [Broken].

Have a look at Figure 13.24. The equation is F = 2*N+I, where
F: the new velocity vector,
I: the original velocity vector,
N: the (negative) projection of F to the normal vector of the wall.

Try to understand the picture and you will get a good intuition.

To understand Lamothe's explanation you will need some knowledge about vector addition, vector normalization, dot product and projection of one vector to another.

Last edited by a moderator:

#### AlephZero

Homework Helper
When the impact occurs, suppose the center of the ball is at (xb, yb), the center of the stationary circular rock is at (xr,yr), and the incoming velocity of the ball is the vector (xv, yv).

The direction normal to the impact is the vector (xn, yn) joining the centers of the two objects, so

(xn, yn) = (xr-xb, yr-yb).

The direction tangential to the impact is (xt, yt) is a vector at 90 degrees to that, so it is

(xt, yt) = (yn, -xn).

(Note, these normal and tangential vectors do not have unit length, but that fact doesn't matter.)

Now, you want to resolve the incoming velocity into the normal and tangential parts. In other words you want to find A and B so that

(xv, yv) = A(xn, yn) + B(xt, yt)

Equating the x and y components of the vectors, you have two equations

xn A + xt B = xv
yn A + yt B = yv

which you can solve for A and B, because all the other quantities are known.

For the outgoing velocity vector, the normal component of the velocity is reversed and the tangential component is unchanged, so the outgoing velocity is

-A(xn, yn) + B(xt, yt)

No trig required

Sorry if the earlier post was not clear.

#### black_em2

The picture you have drawn applies only to two moving objects. The situation looks different for one object moving and the other being static. For example, if you have a ball bouncing off from a spherical rock then this is similar to a ball hitting a curved wall. The original velocity vector is then just reflected (as opposed to the picture you have drawn).

Hence, I suggest first tackling another problem, namely a ball hitting a wall. For this, have a look at http://www.dpfiles.com/dpfileswiki/index.php?title=Tricks_of_the_Windows_Game_Programming_Gurus%2C_Chapter_13:_Playing_God:_Basic_Physics_Modeling#Basic_Ad_Hoc_Collision_Response" [Broken].

Have a look at Figure 13.24. The equation is F = 2*N+I, where
F: the new velocity vector,
I: the original velocity vector,
N: the (negative) projection of F to the normal vector of the wall.

Try to understand the picture and you will get a good intuition.

To understand Lamothe's explanation you will need some knowledge about vector addition, vector normalization, dot product and projection of one vector to another.
Thanks for the link, ill have to give it a good look through

When the impact occurs, suppose the center of the ball is at (xb, yb), the center of the stationary circular rock is at (xr,yr), and the incoming velocity of the ball is the vector (xv, yv).

The direction normal to the impact is the vector (xn, yn) joining the centers of the two objects, so

(xn, yn) = (xr-xb, yr-yb).

The direction tangential to the impact is (xt, yt) is a vector at 90 degrees to that, so it is

(xt, yt) = (yn, -xn).

(Note, these normal and tangential vectors do not have unit length, but that fact doesn't matter.)

Now, you want to resolve the incoming velocity into the normal and tangential parts. In other words you want to find A and B so that

(xv, yv) = A(xn, yn) + B(xt, yt)

Equating the x and y components of the vectors, you have two equations

xn A + xt B = xv
yn A + yt B = yv

which you can solve for A and B, because all the other quantities are known.

For the outgoing velocity vector, the normal component of the velocity is reversed and the tangential component is unchanged, so the outgoing velocity is

-A(xn, yn) + B(xt, yt)

No trig required

Sorry if the earlier post was not clear.
ok so then I just have to take the magnitude to find the corresponding velocity components
X_v = sqrt((-A*xn)^2 + (B*xt)^2)
Y_v = sqrt((-A*yn)^2 + (B*xt)^2)

Also to solve the system of equations in a program, it seems easiest to use kramers rule. However how can I solve it if I end up with a determinant of 0? can I just multiply xn & yt by 1.1? The game is just a small project for school so it does not have to be 100% accurate, but I would like to keep it as realistic as possible. Thanks again!

Last edited by a moderator:

#### AlephZero

Homework Helper
ok so then I just have to take the magnitude to find the corresponding velocity components
X_v = sqrt((-A*xn)^2 + (B*xt)^2)
Y_v = sqrt((-A*yn)^2 + (B*xt)^2)
No, it is just
X_v = -A xn + B xt
Y_v = -A yn + B yt

Also to solve the system of equations in a program, it seems easiest to use kramers rule. However how can I solve it if I end up with a determinant of 0?
Sure, Cramer's rule is as good as anything else for 2 equations in 2 variables.

You can only get a determinant of 0 if you have xb = xr and yb = yr, and that is impossible unless your objects have zero size (or something else is wrong with your program).

If you do have an object of zero size, then the velocity vector is just reversed by the collision, so

X_v = -xv
Y_v = -yv

#### black_em2

Thanks so much for all the help. Everything seems to be working great.

When I looked over the values for the determinant again, it does seem pretty impossible that the determinant would result in 0. But I would rather include a check just in case something else glitched & doesn't cause the program to crash.

#### black_em2

here this code I ended up using thanks to alephzero

Code:
if([tile isRock])
{
c = magnitude(d);                        \\d is a CGPoint of distance (x & y components) between two objects
if(c <= 0.5) // ball collides
{

float x_tan, y_tan, det, A, B;

x_tan = d.y;                          \\Tangent Vector, normal vector rotated 90 degrees
y_tan = -d.x;

det = (d.x * y_tan - x_tan * d.y);        \\Solve determinant

If (det == 0) then
{
velocity.x *= -1;
velocity.y *= -1;

}
else
{

A = (velocity.x * y_tan - velocity.y * x_tan)/ det;    	\\Solve for A magnitude
B = (d.x * velocity.y - d.y * velocity.x)/ det;    	\\Solve for B magnitude

velocity.x = -A * d.x + B * x_tan;       		\\Solve new velocity components
velocity.y = -A * d.y + B * y_tan;
}
}
}

#### davee123

Based on this image, the angle that the ball would reflect would be equal to the orthogonal of the line between their centers and parallel to the normal surface between them. Is this true?
Ok, is this really true? I tried slogging through the posts, but it didn't seem to be discussing the angle of deflection, which I just can't wrap my brain around. Why would it be *orthongonal*? Doing what I would ASSUME would be correct, I get the results of the attached image... Am I way off base?

For reference's sake, I'm saying that the magenta line is the resultant vector of the initially moving ball, and the blue vector represents the resultant vector of the initially static ball.[/edit]

DaveE

#### Attachments

• 3.2 KB Views: 247

#### Edgardo

The website http://www.real-world-physics-problems.com/index.html" [Broken].

Last edited by a moderator:

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving