box2d 计算下一帧的位置/角度
var dt:Number=1/30; var y0:Number=_body.GetPosition().y; var y:Number=y0+_body.GetLinearVelocity().y*dt+_world.GetGravity().y*dt*dt; trace(y0, "y:"+y);
b2Island中的实现:
var l_gravity:b2Vec2=b.m_customGravity?b.m_customGravity:gravity; //==============================added================================ // Integrate velocities. //b.m_linearVelocity += step.dt * (gravity + b.m_invMass * b.m_force); b.m_linearVelocity.x += step.dt * (l_gravity.x + b.m_invMass * b.m_force.x); b.m_linearVelocity.y += step.dt * (l_gravity.y + b.m_invMass * b.m_force.y); b.m_angularVelocity += step.dt * b.m_invI * b.m_torque; // Apply damping. // ODE: dv/dt + c * v = 0 // Solution: v(t) = v0 * exp(-c * t) // Time step: v(t + dt) = v0 * exp(-c * (t + dt)) = v0 * exp(-c * t) * exp(-c * dt) = v * exp(-c * dt) // v2 = exp(-c * dt) * v1 // Taylor expansion: // v2 = (1.0f - c * dt) * v1 b.m_linearVelocity.Multiply( b2Math.Clamp(1.0 - step.dt * b.m_linearDamping, 0.0, 1.0) ); b.m_angularVelocity *= b2Math.Clamp(1.0 - step.dt * b.m_angularDamping, 0.0, 1.0);
角度:
var dt:Number=1/30; var ang:Number=_body.GetAngle(); var angV:Number=_body.GetAngularVelocity(); angV += dt * _body.m_invI * _body.m_torque; angV*=dt angV *= b2Math.Clamp(1.0 - dt * _body.m_angularDamping, 0.0, 1.0); var nextAng:Number=ang+angV; /*trace("ang:",ang); trace("angV:",angV); trace("nextAng:",nextAng+"\n");*/
LinearVelocity与像素的关系:
不考虑摩擦,刚体质量为1,无重力的情况下
linearVelocity*dt=distancePixel(像素距离)/ptm_ratio(像素到b2World单位)