gpt4 book ai didi

c++ - 在基本 C++ 引擎中使用一致速度计算速度

转载 作者:行者123 更新时间:2023-11-28 05:35:45 24 4
gpt4 key购买 nike

我想要实现的是一个 Sprite 在 2D 环境中移动到另一个 Sprite 。我从基本的 Mx = Ax - Bx 交易开始。但我注意到 Sprite 越接近目标,它的速度就越慢。所以我尝试根据速度创建一个百分比/比率,然后每个 x 和 y 都获得它们的速度津贴百分比,但是,它的行为非常奇怪并且似乎只有在 Mx 和 My 为正时才有效这是代码摘录:

ballX = ball->GetX();
ballY = ball->GetY();
targX = target->GetX();
targY = target->GetY();
ballVx = (targX - ballX);
ballVy = (targY - ballY);
percentComp = (100 / (ballVx + ballVy));
ballVx = (ballVx * percentComp)/10000;
ballVy = (ballVy * percentComp)/10000;

/10000 是为了减慢 Sprite 的运动

最佳答案

假设你想让 Sprite 以恒定的速度移动,你可以在 X 和 Y 位置上做一个线性淡入淡出,像这样:

#include <stdio.h>

int main(int, char **)
{
float startX = 10.0f, startY = 20.0f;
float endX = 35.0f, endY = -2.5f;
int numSteps = 20;

for (int i=0; i<numSteps; i++)
{
float percentDone = ((float)i)/(numSteps-1);
float curX = (startX*(1.0f-percentDone)) + (endX*percentDone);
float curY = (startY*(1.0f-percentDone)) + (endY*percentDone);

printf("Step %i: percentDone=%f curX=%f curY=%f\n", i, percentDone, curX, curY);
}
return 0;
}

关于c++ - 在基本 C++ 引擎中使用一致速度计算速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38331706/

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