Sign in to follow this  

3D Collision response with rotation C++

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys. I'm currently making a simple simulation involving collisions between some objects and boundaries. I've siffed through the stuff online, chris hecker's site etc and have tried piece it together for my app but I can't get it right! I'm using the impulse equation to get the response to collisions, works fine before adding the rotational stuffs. I've stripped it right down at the moment to the simplest scenario - a ball dropping onto a flat plane with no initial horizontal linear Velocity and a bit of rotation about the x-axis. My angular velocity is represented as a vector, were the magnitude is the angle to rotate by and the unitVector is the axis to rotate about. For this specific scenario I've noticed a few things with the equation. Here's the eqn from Chris Hecker's piece and when stripped down for sphere/immovable plane impulse I'm also using - Velocity after = Velocity before + J/mass - angularVel = angularVel + (J x R)/I (were J = j*collisionNormal and x is a crossProduct) Heres my understanding of a few things: - My sphere is uniform thus rotation always about an axis through its centre. - Rather than mult by the Inertia tensor's inverse I can just divide by the Inertia (2/5 mr^2) because of the uniformed sphere The crossProducts on the denominator of the equation are confusing me, in the case of sphere vs plane the vector from the sphere centre to collision point will be in the negative direction to the normal, thus the crossProduct is 0 making that part of the eqn dissapear. I assume this will also be the case of sphere vs sphere? Also because of this in - angularVel = angularVel + (J x R)/I - J x R will be 0, so there will be no change to the angular velocity from the collision? Here's the code, sorry if its a tad hard to read. The result is the ball just bounces around as if it weren't rotating
spherePlaneResponse(JPhysicsObject *sphere, JPhysicsObject *plane)
{
  const float e = 0.75f;  // coefficient of restitution
  float I = 0.4f*sphere->getMass()*sphere->getRadius()*sphere->getRadius(); // Inertia of sphere
  JPVector3 v, n, R, w, collisionPoint;
  
  n = *plane->getPlaneNormal();               // collision normal
  collisionPoint = *sphere->getPosition() - n*sphere->getRadius();
  R = collisionPoint - *sphere->getPosition();// sphere centre to collision point vector

  // Linear velocity at collision point, also the relative velocity as plane.velocity = 0
  v = *sphere->getLinearVelocity() + crossProduct(&R, sphere->getAngularVelocity());
  
  // impulse equation
  // [((R x n)/I)xR].n
  JPVector3 rXn = crossProduct(&R, &n)/I;
  rXn = crossProduct(&rXn, &R);
  float  rXnDn = dotProduct(&rXn, &n);

  float numerator = -(e+1.0f)*dotProduct(&v, &n);      // Numerator     -(e+1.0f) * v.n
  float denominator = (1.0f/sphere->getMass()) + rXnDn;// Denominator 1/m + [((R x n)/I)xR].n    

  float j = numerator/denominator;  
  JPVector3 J = n*j;

  v = *sphere->getLinearVelocity() + J/sphere->getMass(); // v = v + J/m
  w = *sphere->getAngularVelocity() + crossProduct(&J, &R)/I; // w = w + (JxR)/I
 
  sphere->setLinearVelocity(&v);
  sphere->setAngularVelocity(&w);




cheers, J [Edited by - SantosaSabrosa on February 28, 2010 10:44:02 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by SantosaSabrosa
The crossProducts on the denominator of the equation are confusing me, in the case of sphere vs plane the vector from the sphere centre to collision point will be in the negative direction to the normal, thus the crossProduct is 0 making that part of the eqn dissapear. I assume this will also be the case of sphere vs sphere?

Also because of this in - angularVel = angularVel + (J x R)/I - J x R will be 0, so there will be no change to the angular velocity from the collision?



yup. If you want to incur rotations on the sphere when you have a collision, no matter where you impact on the sphere, you will need friction.

Here is what I have for reference.

It's a box (so you will have angular change of momentum after collision), there is no friction as yet, and it's against a static plane (the impulse equation is simplified, since the plane is static).

Share this post


Link to post
Share on other sites
Hey thanks alot ;) Thats a good refference, I'll be sure to check out especially when I go tackling the cubes.

So now I've gathered that the balls won't rotate without friction (kind of obvious when you think about it don't know how I missed it!)

I would have assumed, given an initial angular velocity this would however effect the linear Velocity after the impact, but I'm guessing with frictionless contact it also won't effect it still. However;
To add this in I have changed this line

v = *sphere->getLinearVelocity() + J/sphere->getMass(); // v = v + J/m

changed to

v = v + J/sphere->getMass(); // v = v + J/m






were the v beforehand is the velocity at the point of collision rather then the centre. It sort of gives the desired effect (if dropped straight down with some initial spin the spin will change the direction is bounces up) but I don't thik its mathematically correct doing this! exe can be seen here - cube is work in progress :p Motion isn't sorted out when it eventually rests on the plane.

*edit*
well I found few places stating that the new velocities would be given by
v = v + [jn + (uj)t]/m
w = w + {r x [jn + (uj)t]}/I

were t is the perpendicular to n, given by t = [(n x relativeVelocity) x n]
u is coefficient of friction.

Havn't added this yet so I'll see the results soon.

J

[Edited by - SantosaSabrosa on March 2, 2010 10:40:05 AM]

Share this post


Link to post
Share on other sites
if you haven't done the rotational part already, the main problem is converting angular velocity in to a change in orientation, or basically in integrating for orientations. I use a quaternion-to-matrix conversion, but there are other methods.

Share this post


Link to post
Share on other sites
I do have the rotation in place to edit the orientation.
But I'm beginning to think there was no problem in the first place.

The problem was the motion of the spheres were not being altered by the addition to the rotational dynamics in the impulse equation. As there is absolutely no friction..i guess there isn't meant to be any! I did however think the linear resultant velocity would be different but the more I think about it I don't think it will either. So a ball given an initial spin in a competely frictionless environment will bounce around as if it weren't spinning at all?

I guess the problem is my spheres in a fricitonless environment can't be used to simulate rotational motion :p

Share this post


Link to post
Share on other sites
no friction means no change in angular momentum. The spin will remain constant (numerical drift not taken into account).

It's easier to visualise by spinning a ball on super slick ice, as it bounces around, the ball will keep on spinning the same way regardless of collisions.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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