• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ardmax1

Impulse based collision response (friction)

10 posts in this topic

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

 

 

 

0

Share this post


Link to post
Share on other sites

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?
0

Share this post


Link to post
Share on other sites

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.  

0

Share this post


Link to post
Share on other sites

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. 

0

Share this post


Link to post
Share on other sites

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 ?

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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 ?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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  
Followers 0