Hello, can some help me to set up collision for the player object, i can make collision work in the game class, but cant figure out how i have to do it correctly in the player class. m_PlyersBody can be used for checking collision, but it does nothing if i set up collision on the sides of the screen, so i decided to make a pointer to the m_player, with Game::getPlayer function. i made it auto just for simplicity to store data of m_player in the m_player in the player.cpp. But it does not let me use any functions or methods to create collision function. What i am doing wrong? i jsut want to make it collide on the sides with if statement smth like this:
if(m_player.getPostion().x >608)
{
m_player.move(-2,0);
}
full code here:
main.cpp
#include <SFML/Graphics.hpp>
#include "Window.h"
#include "Game.h"
void main(int argc, void** argv[]) {
// Program entry point.
Game game; // Creating our game object.
while (!game.GetWindow()->IsDone()) {
// Game loop.
game.HandleInput();
game.Update();
game.Render();
game.RestartClock();
}
}
//window.h
#pragma once
#include <string>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class Window
{
public:
Window();
Window(const std::string& title, const sf::Vector2u& size);
~Window();
void BeginDraw();
void EndDraw();
void Update();
bool IsDone();
bool IsFullscreen();
sf::RenderWindow* GetRenderWindow();
sf::Vector2u GetWindowSize();
void ToggleFullscreen();
void Draw(sf::Drawable& l_drawable);
private:
void Setup(const std::string title, const sf::Vector2u& size);
void Create();
void Destroy();
sf::RenderWindow m_window;
sf::Vector2u m_windowSize;
std::string m_windowTitle;
bool m_isDone;
bool m_isFullscreen;
};
window.cpp
#include "Window.h"
Window::Window()
{
Setup("Window", sf::Vector2u(640, 480));
}
Window::Window(const std::string& title, const sf::Vector2u& size)
{
Setup(title, size);
}
Window::~Window() { Destroy(); }
void Window::Setup(const std::string title, const sf::Vector2u& size)
{
m_windowTitle = title;
m_windowSize = size;
m_isFullscreen = false;
m_isDone = false;
m_window.setFramerateLimit(60);
Create();
}
void Window::Create() {
auto style = (m_isFullscreen ? sf::Style::Fullscreen
: sf::Style::Default);
m_window.create({ m_windowSize.x, m_windowSize.y, 32 },
m_windowTitle, style);
}
void Window::Destroy() {
m_window.close();
}
void Window::BeginDraw() { m_window.clear(sf::Color::Black); }
void Window::EndDraw() { m_window.display(); }
bool Window::IsDone() { return m_isDone; }
bool Window::IsFullscreen() { return m_isFullscreen; }
void Window::Draw(sf::Drawable& l_drawable) {
m_window.draw(l_drawable);
}
sf::Vector2u Window::GetWindowSize() { return m_windowSize; }
void Window::ToggleFullscreen() {
m_isFullscreen = !m_isFullscreen;
Destroy();
Create();
}
void Window::Update() {
sf::Event event;
while (m_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) { m_isDone = true; }
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::F5) { ToggleFullscreen(); }
}
}
sf::RenderWindow* Window::GetRenderWindow()
{
return &m_window;
}
//game.h
#pragma once
#include "Window.h"
#include "Player.h"
//#include "World.h"
//#include "Textbox.h"
class Game
{
public:
Game();
~Game();
void HandleInput();
void Update();
void Render();
sf::Time GetElapsed();
void RestartClock();
Window* GetWindow();
Player* GetPlayer();
private:
Window m_window;
Player m_player;
sf::Clock m_clock;
float m_elapsed;
//World m_world;
//Textbox m_textbox;
};
//game.cpp
#include "Game.h"
Game::Game() : m_window("Snake", sf::Vector2u(800, 600)), m_snake(m_world.GetBlockSize(), &m_textbox),m_world(sf::Vector2u(800, 600))
{
m_clock.restart();
srand(time(nullptr));
m_textbox.Setup(5, 14, 350, sf::Vector2f(225, 0));
m_elapsed = 0.0f;
m_textbox.Add("Seeded random number generator with: " + std::to_string(time(nullptr)));
}
Game::~Game() {}
sf::Time Game::GetElapsed() { return m_clock.getElapsedTime(); }
void Game::RestartClock() { m_elapsed += m_clock.restart().asSeconds(); }
Window* Game::GetWindow() { return &m_window; }
void Game::HandleInput() {
// Input handling.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)
&& m_snake.GetPhysicalDirection() != Direction::Down) {
m_snake.SetDirection(Direction::Up);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)
&& m_snake.GetPhysicalDirection() != Direction::Up) {
m_snake.SetDirection(Direction::Down);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)
&& m_snake.GetPhysicalDirection() != Direction::Right) {
m_snake.SetDirection(Direction::Left);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)
&& m_snake.GetPhysicalDirection() != Direction::Left) {
m_snake.SetDirection(Direction::Right);
}
}
void Game::Update() {
m_window.Update();
float timestep = 1.0f / m_snake.GetSpeed();
if (m_elapsed >= timestep) {
m_snake.Tick();
m_world.Update(m_snake);
m_elapsed -= timestep;
if (m_snake.HasLost()) {
m_textbox.Add("GAME OVER! Score: "
+ std::to_string((long long)m_snake.GetScore()));
m_snake.Reset();
}
}
}
void Game::Render() {
m_window.BeginDraw();
// Render here.
m_world.Render(*m_window.GetRenderWindow());
m_snake.Render(*m_window.GetRenderWindow());
m_textbox.Render(*m_window.GetRenderWindow());
m_window.EndDraw();
}
//player.h
#pragma once
#include "SFML/Graphics.hpp"
#include "Window.h"
enum{moveLeft, moveRight};
class Player
{
public:
Player();
void Position(sf::Vector2f xy);
void playerUpdate();
void tick();
void move();
void checkCollision();
void draw(sf::RenderWindow& l_window);
//setters and getters
void setHasLost(bool loosing);
bool getHasLost();
int getPositionX();
void setPositionX(int x);
private:
sf::Vector2f xyPosition;
int playersSpeed;
int lives;
int score;
bool hasLost;
sf::RectangleShape m_PlayersBody;
};
player.cpp
#include "Game.h"
Player::Player()
{
Player::hasLost = false;
Player::lives = 10;
Player::playersSpeed = 2;
Player::Position(sf::Vector2f(320, 440));
m_PlayersBody.setSize(sf::Vector2f(32,32));
}
void Player::playerUpdate()
{
tick();
}
void Player::Position(sf::Vector2f xy)
{
xyPosition = xy;
}
void Player::tick()
{
move();
checkCollision();
}
void Player::move()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
xyPosition.x -= playersSpeed;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
xyPosition.x += playersSpeed;
}
}
void Player::checkCollision()
{
auto m_player = Game::GetPlayer;
if (m_PlayersBody.getPosition().x >= 608)
{
//m_player PROBLEM IN HERE DOES NOT LET USE FUNCTIONS/METHODS
std::cout << m_PlayersBody.getPosition().x << std::endl;
}
else if (m_PlayersBody.getPosition().x <= 0)
{
std::cout << m_PlayersBody.getPosition().x << std::endl;
}
}
void Player::draw(sf::RenderWindow& l_window)
{
m_PlayersBody.setFillColor(sf::Color::Blue);
m_PlayersBody.setPosition(xyPosition);
l_window.draw(m_PlayersBody);
}
//setters && getters
void Player::setHasLost(bool loosing)
{
hasLost = loosing;
}
bool Player::getHasLost()
{
return hasLost;
}
void Player::setPositionX(int x)
{
xyPosition.x = x;
}
int Player::getPositionX()
{
return xyPosition.x;
}
thanks if some1 will find a time to check it out