如何防止对象在C++;? 我一直致力于在C++中创建乒乓球游戏,并且我的球和桨碰撞的问题。事实上,当球从顶部撞击桨叶时,球会反弹。但是,如果球从侧面撞到球拍,球就会像被抓住一样沿着球拍滑动,只有到达另一端时才会反弹回来。我曾经尝试过搞乱球拍碰撞盒的尺寸,并使用时钟方法尝试计算球与球拍碰撞的时间,但两种方法都不起作用。有人能帮忙吗
我的主要代码:如何防止对象在C++;? 我一直致力于在C++中创建乒乓球游戏,并且我的球和桨碰撞的问题。事实上,当球从顶部撞击桨叶时,球会反弹。但是,如果球从侧面撞到球拍,球就会像被抓住一样沿着球拍滑动,只有到达另一端时才会反弹回来。我曾经尝试过搞乱球拍碰撞盒的尺寸,并使用时钟方法尝试计算球与球拍碰撞的时间,但两种方法都不起作用。有人能帮忙吗,c++,oop,sfml,C++,Oop,Sfml,我的主要代码: #include "RealBat.h" #include <sstream> #include <string> #include <cstdlib> #include <SFML/Graphics.hpp> #include <iostream> using namespace sf; int windowWidth = 1024; int windowHeight = 868; Bat
#include "RealBat.h"
#include <sstream>
#include <string>
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
int windowWidth = 1024;
int windowHeight = 868;
Bat bat(windowWidth / 2, windowHeight - 50);
sf::Vector2f ballUpdate(sf::Vector2f ballPosition, int windowWidth, int windowHeight, float *velocityX, float *velocityY){
sf::Vector2f ballPosition2 = sf::Vector2f(ballPosition.x, ballPosition.y);
ballPosition2.x += *velocityX;
ballPosition2.y += *velocityY;
if (ballPosition2.x >= windowWidth || ballPosition2.x <= 0) {
*velocityX *= -1.0f;
}
if (ballPosition2.y >= windowHeight || ballPosition2.y <= 0) {
*velocityY *= -1.0f;
}
int ballBottomLeft = ballPosition2.y + 9;
int ballRight = ballPosition2.x + 9;
int batRight = bat.getVector().x + 100;
int score = 0;
if (ballBottomLeft >= bat.getVector().y) {
if (ballPosition2.x >= bat.getVector().x && ballRight <= batRight) {
std::cout << "collisionTracker" << std::endl;
*velocityY *= -1.0f;
score = score + 1;
}
else {
score = 0;
}
return ballPosition2;
}
int main()
{
float velocityX = 0.05;
float velocityY = 0.05;
sf::Vector2f position = sf::Vector2f(10, 10);
RectangleShape ball = RectangleShape();
ball.setSize(sf::Vector2f (10, 10));
RenderWindow window(VideoMode(windowWidth, windowHeight), "ID PONG");
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::Left)) {
bat.moveLeft();
}
else if (Keyboard::isKeyPressed(Keyboard::Right)) {
bat.moveRight();
}
bat.update();
position = ballUpdate(position, windowWidth, windowHeight, &velocityX, &velocityY);
ball.setPosition(position);
window.clear(Color(148, 213, 0, 255));
window.draw(bat.getShape());
window.draw(ball);
window.display();
}
}
最有可能发生的情况是,下一次绕圈时,球仍在球棒的边界内。 想象一下球和球棒,就像这个糟糕的ascii表示(+球棒为0,球为0),球刚刚击中了球棒的右侧,朝下和向左
+--------------+
+ +
+ O
+ +
+--------------+
球的y速度将被反转,下一次迭代将是
+--------------+
+ O+
+ +
+ +
+--------------+
这仍然在球的边界内,因此它的y速度将再次反转
+--------------+
+ +
+ O +
+ +
+--------------+
因此,球将在球棒内上下弹跳,直到击中左手边
+--------------+
+ O O O O O O O+
+O O O O O O O O
+ +
+--------------+
解决这一问题的一种方法是,如果球朝下,只反转Y方向的速度,这样即使球仍在球棒内,球也会继续向上运动。可以加上:
if (*velocityY > 0.0) {
就在collisionTracker的上方
不能呼叫,这样你就不会在向上移动时得分或弹跳。几年前我打乒乓球时也遇到了类似的问题。这是因为我们只检查交点并改变x轴上的速度方向。因此,当它从侧面撞击时,每一帧的速度都在不断反转。当时我解决这个问题的方法是检查我的球中心相对于球拍边界(边)的位置。所以,当球击中任何一边时,我只是简单地反转y轴上的速度方向。这样球就击中了球拍,然后离开了
我很确定有更好的方法,但我当时只是个初学者,这是我能想到的最好的方法
+--------------+
+ O O O O O O O+
+O O O O O O O O
+ +
+--------------+
if (*velocityY > 0.0) {