gpt4 book ai didi

c++ - 在 SFML 中移动一个球

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

我正在使用 SFML 制作乒乓球游戏,球是一个正方形。我查看了教程,但出于某种原因我无法让我的球动起来。我使用 cout 语句来检查球的位置,但它只是输出它的初始位置。我怀疑我的数学有问题……但球应该会移动。

void ball_traits(sf::RectangleShape ball, sf::RectangleShape leftPaddle)
{
double ballSpeed = 5;
int angle = 90;

//Scale X and Y will give the angle
double scaleX = cos(angle);
std::cout << scaleX << std::endl;
double scaleY = sin(angle);
double velocityX = scaleX * ballSpeed;
double velocityY = scaleY * ballSpeed;
//take original position
double moveX = ball.getPosition().x;
double moveY = ball.getPosition().y;

sf::Clock clock;
double elapsed = clock.restart().asSeconds();

moveX += velocityX * elapsed;
moveY += velocityY * elapsed;
std::cout << moveX << std::endl;
std::cout << moveY << std::endl;
ball.move(moveX, moveY);
}

我只想被推向正确的方向。这是我的第一款游戏,我的 C++ 技能有点生疏。[更新] 我忘了通过引用传递对象(脸掌)。

最佳答案

我认为耗时非常非常接近于零。为零。是的。您应该将时钟声明为全局变量。

对于 sin 和 cos 函数,您必须使用弧度。

关于c++ - 在 SFML 中移动一个球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35688965/

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