SFML реализация коллизии
Всем добрый день!
Решил немного попрактиковаться в ООП в C++. Решил написать небольшую игру с помощью библиотеки SFML. Но возникла проблема с реализацией коллизии (гг проходит сквозь статичные объекты). Может кто помочь с её реализацией?
Код программы:
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
#include "LifeBar.h"
using namespace sf;
using namespace std;
RenderWindow window(sf::VideoMode(640, 480), "Straus"); //окно
class Obj {
public:
virtual void draw() = 0;
};
class Persona_5 :public Obj {
private:
Texture Pesn_Tex;
Sprite Pesn_Spr;
Clock Pesn_Clock;
float time;
float CurrentFrame;
public:
//Конструктор объекта
Persona_5(string File_Path);
void Move_From_KeyBoard();
void draw();
};
class Static_obj : public Obj {
private:
Texture obj_Tex;
Sprite obj_Spr;
public:
Static_obj(string File_Path, int x, int y, int l, int t, int w, int h);
void draw();
};
void Static_obj::draw() { // отрисовка статических объектов
//window.clear();
window.draw(obj_Spr);
}
Static_obj::Static_obj(string File_Path, int x, int y, int l, int t, int w, int h) {
obj_Tex.loadFromFile(File_Path);
obj_Spr.setTexture(obj_Tex);
obj_Spr.setTextureRect(IntRect(l, t, w, h));
obj_Spr.setPosition(x, y);
}
void Persona_5::draw() { //отрисовка гг
//window.clear();
Move_From_KeyBoard();
window.draw(Pesn_Spr);
}
Persona_5::Persona_5(string File_Path) {
Pesn_Tex.loadFromFile(File_Path);
Pesn_Spr.setTexture(Pesn_Tex);
Pesn_Spr.setTextureRect(IntRect(0, 192, 48, 48));
Pesn_Spr.setPosition(50, 50);
time = 0;
CurrentFrame = 0;
window.clear();
window.draw(Pesn_Spr);
window.display();
}
void Persona_5::Move_From_KeyBoard() { //управление
time = Pesn_Clock.getElapsedTime().asMicroseconds();
time = time / 800;
Pesn_Clock.restart();
if (Keyboard::isKeyPressed(Keyboard::W)) {
CurrentFrame += 0.005 * time;
if (CurrentFrame > 3) CurrentFrame -= 3;
Pesn_Spr.move(0, 0.1 * time);
Pesn_Spr.setTextureRect(IntRect(48 * int(CurrentFrame), 0, 48, 48));
}
else if (Keyboard::isKeyPressed(Keyboard::S)) {
CurrentFrame += 0.005 * time;
if (CurrentFrame > 3) CurrentFrame -= 3;
Pesn_Spr.move(0, -0.1 * time);
Pesn_Spr.setTextureRect(IntRect(48 * int(CurrentFrame), 144, 48, 48));
}
else if (Keyboard::isKeyPressed(Keyboard::A)) {
CurrentFrame += 0.005 * time;
if (CurrentFrame > 3) CurrentFrame -= 3;
Pesn_Spr.move(-0.1 * time, 0);
Pesn_Spr.setTextureRect(IntRect(48 * int(CurrentFrame), 48, 48, 48));
}
else if (Keyboard::isKeyPressed(Keyboard::D)) {
CurrentFrame += 0.005 * time;
if (CurrentFrame > 3) CurrentFrame -= 3;
Pesn_Spr.move(0.1 * time, 0);
Pesn_Spr.setTextureRect(IntRect(48 * int(CurrentFrame), 48 * 2, 48, 48));
}
else {
return;
}
//window.clear();
//window.draw(Pesn_Spr);
//window.display();
}
int main()
{
Persona_5 My_Obj("images/straus.png");
Static_obj mo("images/forest.png", 100, 100, 290, 193, 90, 90); //1 2 - положение 3 4 - объект 5 6 - размер
LifeBar lifeBarPlayer;//экземпляр класса полоски здоровья
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
lifeBarPlayer.update(100);//сюда передаем значение, которое надо нарисовать. Можно передать здоровья игрока тогда будет lifeBarPlayer.update(player.getHealth());
window.clear();
lifeBarPlayer.draw(window);//рисуем полоску здоровья
My_Obj.draw();
mo.draw();
window.display();
}
return 0;
}
Ответы (1 шт):
Автор решения: kolyshek
→ Ссылка
- obj1 - объект который пересечётся.
- obj2 - объект с которым пересечётся.
obj1.getGlobalBounds().intersects(obj2.getGlobalBounds());
Простейший вариант.
