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

Author Topic: Implementing Collision Detection ?  (Read 3802 times)

0 Members and 1 Guest are viewing this topic.

drinkadriu

  • Newbie
  • *
  • Posts: 5
    • View Profile
Implementing Collision Detection ?
« on: January 23, 2016, 04:09:10 pm »
I've been struggling with collision for days now, i couldn't find a proper tutorial that i could understand, i just want my character to stop when it is touching another object, and please don't tell me a algorithm because i just can't implement algorithms into to code i just can't, here is my game:

#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <Windows.h>

#define Accelerate 9
#define Deccelerate 10

int main()
{
        sf::RenderWindow window(sf::VideoMode(600, 600), "Platformer !");
        sf::Clock deltaClock;
        sf::View view(window.getDefaultView());
        float deltaTime;

        sf::RectangleShape Player;
        sf::RectangleShape BLOK;

        sf::Vector2f Velocity = sf::Vector2f(0, 0);
        sf::Vector2f Position = sf::Vector2f(300, 0);
        sf::Vector2f Size = sf::Vector2f(50, 50);

        bool IsGrounded = false;
        bool UpWalltouching = false;
        bool Walltouching = false;
        bool WalltouchingRight = false;
        bool WalltouchingLeft = false;
        bool Objtouching = false;

        float Gravity = 0;
        float NormalGravity = 700;
        float SlowGravity = 350;
        float DownGravity = 1500;
        float gLimit = 30.f;
        float JumpHeight = -12;
        float Acceleration = 1;
        float accelerator = 1;
        float Speed = 100000;
        float Horizontal = 0;
        float Vertical = 0;

        Player.setSize(Size);
        Player.setFillColor(sf::Color(150, 40, 70, 200));

        BLOK.setSize(Size + Size + Size);
        BLOK.setFillColor(sf::Color(250, 130, 40, 150));
        BLOK.setPosition(sf::Vector2f(200, 500));

        //window.setVerticalSyncEnabled(true);
        window.setFramerateLimit(60);
        // Game Loop
        while (window.isOpen())
        {
                //Input input;
                sf::Event event;
                float Framerate = 1 / deltaClock.getElapsedTime().asSeconds();
                sf::Time dt = deltaClock.restart();
                deltaTime = dt.asSeconds();

                if (deltaTime >= 0.0006f)
                        deltaTime = 0.0006f;
                else if (deltaTime <= 0.0003f)
                        deltaTime = 0.0003f;

                while (window.pollEvent(event))  // Events
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::Resized:
                                window.setView(view = sf::View(sf::FloatRect(0.f, 0.f,
                                        static_cast<float>(window.getSize().x),
                                        static_cast<float>(window.getSize().y))));
                                break;
                        /*case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::Space){
                                        //if (IsGrounded){
                                                Velocity.y = JumpHeight;
                                                printf("JUMPIN");
                                        //}
                                }*/

                                //break;
                        }
                }

                        // Update

                //Player.setFillColor(sf::Color(rand() % 255, rand() % 255, rand() % 255, rand() % 255));

                        // Horizontal Input

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
                                Horizontal = -1;
                                accelerator = 1;
                                if (Acceleration <= 0.35)
                                        Acceleration += Accelerate*deltaTime;
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
                                Horizontal = 1;
                                accelerator = 1;
                                if (Acceleration <= 0.35)
                                        Acceleration += Accelerate*deltaTime;
                        }
                        else {
                                if (Acceleration > 0)
                                        Acceleration -= Deccelerate*deltaTime;
                                else
                                   Acceleration = 0;                   
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
                                Gravity = DownGravity;
                        }
                        else
                                Gravity = NormalGravity;

                        // Wall Collision

                        if (Position.x <= 0){
                                //IsGrounded = true;
                                Position.x = 0;
                                Walltouching = true;
                                WalltouchingLeft = true;
                                Gravity = SlowGravity;
                        }
                        else if (Position.x >= window.getSize().x-Size.x){
                                //IsGrounded = true;
                                Position.x = window.getSize().x - Size.x;
                                Walltouching = true;
                                WalltouchingRight = true;
                                Gravity = SlowGravity;
                        }
                        else {
                                Walltouching = false;
                                WalltouchingRight = false;
                                WalltouchingLeft = false;
                        }

                        // COLLISION

                        //if (Position.x > BLOK.getPosition().x-Size.x && Position.x <= BLOK.getPosition().x+Size.x && Position.y >= BLOK.getPosition().y-Size.y)
                                 //Position.x = BLOK.getPosition().x-Size.x;
                        //if (Position.x <= BLOK.getPosition().x+Size.x && Position.x >= BLOK.getPosition().x-Size.x && Position.y >= BLOK.getPosition().y-Size.y)
                                // Position.x = BLOK.getPosition().x+Size.x;
                        //if (Position.y >= BLOK.getPosition().y-Size.y && Position.x > BLOK.getPosition().x-Size.x && Position.x < BLOK.getPosition().x+Size.x )
                                 //Position.y = BLOK.getPosition().y-Size.y;
                        if (Position.y >= BLOK.getPosition().y-Size.y*3/2)
                                if (Position.x > BLOK.getPosition().x-Size.x*3/2)
                                        if (Position.x < BLOK.getPosition().x+Size.x*3/2)
                                                Position.y = BLOK.getPosition().y-Size.y*3/2;
                        /*
                        if (Position.x < BLOK.getPosition().x + 100 &&
                                Position.x + 50 > BLOK.getPosition().x &&
                                Position.y < BLOK.getPosition().y + 100 &&
                                50 + Position.y > BLOK.getPosition().y){
                                        Velocity = sf::Vector2f(0, 0);
                                         Objtouching = true;
                                printf("\nTOUCHING !");
                        }
                        else
                                 Objtouching = false                    */


                        // Gravity and down collision

                        if (Position.y >= window.getSize().y - Size.y){
                                IsGrounded = true;
                                Velocity.y = 0;
                        }
                        else if (Walltouching){
                                IsGrounded = true;
                                if(Velocity.y <= gLimit)
                                        Velocity.y += Gravity*deltaTime;
                        }
                        else {
                                IsGrounded = false;
                                if(Velocity.y <= gLimit)
                                        Velocity.y += Gravity*deltaTime;
                        }
                        if (Position.y <= (window.getSize().y-window.getSize().y)){
                                UpWalltouching = true;
                                Position.y = (window.getSize().y-window.getSize().y);
                                if(Velocity.y <= gLimit)
                                        Velocity.y += Gravity*deltaTime;
                        }
                        else
                                UpWalltouching = false;

                        // Jumping Input
                       
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
                                if (IsGrounded && !UpWalltouching){
                                        Velocity.y = JumpHeight;
                                }
                        }
                       
                        Velocity.x = ((Horizontal*Speed)*(Acceleration*accelerator))*deltaTime;
                        Player.setPosition(Position);
                        Position += Velocity;
                        printf("\n:   %f", Framerate);
                       
                        // Draw
                        window.clear();
                        window.draw(Player);
                        window.draw(BLOK);
                        window.display();
                }

                return 0;
        }

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: Implementing Collision Detection ?
« Reply #1 on: January 23, 2016, 04:52:28 pm »
The basic collision detection and response is done by using sf::FloatRect:


