Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Impulse based collision response (friction)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 ardmax1   Members   -  Reputation: 127

Like
0Likes
Like

Posted 20 March 2013 - 01:30 PM

I'm trying to implement impulse response with friction and I can't get it to work. I have compared other implementations that I found but I can't find any errors in my code. So what happens is as soon as object stops bouncing (bouncing works) it starts to slide and accelerate indefinitely. I'm testing this using sphere and plane.

 



e0->pos += e0->getVel() * info->getContactTime();
e1->pos += e1->getVel() * info->getContactTime();

vec3 n = info->getNormal();
vec3 r0 = info->getContactPoint() - e0->pos;
vec3 r1 = info->getContactPoint() - e1->pos;

vec3 v0 = e0->getVel() + cross( r0, e0->angVel );
vec3 v1 = e1->getVel() + cross( r1, e1->angVel );

vec3 dv = v1 - v0;

float e = 0.5;
float f = 0.1;
float num = (1+e) * dot(dv,n);
float denom = e0->massInv + e1->massInv + dot( e0->getWorldInertiaInv() * cross(cross(r0,n),r0) + e1->getWorldInertiaInv() * cross(cross(r1,n),r1) ,n );
float jr = num / denom;

e0->applyImpulse( r0, n * jr );
e1->applyImpulse( r1, -n * jr );

vec3 t = normalize( cross( n, cross( dv, n ) ) );

num = -dot(dv,t);
denom = e0->massInv + e1->massInv + dot( e0->getWorldInertiaInv() * cross(cross(r0,t),r0) + e1->getWorldInertiaInv() * cross(cross(r1,t),r1) ,t );
float jf = num / denom;
jf = clamp( jf, -jr * f, jr * f );

e0->applyImpulse( r0, jf * t );
e1->applyImpulse( r1, -jf * t );

 

 

 

Other implementations I've tried:

http://www.gamedev.net/topic/465248-calculating-impulse-due-to-rigid-body-collision-with-friction/

http://en.wikipedia.org/wiki/Collision_response#Impulse-Based_Reaction_Model

 

 

 



Sponsor:

#2 DT....   Members   -  Reputation: 487

Like
0Likes
Like

Posted 20 March 2013 - 04:16 PM

Would changing:

 

vec3 v0 = e0->getVel() + cross( r0, e0->angVel );
vec3 v1 = e1->getVel() + cross( r1, e1->angVel );

 

 

To:

 

vec3 v0 = e0->getVel() + cross( e0->angVel, r0 );

vec3 v1 = e1->getVel() + crosse1->angVel, r1 );

 
 
Fix it?


#3 ardmax1   Members   -  Reputation: 127

Like
0Likes
Like

Posted 20 March 2013 - 04:44 PM

It didn't change anything... There is wrong with calculating tangent because when the sphere doesn't have velocity, only angular velocity, tangent is nan and everything disappears.  



#4 Dirk Gregorius   Members   -  Reputation: 799

Like
0Likes
Like

Posted 20 March 2013 - 07:23 PM

You are not integrating the angular velocity. Also make sure you only integrate once per frame.

The tangent direction is v = vn + vt => vt = v - vn = v - ( v * n ) * n

Finally a sphere would indeed roll down the inclined plane forever. If the sphere is rolling the relative velocity at the contact point will be zero. You would need rolling friction to stop which is different. 



#5 ardmax1   Members   -  Reputation: 127

Like
0Likes
Like

Posted 21 March 2013 - 06:03 AM

Using your tangent the sphere stopped moving but kept on spinning. Actually I found that normalizing tangent caused a lot of problems and both methods seem to work the same.

 

 

You are not integrating the angular velocity. Also make sure you only integrate once per frame.

 

What do you mean I don't integrate angular velocity ? I do integrate once per frame with RK4.

 

vec3 RK4::linAcc( Entity* e ){
	return e->force * e->massInv;
}

vec3 RK4::angAcc( Entity* e ){
	return e->inertiaInv * e->torque;
}

Derivative RK4::eval(Entity* e, float dt ){
	return Derivative( e->vel, linAcc( e ), e->rot, angAcc( e ) );
}

Derivative RK4::eval(Entity* e, float dt, Derivative derivative ){
	vec3 v = e->vel + derivative.acc * dt;

	quat rot = e->rot + derivative.spin * dt;
	vec3 av = e->angVel + derivative.angAcc * dt;

	quat spin = 0.5f * quat(0,av) * rot;
	return Derivative( v, linAcc( e ), spin, angAcc( e ) );
}

void RK4::integrate( Entity* e, float dt ){
	Derivative a = eval(e, 0.0f);
	Derivative b = eval(e, dt*0.5f, a);
	Derivative c = eval(e, dt*0.5f, b);
	Derivative d = eval(e, dt, c);

	vec3 vel = 1/6.f * ( a.vel + 2.0f * (b.vel + c.vel ) + d.vel );
	vec3 acc = 1/6.f * ( a.acc + 2.0f  * (b.acc + c.acc ) + d.acc );
	quat spin = 1/6.f * ( a.spin + 2.0f  * (b.spin + c.spin ) + d.spin );
	vec3 angAcc = 1/6.f * ( a.angAcc + 2.0f  * (b.angAcc + c.angAcc ) + d.angAcc );

	e->pos = e->pos + vel * dt;
	e->vel = e->vel + acc * dt;
	e->angVel = e->angVel + angAcc * dt;
	e->rot = normalize( e->rot + spin * dt );
}

 

Finally a sphere would indeed roll down the inclined plane forever. If the sphere is rolling the relative velocity at the contact point will be zero. You would need rolling friction to stop which is different. 

 

I thought reducing tangent velocity would slow angular velocity too. So i would need to simply apply another impulse with direction of collision normal ?



