Sfml 碰撞:当玩家撞到一个方块时,他就停止移动
Sfml collision: when the player hits a square, he just stops moving
我决定用c++和sfml做一个碰撞测试。但是当玩家击中正方形时,你就不能再移动了。我对如何进行碰撞没有任何问题,但是当我真正发生碰撞时该怎么办。
这是我的代码:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <thread>
using namespace std;
using namespace sf;
RenderWindow window(VideoMode(500, 500), "SFML");
RectangleShape r1;
RectangleShape r2;
void collision(){
r1.setSize(Vector2f(50.0, 50.0));
r2.setSize(Vector2f(50.0, 50.0));
r1.setPosition(20, 200);
r2.setPosition(420, 200);
r1.setFillColor(Color::Red);
r2.setFillColor(Color::Blue);
}
int main(){
collision();
while (window.isOpen()){
Event event;
while (window.pollEvent(event)){
if (event.type == Event::Closed){
window.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::W))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.0, -0.05);
if (Keyboard::isKeyPressed(Keyboard::A))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(-0.05, 0.0);
if (Keyboard::isKeyPressed(Keyboard::S))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.0, 0.05);
if (Keyboard::isKeyPressed(Keyboard::D))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.05, 0.0);
window.draw(r2);
window.draw(r1);
window.display();
window.clear();
}
}
再一次,我想知道如何正确移动你的播放器并使其在你无法进入物体时。
提前致谢!
PS。请不要告诉我 "uh, your code is so horribleee. your bracketss suck ablahb..." 我知道。有点乱好吗?
谢谢。
你的问题是,你只允许你的玩家移动,如果它的边界不与第二个对象的边界相交,所以当你第一次与对象碰撞时,你不能再移动到它的边界之外.
您需要做的是在玩家与物体发生碰撞时将其移回。
例如:
if (Keyboard::isKeyPressed(Keyboard::W)) {
sf::FloatRect& intersection;
if (r1.getGlobalBounds().intersects(r2.getGlobalBounds(), intersection) {
r1.move(0.0, intersection.height);
}
else {
r1.move(0.0, -0.05);
}
}
intersects 方法允许您传入对 sf::Rect 的引用,如果玩家的边界与第二个对象的边界相交,交点将存储在 rect 中。
这允许您将玩家向后移动 space 所需的量,这样对象将不再相交并且玩家可以再次移动。
Jack Edwards 回答的问题是交点控制在移动命令之前。但首先精灵必须移动,然后是交点控制。如果有交集,精灵必须向后移动。
if (Keyboard::isKeyPressed(Keyboard::W)){
r1.move(0.0, -0.05);
if (r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.0, +0.05);}
我决定用c++和sfml做一个碰撞测试。但是当玩家击中正方形时,你就不能再移动了。我对如何进行碰撞没有任何问题,但是当我真正发生碰撞时该怎么办。
这是我的代码:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <thread>
using namespace std;
using namespace sf;
RenderWindow window(VideoMode(500, 500), "SFML");
RectangleShape r1;
RectangleShape r2;
void collision(){
r1.setSize(Vector2f(50.0, 50.0));
r2.setSize(Vector2f(50.0, 50.0));
r1.setPosition(20, 200);
r2.setPosition(420, 200);
r1.setFillColor(Color::Red);
r2.setFillColor(Color::Blue);
}
int main(){
collision();
while (window.isOpen()){
Event event;
while (window.pollEvent(event)){
if (event.type == Event::Closed){
window.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::W))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.0, -0.05);
if (Keyboard::isKeyPressed(Keyboard::A))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(-0.05, 0.0);
if (Keyboard::isKeyPressed(Keyboard::S))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.0, 0.05);
if (Keyboard::isKeyPressed(Keyboard::D))
if (!r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.05, 0.0);
window.draw(r2);
window.draw(r1);
window.display();
window.clear();
}
}
再一次,我想知道如何正确移动你的播放器并使其在你无法进入物体时。
提前致谢!
PS。请不要告诉我 "uh, your code is so horribleee. your bracketss suck ablahb..." 我知道。有点乱好吗?
谢谢。
你的问题是,你只允许你的玩家移动,如果它的边界不与第二个对象的边界相交,所以当你第一次与对象碰撞时,你不能再移动到它的边界之外.
您需要做的是在玩家与物体发生碰撞时将其移回。
例如:
if (Keyboard::isKeyPressed(Keyboard::W)) {
sf::FloatRect& intersection;
if (r1.getGlobalBounds().intersects(r2.getGlobalBounds(), intersection) {
r1.move(0.0, intersection.height);
}
else {
r1.move(0.0, -0.05);
}
}
intersects 方法允许您传入对 sf::Rect 的引用,如果玩家的边界与第二个对象的边界相交,交点将存储在 rect 中。
这允许您将玩家向后移动 space 所需的量,这样对象将不再相交并且玩家可以再次移动。
Jack Edwards 回答的问题是交点控制在移动命令之前。但首先精灵必须移动,然后是交点控制。如果有交集,精灵必须向后移动。
if (Keyboard::isKeyPressed(Keyboard::W)){
r1.move(0.0, -0.05);
if (r1.getGlobalBounds().intersects(r2.getGlobalBounds()))
r1.move(0.0, +0.05);}