gpt4 book ai didi

c++ - 新旧位置之间的速度

转载 作者:行者123 更新时间:2023-11-28 04:28:37 25 4
gpt4 key购买 nike

我在这里看到了通过为 UNITY 保存旧位置来提高速度的帖子,一些代码:

void TestVelocity(Vector3& Pos, Vector3 &Result)
{
Vector3 PreviousPos;

if (GetTickCount() >= velupd)
{
velupd = GetTickCount() + 100//random timer for test;

Vector3 Diff = Pos - PreviousPos;
float Len = sqrtf(Diff .x * Diff .x + Diff .y * Diff .y + Diff .z * Diff .z);

if (Len >= 0.01)
{
Result = (Diff / Len);
}
}
PreviousPos = Pos.
}

计算错误。数据仅可用对象位置(无速度等)。

最佳答案

需要更多详细信息才能为您提供更准确的答案。但我基本上看到两个问题。您没有初始化 PreviousPos 对象,在我看来您希望它持续存在,对吧?

假设Vector3类有减号(-)运算符重载,所以你可以做这个操作,你可能会这样做。

void TestVelocity(Vector3& Pos, Vector3 &Result)
{
static Vector3 PreviousPos; //Initialize here the initial position to zero with your constructor

if (GetTickCount() >= velupd)
{
velupd = GetTickCount() + 100//random timer for test;

Vector3 Diff = Pos - PreviousPos;
float Len = sqrtf(Diff .x * Diff .x + Diff .y * Diff .y + Diff .z * Diff .z);

if (Len >= 0.01)
{
Result = (Diff / Len);
}
}
PreviousPos = Pos.
}

另一种解决方案是将先前的位置作为参数,但正如我所说,如果我们不知道您想要的实现是什么,则很难判断。这些是根据需要的建议

关于c++ - 新旧位置之间的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53614358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com