使用 Curve3D 类进行 3D 路径绘制 - 问题

发布于 2024-10-26 08:35:15 字数 3025 浏览 1 评论 0原文

我目前正在开发一个涉及生成随机轨道的项目(Z 轴上的固定增量和随机 X 和 Y 来创建崎岖/弯曲的轨道),我使用 Catmull Rom 插值实现了该轨道,该轨道按预期工作。这会生成 9800 个点,这些点依次存储在二维数组中。

我的主要问题是,我现在尝试沿着轨道移动一个对象(用于相机使用,以及稍后要跟随的化身)。我目前正在根据 msdn 帮助使用 Curve3D 类 http://msdn.microsoft.com/en-us /library/microsoft.xna.framework.curve.aspx 我有一个函数,它根据时间值和带有 catmull 结果的数组来分配“testCube”的位置。

现在解决问题;从广义上讲,我的立方体沿着轨道移动,但是随着轨道的进展,它似乎以越来越高的频率来回跳跃。我已经消除了任何其他可能干扰它的代码。

我当前正在使用:

public void UpdateMotionCube(GameTime gameTime1)
        {
            testCube.position = motionCubePosition.GetPointOnCurve((float)motionTime);
           motionTime += gameTime1.ElapsedGameTime.TotalMilliseconds;
        }


public void InitiateCurve()
        {
            float time = 0;
            //for (int row = 0; row < 99; row++)

                for (int col= 0; col < 99; col++)
                {
                    //assigns positions to the new curve from drawLocValues
                    motionCubePosition.AddPoint(newTrack.basicPoints/*.drawlocValues*/[/*row,*/ col], time);
                    time += timeConst;
                }



            motionCubePosition.setTangents();
        }

basicPoints 数组保存我用于 catmullRom 插值的原始 100 个点,而 drawLocValues 是插值的结果(分别为 100 和 10000 个值)

关于为什么 testCube 没有沿着轨道直接路径的任何建议反对朝着正确的方向前进,但来回弹跳将不胜感激。 顺便说一句,两个向量数组的值都是正确的,因为我已经检查过它们(并且轨道本身绘制正确),我还在曲线类中为所有 xyz 后循环和前循环使用 CurveX.postloop.CurveLoopType.Linear。

谢谢

编辑: 根据要求设置切线代码:

 public void setTangents()
        {
            CurveKey prev;
            CurveKey current;
            CurveKey next;
            int prevIndex = 0;
            int nextIndex = 0;


            for (int i = 0; i < curveX.Keys.Count; i++)
            {

                prevIndex = i - 1;
                if (prevIndex < 0)
                {
                    prevIndex = i;
                    nextIndex = i + 1;
                }
                if (nextIndex == curveX.Keys.Count) nextIndex = i;
                prev = curveX.Keys[prevIndex];
                next = curveX.Keys[nextIndex];
                current = curveX.Keys[i];
                SetCurveKeyTangent(ref prev, ref current, ref next);
                curveX.Keys[i] = current;
                prev = curveY.Keys[prevIndex];
                next = curveY.Keys[nextIndex];
                current = curveY.Keys[i];
                SetCurveKeyTangent(ref prev, ref current, ref next);
                curveY.Keys[i] = current;

                prev = curveZ.Keys[prevIndex];
                next = curveZ.Keys[nextIndex];
                current = curveZ.Keys[i];
                SetCurveKeyTangent(ref prev, ref current, ref next);
                curveZ.Keys[i] = current;
            }
        }

为了回答您的问题,是的,它本质上是一个包装器。问题似乎在于 testCube.position.Z 将其打印在屏幕上,它开始正常,但随着时间的推移,它开始以不断增加的值加倍,一般来说,它保持前进的动力,但由于缺乏更好的术语它向前口吃,然后向后口吃等。

谢谢您到目前为止的回复

Im currently working on a project that involves generating a random track (fixed increments on the Z axis and randomized X and Y to create a bumpy/bendy track) i have the track implemented using Catmull Rom interpolation which works as expected. This generates 9800 points which are in turn stored in a 2d array.

