ardmax1

Members
  • Content count

    15
  • Joined

  • Last visited

Community Reputation

127 Neutral

About ardmax1

  • Rank
    Member
  1. Impulse based collision response (friction)

    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 ?
  2. Impulse based collision response (friction)

    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.
  3. Impulse based collision response (friction)

    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.       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 ?
  4. Impulse based collision response (friction)

    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.  
  5. 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      
  6. Space partitioning for flocking

    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? [source]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; } } [/source]
  7. Space partitioning for flocking

    But with grid I would need to search 9 cells right? And what if I would need different radius in future? Anyway I'll test the grid thing. I should also mention that this its in 2d and I'm just using points if that helps in some case.
  8. Hi, I'm looking for space partitioning structure which will be good for moving points and will enable fast fixed radius near neighbors search.
  9. ok I found out was the problem. First it was caused by clearing texture, which is used to apply shaders on whole screen rather that on parts that are drawn (This is I think SFML problem). Second, I had to normalize position, so it looks like this now [source]pixel = texture2D(texture, pos.xy / texResolution);[/source] but why this makes texture flip vertically?
  10. If it should work, then i have error somewhere else. This produces just red fragment: [sourcelang="cpp"]varying vec4 pos; uniform sampler2D texture; void main(){ vec4 pixel = texture2D(texture, vec2(pos)); if (pixel.a > 0.5){ gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }else{ gl_FragColor = vec4(0.0, 0.0, 1.0, 0.3); } } [/source] vertex: [source lang="cpp"]varying vec4 pos; void main(){ pos = gl_MultiTexCoord0; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; }[/source]
  11. Hi, is it possible to write GLSL shader such that it will change each texture pixel color based on its alpha value ? Something like: [source lang="cpp"] vec4 pixel = texture2D( texture, pos ); if( pixel.a > 0.5f ){ pixel = vec4( 0.0, 1.0, 0.0, 1.0 ); }else{ pixel = vec4( 1.0, 0.0, 0.0, 1.0 ); }[/source] If it is, could you guys give me tips on how to make it ?
  12. Concave + SAT + Tringulation

    Ok, i have some progress. I tried using SAT with each tringle in each polygon, and then sorting collisions by MTV, but its not perfect, it sometimes misses collision with some triangles. Heres how i do it pTringulation T; vector< shape > tria; vector< shape > trib; T.process(polya,tria); T.process(polyb,trib); for(int i = 0; i < tria.size(); i++){ for(int j = i + 1; j < trib.size(); j++){ satCheck(tria[i],trib[j]); } }
  13. Concave + SAT + Tringulation

    But ignoring that concave shape is concave and just using SAT as it is convex, will give wrong collision detection. So any other way to do this ?
  14. Concave + SAT + Tringulation

    I mean i have both tringulated shapes in vector, and how should i sat_check them to get collision result. Ill show part of code: pTringulation T; vector< shape > tria; vector< shape > trib; T.process(polya,tria); T.process(polyb,trib); and with convex shapes i was using this satCheck(polya,polyb); now how i do this with concave ?
  15. Hi, Im working on my 2d physics engine, its working quite well as for first engine, im using convex polygons only but now i want to add concave ones. So i create new concave polygon, i tringulate it, and heres part i dont know how to solve. I was using SAT for convex polygon collision detection, but now i dont know how to test both tringulated polygons with SAT. Can it be done with SAT, or i need other technique ?