在 SFML 中移动球
Moving a ball in SFML
我正在使用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 函数。
相关文章:
- SFML 向下移动时如何围绕屏幕中心旋转?
- SFML 在屏幕上移动矩形时滞后/小卡顿
- 移动后未绘制 SFML 形状
- SFML 视图:设置中心与移动。快把我逼疯了
- SFML无法正确移动精灵
- 带有 SFML 的 c++ 中的过程无法正常工作,移动算法
- 您如何使形状沿着更BE的曲线SFML移动
- 当在SFML C 中移动一个圆圈时,球后面会有口吃的束缚小故障
- 精灵不在SFML中移动
- 如何使用SFML鼠标移动分别移动多个图像
- 我的 SFML/C++ 程序按预期工作,但如何使移动变慢
- 使对象在 C++-SFML 中平滑移动
- 使投射物移动与时间相关SFML C++
- 将聚焦的精灵逐渐移动到目标位置(C++和SFML)
- SFML中移动位置的居中视图
- 如何在 SFML 中对角线移动对象
- C++ SFML 鼠标移动
- 使用另一个类 SFML c++ 中的函数时,子画面不移动
- 在 SFML 中移动球
- SFML在另一个类中绘制移动精灵