示例#1
0
        //update the rigidbodies position based on currently applied force, velocity, and gravity
        public void Step(float dt, Vector2D gravity)
        {
            //tick the cooldown
            if (multiUseCooldown > 0.0f)
            {
                multiUseCooldown -= dt;
            }

            //only update position if the object can move
            if (moveable)
            {
                Vector2D acceleration = Force / Mass;
                if (obeysGravity)
                {
                    acceleration += gravity;
                }

                //update velocity, locking it below maximum speed
                LinearVelocity = LinearVelocity + acceleration * dt;
                if (LinearVelocity.Length() > maxSpeed)
                {
                    LinearVelocity = LinearVelocity.Normalised() * maxSpeed;
                }


                //Update position based on velocity (uses Runge Kutta integration)
                Vector2D k1 = LinearVelocity + acceleration * dt;
                Vector2D k2 = LinearVelocity + k1 * (dt / 2);
                Vector2D k3 = LinearVelocity + k2 * (dt / 2);
                Vector2D k4 = LinearVelocity + k3 * dt;
                SetPosition(Position + (k1 + k2 * 2 + k3 * 2 + k4) * (dt / 6));

                //reset force
                Force.X = 0;
                Force.Y = 0;

                //reset collision information (assumes collision detection will take place immediately after stepping all rigidbodies)
                otherBody       = null;
                collisionNormal = null;
            }
        }