public Vector3 GetNextInterpolatedPosition(GameTime deltaTime, Vector3 position, float intervals, TerrainNode terrain)
        {
            Vector3 toRet = m_LastPosition;

            if (m_ControlPoints.Count > 0)
            {
                int max = (m_ControlPoints.Count - 1);

                // If the current interpolate value is greater than or equal to the value of "1" then the end of the path between these
                // two control points has been reached so increase the control point index.
                if (m_CurrentInterpolateValue >= 1)
                {
                    m_CurrentInterpolateValue = 0;

                    if (m_ControlPointIndex < (m_ControlPoints.Count - 2))
                        m_ControlPointIndex++;
                    else
                    {
                        if (m_Loop)
                            m_ControlPointIndex = 0;
                        else
                        {
                            m_EndReached = true;
                            toRet = m_ControlPoints[max];
                        }
                    }
                }

                // Make sure the end hasn't been reached yet
                if (!m_EndReached)
                {
                    toRet = CatMullRom3D(m_ControlPoints[Wrap((m_ControlPointIndex - 1), max)],
                                          m_ControlPoints[Wrap(m_ControlPointIndex, max)],
                                          m_ControlPoints[Wrap((m_ControlPointIndex + 1), max)],
                                          m_ControlPoints[Wrap((m_ControlPointIndex + 2), max)],
                                          m_CurrentInterpolateValue);

                    // Increase the interpolate value by the speed * deltaTime
                    if (intervals <= 0)
                        intervals = 0;

                    m_CurrentInterpolateValue += (1 / intervals) * (float)deltaTime.ElapsedGameTime.TotalSeconds;
                }
            }

            if (terrain != null)
                toRet.Y = terrain.GetHeight(toRet);

            // Set the last position to be returned ( if the end has been reached then it should set itself to what it already is 
            // and return the same value ( hence not move anywhere else
            m_LastPosition = toRet;

            return toRet;
        }
        public void AddControlPoints(List<Vector3> newPoints, TerrainNode terrain)
        {
            for (int i = 0; i < newPoints.Count; i++ )
            {
                Vector3 point = newPoints[i];

                // If a terrain has been passed in, check the height of the terrain
                // at the position the new point is at
                if (terrain != null)
                    point.Y = terrain.GetHeight(point); // +newPoint.Y;

                if (m_ControlPoints.Count > 0)
                {
                    // Check whether to have the end point the same as the start or whether to just directly add a point
                    // Depends on looping or not
                    if (m_Loop)
                        m_ControlPoints.Insert((m_ControlPoints.Count - 1), point);
                    else
                        m_ControlPoints.Add(point);
                }
                else
                {
                    m_ControlPoints.Add(point);

                    // If looping add the point again as the end point
                    if (m_Loop)
                        m_ControlPoints.Add(point);
                }
            }
        }
        public void InterpolatePoints( int divisions, TerrainNode terrain, float terrainOffset )
        {
            // If the points havn't been interpolated already and points exist to interpolate between
            if (m_ControlPoints.Count > 0)
            {
                // Create a new list of positions
                List<Vector3> interpolatedPositions = new List<Vector3>();
                int maxPoints = (m_ControlPoints.Count - 1);

                // Between each control point....
                for (int i = 0; i < maxPoints; i++)
                {
                    // Add the control point to the new list
                    interpolatedPositions.Add(m_ControlPoints[i]);

                    // ADd the specifed number of interpolated points
                    for (int j = 0; j < divisions; j++)
                    {
                        // Determine how far to interpolate
                        float amount = ((float)(j + 1) / (float)(divisions + 2));

                        // Find the position based on catmull-rom interpolation
                        Vector3 interpolation = CatMullRom3D(m_ControlPoints[Wrap((i - 1), maxPoints)],
                                                             m_ControlPoints[Wrap(i, maxPoints)],
                                                             m_ControlPoints[Wrap((i + 1), maxPoints)],
                                                             m_ControlPoints[Wrap((i + 2), maxPoints)],
                                                             amount);

                        // Check if a terrain node was passed in or not
                        if (terrain != null)
                        {
                            // If so then check this interpolated points height against
                            // the terrain heights and adjust accordingly
                            interpolation.Y = terrain.GetHeight(interpolation) + terrainOffset;
                        }

                        // Add the new point to the list
                        interpolatedPositions.Add(interpolation);
                    }
                }

                // Add the last point
                interpolatedPositions.Add(m_ControlPoints[maxPoints]);

                // Save the interpolated positions over the original positions
                m_InterpolatedPoints = interpolatedPositions;
            }
        }
Exemple #4
0
 public TerrainSetEvent(TerrainNode terrain)
 {
     m_Terrain = terrain;
 }