#6 Dirk Gregorius   Members   -  Reputation: 799

Like
0Likes
Like

Posted 21 March 2013 - 09:50 AM

First two lines in your original post:

e0->pos += e0->getVel() * info->getContactTime();
e1->pos += e1->getVel() * info->getContactTime();

 

You need two tangent directions in the normal plane. If the sphere is rolling there is no relative velocity at the contact point and then you normalize the zero vector. In the worst case you get arbitrary directions. Look at b2PlaneSpace in Bullet or dPlaneSpace in the ODE for an example how to build proper tangent planes



#7 Dirk Gregorius   Members   -  Reputation: 799

Like
0Likes
Like

Posted 21 March 2013 - 09:52 AM

There is really no sense in using RK4. Use symplectic Euler. You are not considering the constraint (contact) forces when you are evaluating your entities anyway.



#8 ardmax1   Members   -  Reputation: 127

Like
0Likes
Like

Posted 21 March 2013 - 05:21 PM

I looked through Bullet's code and came up with this:



vec3 n = info->getNormal();
vec3 r0 = info->getContactPoint() - e0->pos;
vec3 r1 = info->getContactPoint() - e1->pos;

vec3 a0 = cross( e0->angVel, r0 );
vec3 a1 = cross( e1->angVel, r1 );
vec3 v0 = e0->getVel() + a0;
vec3 v1 = e1->getVel() + a1;

vec3 da = a1 - a0;
vec3 dv = v1 - v0;

float e = 0.5;
float df = 0.01;
float sf = 0.05;
float rf = 0.1;

applyContactImpulse( e0, e1, r0, r1, dv, n, e );

if( length( da ) > 1e30f ){
	da = normalize( da );
	if ( length(da) > 0.001 ) applyRollingFrictionImpulse( e0, e1, r0, r1, dv, da, rf );
}else{
	applyRollingFrictionImpulse( e0, e1, r0, r1, dv, -n, rf );
	vec3 t0,t1;
	planeSpace( n, t0, t1 );
	if ( length( t0 ) > 0.001 ) applyRollingFrictionImpulse( e0, e1, r0, r1, dv, t0, rf );
	if ( length( t1 ) > 0.001 ) applyRollingFrictionImpulse( e0, e1, r0, r1, dv, t1, rf );
}


vec3 t = dv - dot( dv, n ) * n;
if( dot( t,t ) > epsilon<float>() ){
	t = normalize( t );
	applyFrictionImpulse( e0, e1, r0, r1, dv, t, df );
} else {
	vec3 t0,t1;
	planeSpace( n, t0, t1 );

	applyFrictionImpulse( e0, e1, r0, r1, dv, t0, sf );
	applyFrictionImpulse( e0, e1, r0, r1, dv, t1, sf );
}

 



void applyContactImpulse( Entity* e0, Entity* e1, vec3 r0, vec3 r1, vec3 dv, vec3 n, float e ){
	float num = (1+e) * dot(dv,n);
	float denom = e0->massInv + e1->massInv + dot( e0->getWorldInertiaInv() * cross(cross(r0,n),r0) + e1->getWorldInertiaInv() * cross(cross(r1,n),r1) ,n );
	float jr = num / denom;

	e0->applyImpulse( r0, n * jr );
	e1->applyImpulse( r1, -n * jr );
}

void applyFrictionImpulse( Entity* e0, Entity* e1, vec3 r0, vec3 r1, vec3 dv, vec3 n, float e ){
	float num = e * dot(dv,n);
	float denom = e0->massInv + e1->massInv + dot( e0->getWorldInertiaInv() * cross(cross(r0,n),r0) + e1->getWorldInertiaInv() * cross(cross(r1,n),r1) ,n );
	float jr = num / denom;

	e0->applyImpulse( r0, n * jr );
	e1->applyImpulse( r1, -n * jr );
}

void applyRollingFrictionImpulse( Entity* e0, Entity* e1, vec3 r0, vec3 r1, vec3 dv, vec3 n, float e ){
	float num = e * dot(dv,n);
	float denom = e0->massInv + e1->massInv + dot( e0->getWorldInertiaInv() * n + e1->getWorldInertiaInv() * n,n );
	float jr = num / denom;

	e0->applyImpulse( r0, n * jr );
	e1->applyImpulse( r1, -n * jr );
}

 

but I can't figure out how to do rolling friction. It looks more stable now, but it just keeps on spinning.



#9 Dirk Gregorius   Members   -  Reputation: 799

Like
0Likes
Like

Posted 21 March 2013 - 10:12 PM

Your effective mass in applyContactImpulse() and applyFrictionImpulse look wrong. You should please verify it is equivalent to:

denom = InvM1 + InvM2 + ( r1 x n )^T * InvI1 * ( r1 x n ) + ( r2 x n )^T * InvI2 * ( r2 x n )

 

You also need to recompute the relative velocity after you apply an impulse. You have essentially a Jacobi solver.



#10 ardmax1   Members   -  Reputation: 127

Like
0Likes
Like

Posted 22 March 2013 - 07:53 AM

Like this ?

denom = e0->massInv + e1->massInv + dot( cross(r0,n), e0->getWorldInertiaInv() * cross(r0,n) ) + dot( cross(r1,n), e1->getWorldInertiaInv() * cross(r1,n) );

It looks like it didn't change anything.

 

Does applyRollingFrictionImpulse look good at all ? Also I should use applyImpulse or applyTorque in rolling friction ?



#11 Dirk Gregorius   Members   -  Reputation: 799

Like
0Likes
Like

Posted 22 March 2013 - 12:07 PM

I am not sure, but I would guess that rolling friction is essentially an angular motor which drives the angular velocity to zero. So I would use applyTorque(). Not that he names are misleading. It should be applyAngularImpulse.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS