Программы на C++. Форум разработчиков
-
Anonymous
Столкновение 2D-шаров приводит к перекрытию или слипанию шаров с высокой частотой появления
Сообщение
Anonymous »
Я использую Ball::update() и Ball::checkCollision() для двухмерных столкновений шаров:
Код: Выделить всё
#include
#include
#include
constexpr unsigned int BALL_RADIUS = 15;
constexpr int SCREEN_WIDTH = 800;
constexpr int SCREEN_HEIGHT = 600;
class Ball {
public:
Ball(float posX = 800 / 2, float posY = 600 / 2);
void update(sf::RenderWindow&, std::list&);
sf::Vector3f checkCollision(std::list&);
~Ball() = default;
private:
float BALL_SPEED_X = 0.05;
float BALL_SPEED_Y = 0.05;
sf::CircleShape ballSprite;
sf::Vector2f trueBallSize;
};
Ball::Ball(float posX, float posY) {
ballSprite = sf::CircleShape(BALL_RADIUS);
ballSprite.setFillColor(sf::Color(255, 255, 0, 255));
ballSprite.setOrigin(BALL_RADIUS, BALL_RADIUS);
ballSprite.setPosition(posX, posY);
}
void Ball::update(sf::RenderWindow& window, std::list& balls) {
sf::Vector3f velocity = checkCollision(balls);
if (velocity.z) {
BALL_SPEED_X = velocity.x;
BALL_SPEED_Y = velocity.y;
}
float ballX = ballSprite.getPosition().x;
float ballY = ballSprite.getPosition().y;
ballX += BALL_SPEED_X;
ballY += BALL_SPEED_Y;
if ((ballX - BALL_RADIUS) = SCREEN_WIDTH) BALL_SPEED_X *= -1;
if ((ballY - BALL_RADIUS) = SCREEN_HEIGHT) BALL_SPEED_Y *= -1;
ballSprite.setPosition(sf::Vector2f(ballX, ballY));
window.draw(ballSprite);
}
sf::Vector3f Ball::checkCollision(std::list& balls) {
sf::Vector2f centerA = ballSprite.getPosition();
for (auto& ball : balls) {
// When Checking ball against itself
// Distance become 0, d
Подробнее здесь: [url]https://stackoverflow.com/questions/79881524/2d-ball-collision-causes-balls-to-overlap-or-stick-at-high-spawn-rates[/url]
1770110172
Anonymous
Я использую Ball::update() и Ball::checkCollision() для двухмерных столкновений шаров:
[code]#include
#include
#include
constexpr unsigned int BALL_RADIUS = 15;
constexpr int SCREEN_WIDTH = 800;
constexpr int SCREEN_HEIGHT = 600;
class Ball {
public:
Ball(float posX = 800 / 2, float posY = 600 / 2);
void update(sf::RenderWindow&, std::list&);
sf::Vector3f checkCollision(std::list&);
~Ball() = default;
private:
float BALL_SPEED_X = 0.05;
float BALL_SPEED_Y = 0.05;
sf::CircleShape ballSprite;
sf::Vector2f trueBallSize;
};
Ball::Ball(float posX, float posY) {
ballSprite = sf::CircleShape(BALL_RADIUS);
ballSprite.setFillColor(sf::Color(255, 255, 0, 255));
ballSprite.setOrigin(BALL_RADIUS, BALL_RADIUS);
ballSprite.setPosition(posX, posY);
}
void Ball::update(sf::RenderWindow& window, std::list& balls) {
sf::Vector3f velocity = checkCollision(balls);
if (velocity.z) {
BALL_SPEED_X = velocity.x;
BALL_SPEED_Y = velocity.y;
}
float ballX = ballSprite.getPosition().x;
float ballY = ballSprite.getPosition().y;
ballX += BALL_SPEED_X;
ballY += BALL_SPEED_Y;
if ((ballX - BALL_RADIUS) = SCREEN_WIDTH) BALL_SPEED_X *= -1;
if ((ballY - BALL_RADIUS) = SCREEN_HEIGHT) BALL_SPEED_Y *= -1;
ballSprite.setPosition(sf::Vector2f(ballX, ballY));
window.draw(ballSprite);
}
sf::Vector3f Ball::checkCollision(std::list& balls) {
sf::Vector2f centerA = ballSprite.getPosition();
for (auto& ball : balls) {
// When Checking ball against itself
// Distance become 0, d
Подробнее здесь: [url]https://stackoverflow.com/questions/79881524/2d-ball-collision-causes-balls-to-overlap-or-stick-at-high-spawn-rates[/url]