0

I am trying to implement an angular constraint into a simple verlet integration based 2D physic engine. This is the code I am currently using:

int indexA = physicAngularConstraint[i].indexA;
int indexB = physicAngularConstraint[i].indexB;
int indexC = physicAngularConstraint[i].indexC;

CGPoint e = CGPointSubtract(physicParticle[indexB].pos,physicParticle[indexA].pos);
CGPoint f = CGPointSubtract(physicParticle[indexC].pos,physicParticle[indexB].pos);

float dot = CGPointDot(e, f);
float cross = CGPointCross(e, f);
float angle = atan2f(cross, dot);

float da = (angle < physicAngularConstraint[i].minAngle)? angle - physicAngularConstraint[i].minAngle : (angle > physicAngularConstraint[i].maxAngle)? angle - physicAngularConstraint[i].maxAngle : 0.0f;

if (da != 0.0f)
{
    physicParticle[indexA].pos = CGPointRotate(physicParticle[indexA].pos,
                                               physicParticle[indexB].pos, da);
    physicParticle[indexC].pos = CGPointRotate(physicParticle[indexC].pos,
                                               physicParticle[indexB].pos, -da);
}

The CGPointRotate function looks like this:

CGPoint CGPointRotate(CGPoint pt, CGPoint center, float angle)
{
    CGPoint ret;

    pt = CGPointSubtract(pt, center);

    float co = cosf(angle);
    float si = sinf(angle);
    ret.x = pt.x * co - pt.y * si;
    ret.y = pt.x * si + pt.y * co;

    ret = CGPointAdd(ret, center);

    return ret;
}

I am testing this implementation with a row of particles which are connected through distant constraints. Without the angular constraints they act like a rope. I am trying to give the "rope" some stiffness through the angular constraints. But my implementation above is gaining energy and blowing up the system after some millisecs. Why does this constrain implementation is gaining energie?

quadrupleslap
  • 460
  • 1
  • 6
  • 18
  • This is not how one implements constraints. Simply rotating the particles while keeping their velocities results in states that have different total energy. Even if you'd correct the velocity for the gain (or loss) of potential energy, the end result will have little to do with the laws of physics. You should read about the RATTLE and SHAKE algorithms to understand how holonomic constraints are usually implemented. If you are into math, click [here](http://www.ccp5.ac.uk/infoweb/knowledge_center/kutteh.pdf). – Hristo Iliev Jun 08 '15 at 15:29
  • I searched for RATTLE and SHAKE but only found mathematical papers. I am not into math that much so they are hard to understand for me. Do you know any kind of tutorial of how to implementing this technics? I am trying to animate something like a tree with particle physics. I don't need any collision nor much realism. Something like this without the collisions: https://youtu.be/eH9y05ub99o –  Jun 08 '15 at 20:12
  • Unfortunately I'm not aware of such tutorials. Aren't such constraint integrators included in most open-source physics engines? You could also take a look at some open-source molecular dynamics package and see how it is implemented there. LAMMPS, for example, is written in C++. – Hristo Iliev Jun 08 '15 at 20:46

0 Answers0