#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <Windows.h>
#include <iostream>
#include <math.h>
#include <cmath>
#include <thread>
#include <random>
double DISTANCE( sf::Vector2f l, sf::Vector2f k);
bool Check_Collision(sf::CircleShape x, sf::CircleShape y);
double angle_opp(sf::Vector2f l, sf::Vector2f k);
double angle(sf::Vector2f l, sf::Vector2f k);
double angle_adj(sf::Vector2f l, sf::Vector2f k);
int main() {
int i = 1;
int x1 = 400;
int y1 = 200;
int x2 = 200;
int y2 = 200;
int rad = 20;
sf::Clock clock;
sf::RenderWindow window(sf::VideoMode(1000,1000),"Sheka Snake");
sf::CircleShape circle1;
sf::CircleShape boost;
circle1.setPosition(x1,y1);
circle1.setRadius(rad);
boost.setPosition(300, 500);
boost.setRadius(10);
boost.setFillColor(sf::Color::Red);
window.clear();
window.draw(circle1);
window.display();
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(false);
sf::Vector2f f1;
sf::Vector2f f2;
bool collid = false;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
float fps = clock.restart().asMilliseconds();
float speed = 0.3f * fps;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
circle1.move(-speed, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
circle1.move(0, -speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
circle1.move(0, speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
circle1.move(speed, 0);
}
if (Check_Collision(circle1, boost) == true) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(0, 1000);// distribution in range [1, 6]
std::cout << dist6(rng)<<std::endl;
boost.setPosition(dist6(rng), dist6(rng));
rad = rad + 30;
circle1.setRadius(rad);
}
sf::Vector2f l;
sf::Vector2f k;
l = circle1.getPosition();
k = boost.getPosition();
window.clear();
window.draw(circle1);
window.draw(boost);
window.display();
}
}
double DISTANCE(sf::Vector2f l, sf::Vector2f k) {
double dy = (l.y - k.y);
double dx = (l.x - k.x);
double d = sqrt(pow(dy, 2) + pow(dx, 2));
return d;
}
double angle_opp(sf::Vector2f l, sf::Vector2f k) {
return (k.y - l.y);
}
double angle_adj(sf::Vector2f l, sf::Vector2f k) {
return (k.x - l.x);
}
double angle(sf::Vector2f l, sf::Vector2f k) {
return atan((k.y - l.y) / (k.x - l.x));
}
bool Check_Collision(sf::CircleShape x,sf::CircleShape y) {
using namespace sf;
Vector2f v1; Vector2f v2;
v1 = x.getPosition();
v2 = y.getPosition();
std::cout << "x:" << v2.x << "y:" << v2.y << std::endl;
double dis =DISTANCE(v1,v2);
double a;
if (dis <= x.getRadius() + y.getRadius()) {
return true;
}
}
你好,我是一名尝试学习 SFML 的初学者,我尝试让一个球在外部与另一个球碰撞,但碰撞无法正常工作,有时球在接触另一个球之前发生碰撞,有时在进入另一个球后发生碰撞。第一次碰撞后的距离似乎计算不正确
根据您提供的代码,我认为您试图解决的问题是由于您正在计算所有这些距离并比较它们和其他东西但不考虑起始(原点)偏移而造成的。当我们通常比较或计算这些物体的距离时,对我们来说物体的起点总是在中心。而在 sfml 中,原点总是在形状的左上角。对于任何非矩形形状,它是围绕形状边缘绘制的矩形(不可见)的左上角。
但是有一个更简单的解决方案。 SFML 已经有一个内置的基本碰撞:
int main()
{
sf::CircleShape circ1;
sf::CircleShape circ2;
cout << circ1.getGlobalBounds().intersects(circ2) << endl;
上述函数获取 circ1 的面积并检查是否有任何其他 sf::Drawable 与该形状接触。希望这有帮助