• Create Account

# ardmax1

Member Since 14 Sep 2010
Offline Last Active May 16 2013 11:12 AM

### In Topic: Impulse based collision response (friction)

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 ?

### In Topic: Impulse based collision response (friction)

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.

### In Topic: Impulse based collision response (friction)

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 ?

### In Topic: Impulse based collision response (friction)

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.

### In Topic: Space partitioning for flocking

06 July 2012 - 11:30 AM

First try with grid i got 4 times more (1k to 4k) birds without fps drop, but I'm sure it can be faster. Any idea how to optimize it?
```Grid::Grid( Flock* flock, float cellsize ) {
min = flock->min - flock->max / 2;
max = flock->max * 1.5f;

this->cellsize = cellsize;
ccx = (int)((max.x - min.x) / cellsize) + 1;
ccy = (int)((max.y - min.y) / cellsize) + 1;

cells = vector< vector< vector< Bird* > > >(ccy);
for( int i = 0; i < ccy; i++ ){
cells[i] = vector< vector< Bird* > >(ccx);
for( int j = 0; j < ccx; j++ ){
cells[i][j] = vector< Bird* >();
}
}

for( auto b : flock->birds ){
int cx = (int)((b->pos.x - min.x) / cellsize);
int cy = (int)((b->pos.y - min.y) / cellsize);
b->cx = cx; b->cy = cy;
cells[cy][cx].push_back( b );
}
}

Grid::~Grid() {}

vector< Bird* > Grid::getNeighbors( float x, float y, float r ){
int cx = (x - min.x) / cellsize;
int cy = (y - min.y) / cellsize;

vector< Bird* > ret;
int m = ceil( r / cellsize );
for( int i = cy-m; i <= cy+m; i++ ){
for( int j = cx-m; j <= cx+m; j++ ){
if( j < 0 || i < 0 || j >= ccx || i >= ccy )
continue;
ret.insert( ret.end(), cells[i][j].begin(), cells[i][j].end());
}
}

return ret;
}

void Grid::update( Bird* bird ){
int cx = (bird->pos.x - min.x) / cellsize;
int cy = (bird->pos.y - min.y) / cellsize;

if( bird->cx != cx || bird->cy != cy ){
auto cell = &cells[bird->cy][bird->cx];
cell->erase( remove( cell->begin(), cell->end(), bird ) );
cells[cy][cx].push_back( bird );

bird->cx = cx; bird->cy = cy;
}
}
```

PARTNERS