# Sphere collision questions.

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

## Recommended Posts

Im try to implement a very simple sphere collision for particles i dont understand how to calculate the new velocity from 2 colliding objects. My maths is very limited so im having trouble understanding the very basics but i got to start some where. From nehe tutorials lesson 30 - explains how to get the new velocity off 2 colliding spheres
Quote:
 U1y and U2y are the projected vectors of the velocity vectors U1,U2 onto the axis which is perpendicular to the X_Axis. To find these vectors a few simple dot products are needed. M1, M2 is the mass of the two spheres respectively. V1,V2 are the new velocities after the impact, and V1x, V1y, V2x, V2y are the projections of the velocity vectors onto the X_Axis. In More Detail: a) Find X_Axis X_Axis = (center2 - center1); Unify X_Axis, X_Axis.unit(); b) Find Projections U1x= X_Axis * (X_Axis dot U1) U1y= U1 - U1x U2x =-X_Axis * (-X_Axis dot U2) U2y =U2 - U2x c) Find New Velocities (U1x * M1)+(U2x*M2)-(U1x-U2x)*M2 V1x= -------------------------------- M1+M2 (U1x * M1)+(U2x*M2)-(U2x-U1x)*M1 V2x= -------------------------------- M1+M2 In our application we set the M1=M2=1, so the equations get even simpler.
Dont think i follow what he means by the whole x axis thing why do i need to calculate that and wouldnt the axis change ? thx alot for any help, fishy

##### Share on other sites
I think in the example above it assumes the spheres are colliding head on
O---->           <------o     O---<-->--o     <----O o---->

##### Share on other sites

Quote:
 R is the new direction vectorI is the old direction vector before the collisionN is the Normal at the collision point The new vector R is calculated as follows: R= 2*(-I dot N)*N + I

So in theory this would just make me bounce off at a mirror angle ?

So i know at what point the sphere collides ok but how i do i calculate the normal at the collision point ?

##### Share on other sites
When it mentions N is it talking about the face normals or is this some sorta new normal that has to be calculated specifically for collision ?

Just with my particles they are all 2d billboards in 3d space.

##### Share on other sites
When the tutorial says x axis, it means the axis in the direction of the collision. For two spheres, the direction of the collision is the same as the vector from the center of one sphere to the center of the other. The code finds this direction and normalizes it to get what it is calling the x axis, which is really the collision axis. In a frictionless collision, the is the only direction in which the velocities are affected. The velocities perpendicular to this direction are unaffected.

##### Share on other sites
thx for that cleared it up slighty.

Ive implemented the way it says like so,

C = CenterPosB - CenterPosA;

VEC3 Axis;
Axis = Unit_Vec(C);

VEC3 U1, U2;
U1.x = Axis.x * (Axis.Dot_ProductVEC3(VelA));
U1.y = VelA->x - U1.x;
U2.x = -Axis.x * (-Axis.Dot_ProductVEC3(VelB));
U2.y = VelB->x - U2.x;

VelA->x = (U1.x + U2.x - (U1.x - U2.x));
VelA->y = U1.y;

The problem is that all my particles start off from a single spot so i figured they would all bounce all over the place but even with the emission rate as 2 every 10 seconds and with me only checking particle A against B as soon as they collide the velocity of A is reduced to almost zero.

Am i missing something ?

thx again.

Some of the syntax is really confusing i.e.

U1y=ArrayVel[BallColNr1]-U1x; this mean Vel.x - U1x or Vel.y - U1x or something else ><

##### Share on other sites
U1x, U1y, U2x, and U2y are all vectors. This seems to me to be a particularly confusing implementation of a collision response for two spheres...

##### Share on other sites
Sounds very simple aswell ><.

heres the link btw to that guide,
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=30

Is there a better explination of this any where ?

##### Share on other sites
Wooo i got it to work sorry my stupidity i was calculating the unit vector wrong ><. Was doing *= mag instead of /= im still not fully sure that its working though ill have to go through the code in more detail but for now it does seem to bounce away.

