Esempio n. 1
0
        internal void DoDamping()
        {
            if (bodyID == dBodyID.Zero)
            {
                return;
            }

            Ode.dMass mass = new Ode.dMass();
            Ode.dBodyGetMass(bodyID, ref mass);

            // Linear damping
            if (LinearDamping != 0)
            {
                // The damping force depends on the damping amount, mass, and velocity
                // (i.e. damping amount and momentum).
                float factor = -LinearDamping * mass.mass;
                Vec3  force  = LinearVelocity * factor;

                // Add a global force opposite to the global linear velocity.
                Ode.dBodyAddForce(bodyID, force.X, force.Y, force.Z);
            }

            // Angular damping
            if (AngularDamping != 0)
            {
                Vec3 localVelocity;
                {
                    Ode.dVector3 aVelLocal = new Ode.dVector3();
                    Ode.dBodyVectorFromWorld(bodyID, AngularVelocity.X, AngularVelocity.Y,
                                             AngularVelocity.Z, ref aVelLocal);
                    localVelocity = Convert.ToNet(aVelLocal);
                }

                // The damping force depends on the damping amount, mass, and velocity
                // (i.e. damping amount and momentum).
                float factor = -AngularDamping;

                Vec3 momentum = new Vec3(
                    Vec3.Dot(new Vec3(mass.I.M00, mass.I.M01, mass.I.M02), localVelocity),
                    Vec3.Dot(new Vec3(mass.I.M10, mass.I.M11, mass.I.M12), localVelocity),
                    Vec3.Dot(new Vec3(mass.I.M20, mass.I.M21, mass.I.M22), localVelocity));

                Vec3 torque = momentum * factor;

                // Add a local torque opposite to the local angular velocity.
                Ode.dBodyAddRelTorque(bodyID, torque.X, torque.Y, torque.Z);
            }
        }