Ejemplo n.º 1
0
        /// <summary>
        /// Computes current position and orientation in the curve
        /// </summary>
        /// <param name="gameTime">Game time</param>
        /// <param name="manipulator">Manipulator to update</param>
        public override void UpdateManipulator(GameTime gameTime, Manipulator3D manipulator)
        {
            if (this.HasPath)
            {
                var   target    = this.path.GetNextControlPoint(this.path.Length);
                var   position  = manipulator.Position;
                float dToTarget = (target - position).Length();

                if (dToTarget > this.ArrivingThreshold)
                {
                    float maxSpeed = this.MaximumSpeed * gameTime.ElapsedSeconds;

                    this.pathTime += maxSpeed;

                    var next = this.path.GetPosition(this.pathTime);
                    this.Velocity = next - position;

                    manipulator.SetPosition(next, true);
                    manipulator.LookAt(next + this.Velocity, false, 0, true);
                }
                else
                {
                    this.Clear();
                }
            }
        }
        /// <summary>
        /// Updates the manipulator's view and position
        /// </summary>
        /// <param name="gameTime">Game time</param>
        /// <param name="manipulator">Manipulator</param>
        public override void UpdateManipulator(GameTime gameTime, Manipulator3D manipulator)
        {
            if (this.HasPath)
            {
                var   target    = this.path.GetNextControlPoint(this.path.Length);
                var   position  = manipulator.Position;
                float dToTarget = (target - position).Length();

                if (dToTarget > this.ArrivingThreshold)
                {
                    float maxSpeed = this.MaximumSpeed * gameTime.ElapsedSeconds;
                    float maxForce = this.MaximumForce * gameTime.ElapsedSeconds;

                    var next = this.path.GetNextControlPoint(this.pathTime + maxSpeed);

                    // A vector pointing from the location to the target
                    var   desired = (next - position);
                    float dToNext = desired.Length();
                    if (dToNext != 0)
                    {
                        if (dToTarget < this.ArrivingRadius)
                        {
                            var m = Map(dToTarget, 0, this.ArrivingRadius, 0, maxSpeed);
                            desired = Vector3.Normalize(desired) * m;
                        }
                        else
                        {
                            desired = Vector3.Normalize(desired) * maxSpeed;
                        }

                        // Steering = Desired minus Velocity
                        var steer = desired - this.Velocity;

                        // Limit to maximum steering force
                        steer = steer.Limit(maxForce);

                        // Update velocity
                        this.Velocity += steer;

                        // Limit speed
                        this.Velocity = this.Velocity.Limit(maxSpeed);

                        this.pathTime += this.Velocity.Length();
                        var newPosition = this.path.GetPosition(this.pathTime);
                        var newNormal   = this.path.GetNormal(this.pathTime);

                        manipulator.SetPosition(newPosition, true);
                        manipulator.LookAt(newPosition + (newPosition - position), newNormal, true, 0.1f);
                    }
                }
                else
                {
                    this.Clear();
                }
            }
        }