Esempio n. 1
0
        public override void Update(GameTime gameTime)
        {
            float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds;
            float smoothRate;

            steeringBehaviours.timeDelta = timeDelta;
            force = steeringBehaviours.calculate();
            SteeringBehaviours.checkNaN(force);
            Vector3 newAcceleration = force / Mass;

            if (timeDelta > 0)
            {
                smoothRate = Utilities.Clip(9 * timeDelta, 0.15f, 0.4f) / 2.0f;
                Utilities.BlendIntoAccumulator(smoothRate, newAcceleration, ref acceleration);
            }

            velocity += acceleration * timeDelta;
            float speed = velocity.Length();

            if (speed > maxSpeed)
            {
                velocity.Normalize();
                velocity *= maxSpeed;
            }
            Position += velocity * timeDelta;

            // the length of this global-upward-pointing vector controls the vehicle's
            // tendency to right itself as it is rolled over from turning acceleration
            Vector3 globalUp = new Vector3(0, 0.2f, 0);
            // acceleration points toward the center of local path curvature, the
            // length determines how much the vehicle will roll while turning
            Vector3 accelUp = acceleration * 0.05f;
            // combined banking, sum of UP due to turning and global UP
            Vector3 bankUp = accelUp + globalUp;

            // blend bankUp into vehicle's UP basis vector
            smoothRate = timeDelta * 3;
            Vector3 tempUp = Up;

            Utilities.BlendIntoAccumulator(smoothRate, bankUp, ref tempUp);
            Up = tempUp;
            Up.Normalize();

            if (speed > 0.0001f)
            {
                Look = velocity;
                Look.Normalize();
                if (Look.Equals(Right))
                {
                    Right = Vector3.Right;
                }
                else
                {
                    Right = Vector3.Cross(Look, Up);

                    Right.Normalize();

                    SteeringBehaviours.checkNaN(ref Right, Vector3.Right);
                    Up = Vector3.Cross(Right, Look);
                    Up.Normalize();
                    SteeringBehaviours.checkNaN(ref Up, Vector3.Up);
                }
                // Apply damping
                velocity *= 0.99f;
            }

            if (Look != basis)
            {
                float   angle = (float)Math.Acos(Vector3.Dot(basis, Look));
                Vector3 axis  = Vector3.Cross(basis, Look);

                quaternion = Quaternion.CreateFromAxisAngle(axis, angle);
                quaternion.Normalize();

                worldTransform.Up      = Up;
                worldTransform.Forward = Look;
                worldTransform.Right   = Right;
                worldTransform         = Matrix.CreateWorld(Position, Look, Up);
                checkNan(worldTransform);
            }
            else
            {
                worldTransform = Matrix.CreateTranslation(Position);
            }
            drawAxis = false;
        }