Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Need help with collision  (Read 861 times)

0 Members and 1 Guest are viewing this topic.

MrMode

  • Newbie
  • *
  • Posts: 8
    • View Profile
Need help with collision
« on: November 26, 2018, 03:45:47 pm »
Hello, just started coding with SFML and got stuck with collision, hwo does it needed to be done properly?
Here is my code:
main.cpp
#include "SFML/Graphics.hpp"
#include <iostream>
#include "playerClass.h"
#include "platformClass.h"

int windowWidth = 800;
int windowHeight = 600;


int main()
{
        sf::RenderWindow  window(sf::VideoMode(windowWidth, windowHeight), "SFML");
        float startingX = 10, startingY = 10;
        bool playerLeft, playerRight, playerUp, playerDown, isCollidingLeft = false, isCollidingRight = false, isCollidingUp = false , isCollidingDown = false;
        PlayerClass playerObject(startingX,startingY);
       
        sf::Font consolasFont;
        consolasFont.loadFromFile("data/fonts/consola.ttf");
        sf::Text helloText("Hello knights", consolasFont,50);

        //loading kight texture
        sf::Texture knightTexture;
        knightTexture.loadFromFile("knight.png");
        sf::Sprite knightObj(knightTexture);
        //cube
        sf::RectangleShape cube1;
        cube1.setSize(sf::Vector2f(100, 100));
        cube1.setFillColor(sf::Color(2500, 0, 0, 255));
        cube1.setPosition(200, 200);

        while (window.isOpen())
        {

                sf::Event evnt;
                while (window.pollEvent(evnt))
                {
                        if (evnt.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        {
                                window.close();
                        }
                        //if colliding stop moving from that side
                        if (knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                        {
                                if (knightObj.getPosition().x > cube1.getPosition().x && knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                                {
                                        isCollidingLeft = true;
                                        isCollidingRight = false;
                                        isCollidingUp = false;
                                        isCollidingDown = false;
                                        std::cout << "Player is collding from the left" << std::endl;
                                }
                                if (knightObj.getPosition().x < cube1.getPosition().x && knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                                {
                                        isCollidingLeft = false;
                                        isCollidingRight = true;
                                        isCollidingUp = false;
                                        isCollidingDown = false;
                                        std::cout << "Player is collding from the right" << std::endl;
                                }
                                if (knightObj.getPosition().y < cube1.getPosition().y && knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                                {
                                        isCollidingLeft = false;
                                        isCollidingRight = false;
                                        isCollidingUp = true;
                                        isCollidingDown = false;
                                        std::cout << "Player is collding from the right" << std::endl;
                                }
                                if (knightObj.getPosition().y < cube1.getPosition().y && knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                                {
                                        isCollidingLeft = false;
                                        isCollidingRight = false;
                                        isCollidingUp = false;
                                        isCollidingDown = true;
                                        std::cout << "Player is collding from the right" << std::endl;
                                }
                        }
                        else if(!knightObj.getGlobalBounds().intersects(cube1.getGlobalBounds()))
                        {
                                isCollidingLeft = false;
                                isCollidingRight = false;
                                isCollidingUp = false;
                                isCollidingDown = false;
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                                playerLeft = true;
                        }
                        else
                        {
                                playerLeft = false;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                playerRight = true;
                        }
                        else
                        {
                                playerRight = false;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                                playerUp = true;
                        }
                        else
                        {
                                playerUp = false;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                                playerDown = true;
                        }
                        else
                        {
                                playerDown = false;
                        }
                        playerObject.update(playerUp, playerDown, playerRight, playerLeft, isCollidingLeft, isCollidingRight, isCollidingUp, isCollidingDown);
                }
               
                window.clear();
                //window.draw(helloText);
                window.draw(knightObj);
                window.draw(cube1);
                knightObj.move(sf::Vector2f(playerObject.getxVel(), playerObject.getyVel()));
                window.display();
       
        }
       
        return 0;
}
playerClass.cpp
#include "playerClass.h"
#include "platformClass.h"
#include <iostream>

PlayerClass::PlayerClass(float x, float y)
{
        PlayerClass::setXPos(x);
        PlayerClass::setYPos(y);
}
void PlayerClass::update(bool playerUp, bool playerDown, bool playerRight, bool playerLeft, bool isCollidingLeft, bool isCollidingRight, bool isCollidingUp, bool isCollidingDown)
{
        if (playerDown == false && playerUp == false && playerRight == false && playerLeft == false)
        {
                PlayerClass::setyVel(0);
                PlayerClass::setxVel(0);
                std::cout << "Player stands still" << std::endl;
        }
        if (playerUp)
        {
                if (isCollidingUp == false)
                {
                        PlayerClass::setyVel(-0.5);
                        PlayerClass::setFaceUp(true);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves up" << std::endl;
                }
                else
                {
                        std::cout << "Player cant move up" << std::endl;
                }

        }
        if (playerDown)
        {
                if (!isCollidingDown)
                {
                        PlayerClass::setyVel(0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(true);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves down" << std::endl;
                }
                else
                {

                }
               
        }
        if (playerRight)
        {
                if (!isCollidingRight)
                {
                        PlayerClass::setxVel(0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(true);
                        std::cout << "Player moves right" << std::endl;
                }
                else
                {
                        std::cout << "player cant move right object in the way" << std::endl;
                }
       
        }
        if (playerLeft)
        {
                if (!isCollidingLeft)
                {
                        PlayerClass::setxVel(-0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(true);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves left" << std::endl;
                }
                else
                {
                        std::cout << "player cant move left object in the way" << std::endl;
                }
        }
        PlayerClass::setYPos(PlayerClass::getyVel());
        std::cout << PlayerClass::getYPos() << std::endl;

}
playerClass.h
#include "playerClass.h"
#include "platformClass.h"
#include <iostream>

PlayerClass::PlayerClass(float x, float y)
{
        PlayerClass::setXPos(x);
        PlayerClass::setYPos(y);
}
void PlayerClass::update(bool playerUp, bool playerDown, bool playerRight, bool playerLeft, bool isCollidingLeft, bool isCollidingRight, bool isCollidingUp, bool isCollidingDown)
{
        if (playerDown == false && playerUp == false && playerRight == false && playerLeft == false)
        {
                PlayerClass::setyVel(0);
                PlayerClass::setxVel(0);
                std::cout << "Player stands still" << std::endl;
        }
        if (playerUp)
        {
                if (isCollidingUp == false)
                {
                        PlayerClass::setyVel(-0.5);
                        PlayerClass::setFaceUp(true);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves up" << std::endl;
                }
                else
                {
                        std::cout << "Player cant move up" << std::endl;
                }

        }
        if (playerDown)
        {
                if (!isCollidingDown)
                {
                        PlayerClass::setyVel(0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(true);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves down" << std::endl;
                }
                else
                {

                }
               
        }
        if (playerRight)
        {
                if (!isCollidingRight)
                {
                        PlayerClass::setxVel(0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(false);
                        PlayerClass::setFaceRight(true);
                        std::cout << "Player moves right" << std::endl;
                }
                else
                {
                        std::cout << "player cant move right object in the way" << std::endl;
                }
       
        }
        if (playerLeft)
        {
                if (!isCollidingLeft)
                {
                        PlayerClass::setxVel(-0.5);
                        PlayerClass::setFaceUp(false);
                        PlayerClass::setFaceDown(false);
                        PlayerClass::setFaceLeft(true);
                        PlayerClass::setFaceRight(false);
                        std::cout << "Player moves left" << std::endl;
                }
                else
                {
                        std::cout << "player cant move left object in the way" << std::endl;
                }
        }
        PlayerClass::setYPos(PlayerClass::getyVel());
        std::cout << PlayerClass::getYPos() << std::endl;

}
platformClass.h
#pragma once
#include "SFML/Graphics.hpp"

class Platform
{
private:
        float xPos;
        float yPos;
        float width;
        float height;
public:
        Platform();
        //setters
               

        //getters
        float getXPos()
        {
                return xPos;
        }
};
platform.cpp
#include "platformClass.h"

Platform::Platform()
{
        xPos = 100;
        yPos = 100;
        width = 100;
        height = 100;
}

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Need help with collision
« Reply #1 on: November 26, 2018, 05:19:42 pm »
With what are you stuck?
What does your program do that you don't expect it to do?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*