One thing that im still not quite sure on though is what if the collision is in the z axis ? That method seems to only update the x and the y how would you apply this to the z aswell.

##### Share on other sites
A simpler formulation that works in any dimension goes like this:

Let the centers of the spheres be C1 and C2, and let their respective velocities be V1 and V2 and their masses be M1 and M2. e is the coefficient of restitution (0 for an inelastic collision, 1 for an elastic collision, and in between for anything else).

N = C1-C2
V = V1-V2
Impulse = -(1+e)*V*N / N*N*(1/M1 + 1/M2)
V1 = V1 + Impulse/M1
V2 = V2 + -Impulse/M2

For more information on how this is derived, you can look at the third article in Chris Hecker's series of articles on rigid body dynamics, found at http://www.d6.com/users/checker/dynamics.htm

This formulation is more concise, involves fewer operations, and works in 2 or 3 dimensions. Note that all of the quantities involved, except mass and the coefficient of restitution, are vectors.

##### Share on other sites
Wow that gives some good results :D.

Just to clarify tho N and V vectors are unit vectors ?

Nice site aswell lots of good info.

I see this sub dividing time alot so that you get the perfect position when the two spheres collide do you have to do this im just thinking cpu wise with thousands of particles ?

Mmmm for some reason i keep getting -Indo for y
Sorry for my stupidity yeah simple check to make sure N isnt equal to zero for x y z solves that problem.

In that article the guy mentions that the N and V vectors dont have to be unit vectors but for me it will only work if they are ><..

If they arnt unit vectors they collide then seem to just stay still.

[Edited by - fishleg003 on February 28, 2006 5:56:50 PM]

##### Share on other sites
N is the collision 'normal' so it must be unit length but V must not.

+y
^
|
|
|
+-------->+x

or this way:

+-------->+x
|
|
|
|
+y

??

Not sure but this can be the problem: instead of moving up, it moves down :P...if this is your problem, just negate the y impulse.

##### Share on other sites
With the equations I posted above from Chris Hecker's article, N and V do not need to be unit vectors. V in particular is definitely not a unit vector, it is the velocity of the first sphere relative to the second. N is the direction of the collision, and using the equations given it does not need to be a unit vector. Actually I made a slight mistake in the equations I wrote earlier...the updates for the velocity should be

V1 = V1 + N * Impulse/M1
V2 = V2 - N * Impulse/M2

The way I had it before didn't make sense because it was adding a scalar to a vector...the way the equations work out, N is in the numerator twice and in the denominator twice, so its magnitude cancels out. As long as N isn't zero it is fine.

The spheres can be allowed to overlap before applying the collision impulses and it will still be roughly correct. Just check that the impulse is positive before applying it. If the impulse value as calculated above is negative, that means the spheres were already moving apart, and applying it would actually cause the spheres to stick together. Which in my opinion can also look pretty neat.

##### Share on other sites
Its working but one problem i have noticed because they all start off at the same spot say 20 at once the particles go absolutely balistic bouncing all over the place since they cant get away from each other.

Not only that the velocity seems to build up really quickly its hard to explain but say it checks collision all the time as much as it can when 2 spheres are inside each other the velocity is added on a good few times before the 2 spheres seperate is that correct ?

If velocity is a unit vector it seems to be more stable ill have another play with it later.

thx alot for the help.

##### Share on other sites
You're doing something wrong...the velocity should definitely NOT be a unit vector, it should be the difference between the velocities of the spheres. Starting every sphere at the same spot is a bad idea because if they are exactly in the same spot the collision routine won't work because the difference between their centers will be the zero vector. If the calculated impulse is negative, break out early and do not change the velocities. After a single collision, the colliding spheres should be separating from each other. If the spheres are separating, the impulse calculated will be negative, so the code should break out early and not change the velocities.

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628653
• Total Posts
2984054

• 10
• 9
• 9
• 10
• 21