sf::RectangleShape player;
sf::RectangleShape object;

sf::FloatRect  player_bounds = player.getGlobalBounds();
sf::FloatRect  object_bounds = object.getGlobalBounds();

//  check if two entities collides and make an adequate response:

if (object_bounds.intersects(player_bounds)
{
    // Response
}

// for single pixels, use ".contains" - usually used for checking if mouse is inside object.

sf::Vector2f   mouse_pos;
if (object_bounds.contains(mouse_pos)
{
    // Response;
}

 

Also, keep in mind that bounds of entities always needs to be updated, so after you have moved your entities elsewhere, you need also to call .getGlobalBounds() to it's respective sf::FloatRect again.

drinkadriu

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Implementing Collision Detection ?
« Reply #2 on: January 24, 2016, 01:41:59 pm »
The basic collision detection and response is done by using sf::FloatRect:


sf::RectangleShape player;
sf::RectangleShape object;

sf::FloatRect  player_bounds = player.getGlobalBounds();
sf::FloatRect  object_bounds = object.getGlobalBounds();

//  check if two entities collides and make an adequate response:

if (object_bounds.intersects(player_bounds)
{
    // Response
}

// for single pixels, use ".contains" - usually used for checking if mouse is inside object.

sf::Vector2f   mouse_pos;
if (object_bounds.contains(mouse_pos)
{
    // Response;
}

 

Also, keep in mind that bounds of entities always needs to be updated, so after you have moved your entities elsewhere, you need also to call .getGlobalBounds() to it's respective sf::FloatRect again.

That is not the problem im affraid, i know how to check if the player is colliding with object, what i want to do is stop the player from going through other objects !

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
« Last Edit: March 06, 2020, 11:14:10 pm by eXpl0it3r »
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/