Bobby5808
unread,May 27, 2009, 5:25:46 AM5/27/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Physics2D.Net
Could someone help please? Ive integrated it into the FixedHingeJoint
Class, but had some problems applying the impulse. It works fine on
ordinary objects, but fixed objects(infinite mass) there are some
problems.
Heres the 2 functions Prestep and ApplyImpulse, if someone would
please look at them.
(ive got a feeling that I should use the Add/Subtract impulse
functions, but I dont know where to put the motors impulse into it)
void Solvers.ISequentialImpulsesJoint.PreStep(TimeStep step)
{
Scalar mass1Inv = body.Mass.MassInv;
Scalar inertia1Inv = body.Mass.MomentOfInertiaInv;
// Pre-compute anchors, mass matrix, and bias.
Vector2D.TransformNormal(ref body.Matrices.ToWorld, ref
localAnchor1, out r1);
// deltaV = deltaV0 + K * impulse
// invM = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew
(r1) - skew(r2) * invI2 * skew(r2)]
// = [1/m1+1/m2 0 ] + invI1 * [r1.y*r1.y -
r1.x*r1.y] + invI2 * [r1.y*r1.y -r1.x*r1.y]
// [ 0 1/m1+1/m2] [-r1.x*r1.y
r1.x*r1.x] [-r1.x*r1.y r1.x*r1.x]
Matrix2x2 K;
K.m00 = mass1Inv;
K.m11 = mass1Inv;
K.m00 += inertia1Inv * r1.Y * r1.Y;
K.m01 = -inertia1Inv * r1.X * r1.Y;
K.m10 = -inertia1Inv * r1.X * r1.Y;
K.m11 += inertia1Inv * r1.X * r1.X;
K.m00 += softness;
K.m11 += softness;
Matrix2x2.Invert(ref K, out M);
//MOTOR
_motorMass = 1.0f / inertia1Inv ;
maxImpulse = step.Dt * _maxMotorTorque; //used in next
step - put it here cause no dt in next step
if (_enableMotor == false)
{
_motorImpulse = 0.0f;
}
Vector2D dp;
Vector2D.Add(ref body.State.Position.Linear, ref r1, out
dp);
Vector2D.Subtract(ref anchor, ref dp, out dp);
if (!Scalar.IsPositiveInfinity(distanceTolerance) &&
dp.MagnitudeSq > distanceTolerance *
distanceTolerance)
{
this.Lifetime.IsExpired = true;
}
if (solver.PositionCorrection)
{
//bias = -0.1f * dtInv * dp;
Scalar flt = -biasFactor * step.DtInv;
Vector2D.Multiply(ref dp, ref flt, out bias);
}
else
{
bias = Vector2D.Zero;
}
if (solver.WarmStarting)
{
_motorImpulse *= step.DtInv * step.Dt;
PhysicsHelper.SubtractImpulse(
ref body.State.Velocity, ref accumulatedImpulse,
ref r1, ref mass1Inv, ref inertia1Inv );
//Motor
body.State.Velocity.Angular += _motorImpulse;
}
else
{
accumulatedImpulse = Vector2D.Zero;
_motorImpulse = 0;
}
body.ApplyProxy();
}
void Solvers.ISequentialImpulsesJoint.ApplyImpulse()
{
Scalar mass1Inv = body.Mass.MassInv;
Scalar inertia1Inv = body.Mass.MomentOfInertiaInv;
Vector2D dv;
PhysicsHelper.GetRelativeVelocity(ref body.State.Velocity,
ref r1, out dv);
float w1 = 0;
//MOTOR
if (_enableMotor)
{
w1 = body.State.Velocity.Angular;
float Cdot = w1 - _motorSpeed;
float mimpulse = _motorMass * (-Cdot);
float oldImpulse = _motorImpulse;
//float maxImpulse = step.Dt * _maxMotorTorque;
_motorImpulse = MathHelper.Clamp(_motorImpulse +
mimpulse, -maxImpulse, maxImpulse);
mimpulse = _motorImpulse - oldImpulse;
w1 = inertia1Inv * mimpulse;
}
Vector2D impulse;
impulse.X = bias.X - dv.X - softness *
accumulatedImpulse.X;
impulse.Y = bias.Y - dv.Y - softness *
accumulatedImpulse.Y;
Vector2D.Transform(ref M, ref impulse, out impulse);
//impulse = M * (bias - dv - softness * P);
PhysicsHelper.SubtractImpulse(
ref body.State.Velocity, ref impulse,
ref r1, ref mass1Inv, ref inertia1Inv);
//Motor
body.State.Velocity.Angular += w1;
Vector2D.Add(ref accumulatedImpulse, ref impulse, out
accumulatedImpulse);
body.ApplyProxy();
}