Example #1
0
        public override void Update(GameTime gameTime)
        {
            // Update _time.
            _time += (float)gameTime.ElapsedGameTime.TotalSeconds;

            var debugRenderer = GraphicsScreen.DebugRenderer;

            debugRenderer.Clear();

            // Draw the path key points.
            foreach (var point in _path.Select(key => key.Point))
            {
                debugRenderer.DrawPoint(point, Color.Black, false);
            }

            // Draw the path.
            for (float i = 0; i < _path.Last().Parameter; i += 0.1f)
            {
                var point0 = _path.GetPoint(i);
                var point1 = _path.GetPoint((i + 0.1f));
                debugRenderer.DrawLine(point0, point1, Color.Black, false);
            }

            // Move an object with constant speed along the path.
            const float speed            = 2;
            var         traveledDistance = _time * speed;
            // Get path parameter where the path length is equal to traveledDistance.
            var parameter = _path.GetParameterFromLength(traveledDistance, 10, 0.01f);
            // Get path point at the traveledDistance.
            Vector3F position = _path.GetPoint(parameter);
            // Get the path tangent at traveledDistance and use it as the forward direction.
            Vector3F forward = _path.GetTangent(parameter).Normalized;

            // Draw an object on the path.
            DrawObject(position, forward);

            base.Update(gameTime);
        }
Example #2
0
        private void CreatePath()
        {
            // Create a cyclic path. (More information on paths can be found in the DigitalRune
            // Mathematics documentation and related samples.)
            _path = new Path3F
            {
                SmoothEnds = true,
                PreLoop    = CurveLoopType.Cycle,
                PostLoop   = CurveLoopType.Cycle
            };

            // The curvature of the path is defined by a number of path keys.
            _path.Add(new PathKey3F
            {
                Parameter     = 0,                              // The path parameter defines position of the path key on the curve.
                Point         = new Vector3(-4, 0.5f, -3),      // The world space position of the path key.
                Interpolation = SplineInterpolation.CatmullRom, // The type of interpolation that is used between this path key and the next.
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 1,
                Point         = new Vector3(-1, 0.5f, -5),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 2,
                Point         = new Vector3(3, 0.5f, -4),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 3,
                Point         = new Vector3(0, 0.5f, 0),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 4,
                Point         = new Vector3(-3, 0.5f, 3),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 5,
                Point         = new Vector3(-1, 0.5f, 5),
                Interpolation = SplineInterpolation.CatmullRom,
            });
            _path.Add(new PathKey3F
            {
                Parameter     = 6,
                Point         = new Vector3(0, 0.5f, 0),
                Interpolation = SplineInterpolation.CatmullRom,
            });

            // The last key uses the same position as the first key to create a closed path.
            PathKey3F lastKey = new PathKey3F
            {
                Parameter     = _path.Count,
                Point         = _path[0].Point,
                Interpolation = SplineInterpolation.CatmullRom,
            };

            _path.Add(lastKey);

            // The current path parameter goes from 0 to 7. This path parameter is not linearly
            // proportional to the path length. This is not suitable for animations.
            // To move an object with constant speed along a path, the path parameter should
            // be linearly proportional to the length of the path.
            // ParameterizeByLength() changes the path parameter so that the path parameter
            // at the each key is equal to the length of path (measured from the first key position
            // to the current key position).
            // ParameterizeByLength() uses an iterative process, we end the process after 10
            // iterations or when the error is less than 0.01f.
            _path.ParameterizeByLength(10, 0.01f);

            // Sample the path for rendering.
            int   numberOfSamples = _pointList.Length - 1;
            float pathLength      = _path.Last().Parameter;

            for (int i = 0; i <= numberOfSamples; i++)
            {
                Vector3 pointOnPath = _path.GetPoint(pathLength / numberOfSamples * i);
                _pointList[i] = pointOnPath;
            }
        }
Example #3
0
    private void CreatePath()
    {
      // Create a cyclic path. (More information on paths can be found in the DigitalRune
      // Mathematics documentation and related samples.)
      _path = new Path3F
      {
        SmoothEnds = true,
        PreLoop = CurveLoopType.Cycle,
        PostLoop = CurveLoopType.Cycle
      };

      // The curvature of the path is defined by a number of path keys.
      _path.Add(new PathKey3F
      {
        Parameter = 0,                                  // The path parameter defines position of the path key on the curve.
        Point = new Vector3F(-4, 0.5f, -3),             // The world space position of the path key.
        Interpolation = SplineInterpolation.CatmullRom, // The type of interpolation that is used between this path key and the next.
      });
      _path.Add(new PathKey3F
      {
        Parameter = 1,
        Point = new Vector3F(-1, 0.5f, -5),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 2,
        Point = new Vector3F(3, 0.5f, -4),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 3,
        Point = new Vector3F(0, 0.5f, 0),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 4,
        Point = new Vector3F(-3, 0.5f, 3),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 5,
        Point = new Vector3F(-1, 0.5f, 5),
        Interpolation = SplineInterpolation.CatmullRom,
      });
      _path.Add(new PathKey3F
      {
        Parameter = 6,
        Point = new Vector3F(0, 0.5f, 0),
        Interpolation = SplineInterpolation.CatmullRom,
      });

      // The last key uses the same position as the first key to create a closed path.
      PathKey3F lastKey = new PathKey3F
      {
        Parameter = _path.Count,
        Point = _path[0].Point,
        Interpolation = SplineInterpolation.CatmullRom,
      };
      _path.Add(lastKey);

      // The current path parameter goes from 0 to 7. This path parameter is not linearly
      // proportional to the path length. This is not suitable for animations.
      // To move an object with constant speed along a path, the path parameter should
      // be linearly proportional to the length of the path.
      // ParameterizeByLength() changes the path parameter so that the path parameter
      // at the each key is equal to the length of path (measured from the first key position
      // to the current key position).
      // ParameterizeByLength() uses an iterative process, we end the process after 10 
      // iterations or when the error is less than 0.01f.
      _path.ParameterizeByLength(10, 0.01f);

      // Sample the path for rendering.
      int numberOfSamples = _pointList.Length - 1;
      float pathLength = _path.Last().Parameter;
      for (int i = 0; i <= numberOfSamples; i++)
      {
        Vector3F pointOnPath = _path.GetPoint(pathLength / numberOfSamples * i);
        _pointList[i] = pointOnPath;
      }
    }