我正在尝试将角度约束实现为基于简单的基于verlet集成的2D物理引擎。这是我目前使用的代码:
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);
}
CGPointRotate
函数如下所示:
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;
}
我正在使用一行通过远程约束连接的粒子来测试此实现。没有角度约束,它们就像一根绳子。我试图通过角度约束给“绳索”一些刚度。但是我上面的实现正在获得能量并在几毫秒之后炸毁系统。为什么这种限制实施正在获得能量?