Bug in Verlet integrated physics engine.

Started by
5 comments, last by Lars-Kristian 11 years, 6 months ago
Hi everyone.

I am working on a game that uses Verlet integrated physics, and it works pretty well.
I got 2 types of objects vertices and edges.

The problem is when 2 horizontal edges collide and one of them is static, the other one jumps pretty high.

[attachment=11585:path3961-6.png]

Is this a common problem? does anyone know how to fix it?
Advertisement
Its not common, i think. one object jumps becouse it layering other object too deep inside, it hapens than object speed is very high and steps are big or its got pressure from other objects or it depends on collision and (if exist) constraint satisfaction routine or verlet system update frequency or colision not allways calculated or ...
I had some problems with it, but it was just too meny object pressure, so I build in limits of object speed and it solved to me the problem.
But yours , I dont know, must see verlet integration code first to find the bug...
Thanks for a good reply

The game is a typical bridge builder game. And the problem is with the train.

I have managed to get the jumping down to 2-3px, but it still looks horrible.

[attachment=11605:test.png]

The train has not yet been given a texture

. tongue.png


I think the problem is here

v1 and v2 is the edge, and m is the colliding vertex.

private void CollidingE(Vertex m, Vertex v1, Vertex v2, Vector2 cv) //cv = Collision Vector
{
if (v1.active == false && v2.active == false)
{
m.pos += cv * fraction;
return;
}

float t;
if (Math.Abs(v1.pos.X - v2.pos.X) > Math.Abs(v1.pos.Y - v2.pos.Y))
{
t = (m.pos.X - cv.X - v1.pos.X) / (v2.pos.X - v1.pos.X);
}
else
{
t = (m.pos.Y - cv.Y - v1.pos.Y) / (v2.pos.Y - v1.pos.Y);
}
float lambda = 1.0f / (t * t + (1 - t) * (1 - t));


Vector2 v1pos = v1.pos;
Vector2 v2pos = v2.pos;
Vector2 mpos = m.pos;

if (m.active == true)
{
if (v1.active == true && v2.active == true)
{
v1pos -= cv * ((1 - t) * lambda / 2.0f) * fraction;
v2pos -= cv * (t * lambda / 2.0f) * fraction;
}
else if (v1.active == false && v2.active == true)
{
v2pos -= cv * (t * lambda) * fraction;
}
else if (v1.active == true && v2.active == false)
{
v1pos -= cv * ((1 - t) * lambda) * fraction;
}
mpos += cv * fraction;
}
else
{
if (v1.active == true && v2.active == true)
{
v1pos -= cv * ((1 - t) * lambda) * fraction;
v2pos -= cv * (t * lambda) * fraction;
}
else if (v1.active == false && v2.active == true)
{
v2pos -= cv * (t * lambda * 2) * fraction;
}
else if (v1.active == true && v2.active == false)
{
v1pos -= cv * ((1 - t) * lambda * 2) * fraction;
}
}
v1.pos = v1pos;
v2.pos = v2pos;
m.pos = mpos;
}



Or here

private void UpdateVertices(List<Vertex> list)
{
Vector2 tmp;
foreach (Vertex v in list)
{
Vector2 pos = v.pos;
Vector2 oldpos = v.oldpos;
Vector2 deltapos = v.deltapos;
if (v.type == Vertex.NORMAL)
{
deltapos.X = 0;
deltapos.Y = v.mass;
deltapos -= (pos - oldpos) * 0.0001f;//0.0001f * (pos - oldpos);
tmp = pos;
pos += pos - oldpos + deltapos;
oldpos = tmp;
}
else if (v.type == Vertex.TRAIN)
{
deltapos.X = 0;
deltapos.Y = v.mass;
deltapos -= 0.05f * (pos - oldpos);
tmp = pos;
pos += pos - oldpos + deltapos;
oldpos = tmp;
}
else
{
pos = oldpos;
}
v.pos = pos;
v.oldpos = oldpos;
v.deltapos = deltapos;
}
}

I have looked at your code, and find some strange things...

cv * fraction - is this a friction or that? If so its applies to all direction independantly on surfase orentation. Must be orented to surface...

You sometimes push v1 and v2, but dont push m, I downt know these parameters belongs to train or bridge, are theey skick or not

After such code you push v1 v2 /2 , bus push m /1
{
v1pos -= cv * ((1 - t) * lambda / 2.0f) * fraction;
v2pos -= cv * (t * lambda / 2.0f) * fraction;
}

Must be m/2 too:
mpos += cv * fraction/2,0f;

If deltapos is for inertia, your update function looks weard for me:
object mass must figure in collision routine not in object move
I simplefied your coude and mass is gravity?gravity for all ojects must be the same :)

tmp = pos;
pos += (pos - oldpos)* 0.9999f+(0,v.mass);
oldpos = tmp;
I understand that not everything makes sense. :P
The fraction thing is a variable between 0.5-1.0, it helps making everything more stable.
If something collides, instead of moving both object all the away from each other, they are only moved by a fraction. like a dampening effect.

m is a vertex, v1 and v2 is an edge.
If a vertex is active it can move, but not if its not active.

I agree with that mass thing.
Are you saying that if 2 objects collide, the object with the least mass moves the most?
hmm wrote an answer but it disapeard .
ok I will answer again smile.png
I think maybe your problem is in two places , try to rewrite these places with code below
maybe this helps...



if (v1.active == true && v2.active == true)
{
v1pos -= cv * ((1 - t) * lambda / 2.0f) * fraction; // (1-t)/2
v2pos -= cv * (t * lambda / 2.0f) * fraction; // t/2 ( t+(1-t))/2=1/2 half energy for edge
}
else if (v1.active == false && v2.active == true)
{
v2pos -= cv * (t * lambda/2.0f) * fraction; //half energy for edge
}
else if (v1.active == true && v2.active == false)
{
v1pos -= cv * ((1 - t) * lambda/2.0f) * fraction; //half energy
}
mpos += cv * fraction/2.0f; // if half energy for edge so must leav half energy to m


About mass, ofcouse if mass different , collision will effect diferently for that objects



//redistribute energy to objects with different mass
float c=1/(obj1.invmass+obj2.invmass);
obj1.vec -= obj1.invmass*c/2*collision_vector;
obj2.vec += obj1.invmass*c/2*collision_vector;
//invmas=1/mass;
Thanks for the help, I will try those things tomorrow. :D

This topic is closed to new replies.

Advertisement