【发布时间】:2021-06-03 21:11:03
【问题描述】:
我试图让我的移动矩形在与它们碰撞时从窗口边框反弹,但是矩形正在旋转。我不确定如何定义我的碰撞函数,这就是我想出的:
//left border collision
if(rectangle.getPosition().y<0){
rectangle.setPosition(rectangle.getPosition().x,0);
//top border collision
};
if(rectangle.getPosition().x<0){
rectangle.setPosition(0,rectangle.getPosition().y);
};
//right border collision
if(rectangle.getPosition().y+rectangle.getGlobalBounds().width>WINDOW_W){
rectangle.setPosition(rectangle.getPosition().y,WINDOW_W-rectangle.getGlobalBounds().width);
};
//bottom border collision
if(rectangle.getPosition().x+rectangle.getGlobalBounds().height>WINDOW_H){
rectangle.setPosition(WINDOW_H-rectangle.getGlobalBounds().height,rectangle.getPosition().y);
};
代码检测到一些碰撞,但我认为它没有考虑矩形的旋转。
这是我的全部代码:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
// create some shapes
sf::CircleShape circle(100.0);
circle.setPosition(100.0, 300.0);
circle.setFillColor(sf::Color(100, 250, 50));
sf::RectangleShape rectangle(sf::Vector2f(120.0, 60.0));
rectangle.setPosition(500.0, 400.0);
rectangle.setFillColor(sf::Color(100, 50, 250));
sf::ConvexShape triangle;
triangle.setPointCount(3);
triangle.setPoint(0, sf::Vector2f(0.0, 0.0));
triangle.setPoint(1, sf::Vector2f(0.0, 100.0));
triangle.setPoint(2, sf::Vector2f(140.0, 40.0));
triangle.setOutlineColor(sf::Color::Red);
triangle.setOutlineThickness(5);
triangle.setPosition(600.0, 100.0);
//clock function
sf::Clock clock;
//finding screen resolutions for collision
int WINDOW_W = window.getSize().y;
int WINDOW_H = window.getSize().x;
//declaring moving speeds for the rectangle
float rec_vel_x = 50;
float rec_vel_y = 150;
//delcaring speed of rotation of the rectangle
float rec_ang_vel = 10;
// run the program as long as the window is open
while (window.isOpen()) {
//Time elapsed function
sf::Time elapsed = clock.getElapsedTime();
// getting elapsed time as float, needed fro the float function
float el1 = clock.restart().asSeconds();
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event)) {
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
// moving the rectangle per each elapsed second with the preset values
rectangle.move(el1*rec_vel_x,el1*rec_vel_y);
//rotating the rectangle
rectangle.rotate(el1*rec_ang_vel);
// draw everything here...
window.draw(circle);
window.draw(rectangle);
window.draw(triangle);
// end the current frame
window.display();
//game updates
std::cout<<elapsed.asSeconds()<<std::endl;
//extracting values for the collision
//sf::FloatRect rectangle_bounds = rectangle.getGlobalBounds();
//left border collision
if(rectangle.getPosition().y<0){
rectangle.setPosition(rectangle.getPosition().x,0);
//top border collision
};
if(rectangle.getPosition().x<0){
rectangle.setPosition(0,rectangle.getPosition().y);
};
//right border collision
if(rectangle.getPosition().y+rectangle.getGlobalBounds().width>WINDOW_W){
rectangle.setPosition(rectangle.getPosition().y,WINDOW_W-rectangle.getGlobalBounds().width);
};
//bottom border collision
if(rectangle.getPosition().x+rectangle.getGlobalBounds().height>WINDOW_H){
rectangle.setPosition(WINDOW_H-rectangle.getGlobalBounds().height,rectangle.getPosition().y);
};
}
sf::FloatRect rectangle_bounds = rectangle.getGlobalBounds();
std::cout << rectangle_bounds.top << " " << rectangle_bounds.left << " " ;
std::cout << rectangle_bounds.width << " " << rectangle_bounds.height << std::endl;
std::cout <<"wysokosc okna to: " << WINDOW_H << std::endl;
std::cout <<"szerokosc okna to: " << WINDOW_W << std::endl;
return 0;
}
我还没有实现弹跳,现在我希望矩形不要超出窗口
【问题讨论】:
标签: c++ collision-detection sfml collision