My main issue is that i am now attempting to path an object along the track (for camera usage and later an avatar to follow). I am currently using the Curve3D class as per msdn help
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.curve.aspx i have a function which assigns the position of the "testCube" based off the values time and the array with the catmull results in.

Now to the problem ; my cube paths down the track in a broad sense however as the track progresses it appears to jump backwards and forwards with increasing frequency. I have eliminated any other code as a possibility for interfering with it.

i am currently using:

public void UpdateMotionCube(GameTime gameTime1)
        {
            testCube.position = motionCubePosition.GetPointOnCurve((float)motionTime);
           motionTime += gameTime1.ElapsedGameTime.TotalMilliseconds;
        }


public void InitiateCurve()
        {
            float time = 0;
            //for (int row = 0; row < 99; row++)

                for (int col= 0; col < 99; col++)
                {
                    //assigns positions to the new curve from drawLocValues
                    motionCubePosition.AddPoint(newTrack.basicPoints/*.drawlocValues*/[/*row,*/ col], time);
                    time += timeConst;
                }



            motionCubePosition.setTangents();
        }

the basicPoints array holds the original 100 points i used for catmullRom interpolation and the drawLocValues are the results from the interpolation (100 and 10000 values respectively)

Any advice as to why the testCube isn't pathing straight down the track as opposed to heading in the correct direction but bouncing back and forwards would be greatly appreciated.
As an aside the values for both of the vector arrays are correct as i have checked them (and the track itself draws correctly) i am also using CurveX.postloop.CurveLoopType.Linear inside the curve class for all xyz post and pre loop.

Thanks

EDIT:
Set Tangents Code as requested:

 public void setTangents()
        {
            CurveKey prev;
            CurveKey current;
            CurveKey next;
            int prevIndex = 0;
            int nextIndex = 0;


            for (int i = 0; i < curveX.Keys.Count; i++)
            {

                prevIndex = i - 1;
                if (prevIndex < 0)
                {
                    prevIndex = i;
                    nextIndex = i + 1;
                }
                if (nextIndex == curveX.Keys.Count) nextIndex = i;
                prev = curveX.Keys[prevIndex];
                next = curveX.Keys[nextIndex];
                current = curveX.Keys[i];
                SetCurveKeyTangent(ref prev, ref current, ref next);
                curveX.Keys[i] = current;
                prev = curveY.Keys[prevIndex];
                next = curveY.Keys[nextIndex];
                current = curveY.Keys[i];
                SetCurveKeyTangent(ref prev, ref current, ref next);
                curveY.Keys[i] = current;

                prev = curveZ.Keys[prevIndex];
                next = curveZ.Keys[nextIndex];
                current = curveZ.Keys[i];
                SetCurveKeyTangent(ref prev, ref current, ref next);
                curveZ.Keys[i] = current;
            }
        }

To answer your question , yes it is essentially a wrapper. The issue seems to lie in the testCube.position.Z having printed it out on screen it starts okay but as the time progresses it begins to double back on itself in ever increasing values , it maintains forward momentum generally speaking but for want of a better term it stutters forwards then backwards etc.

Thank you for the replies thus far

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-11-02 08:35:15

我猜测 gameTime1.ElapsedGameTime.TotalMilliseconds 是自上次调用 UpdateMotionCube 以来经过的时间?

  1. testCube.position = 肯定应该在更新运动时间后发生吗?
  2. 检查您的输入:打印出您的要点列表并确保它们确实取得了进展。
  3. 检查您的输入:游戏时间是否以合理的方式增加?
  4. 打印出您检索到的游戏时间、动作时间和结果分数,以确保一切都正确地发生。

I'm guessing gameTime1.ElapsedGameTime.TotalMilliseconds is the elapsed time since UpdateMotionCube was last called?

  1. Surely testCube.position = should happen after you've updated the motion time?
  2. Check your input: Print out your list of points and make sure they actually do progress.
  3. Check your input: Does the game time increment in a reasonable fasion?
  4. Print out the gametime, motion time and resulting points you retrieve to ensure that evrything's happening properly together.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文