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

Author Topic: Basic collision detection  (Read 2933 times)

0 Members and 1 Guest are viewing this topic.

idontmakesense

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Basic collision detection
« on: July 17, 2013, 12:15:52 pm »
I'm really sorry of asking such a basic question, but I tried fixing it with searches and the sfml documents and couldn't get it. Also, it's my first sfml/graphics program.

All i want to is for my rectangle to not go out of the screen.

Here's my code.   

#include <SFML/Graphics.hpp>

int main()
{
   float x= 50, y= 50, width= 20, height= 10, xVid= 800, yVid=600;
   float mov= 0.5;
   
    sf::RenderWindow window(sf::VideoMode(xVid, yVid), "SFML works!");
   
    sf::RectangleShape rectangle(sf::Vector2f(width, height));
    rectangle.setFillColor(sf::Color::White);
    rectangle.setPosition(x, y);

    while (window.isOpen())
    {   
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
           
            if(event.type== sf::Event::KeyReleased)
            {
               if(event.key.code== sf::Keyboard:: Escape)
                  window.close();
            }
        }      
       
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
           
           if(x<0)
              x= 0;
           else
              rectangle.move(-mov,0);
        }   
           
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
           if(x+width>600)
              x= 600- width;
           else
              rectangle.move(mov,0);
        }
       
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
           if(y<0)
              y= 0;
           else
              rectangle.move(0, -mov);
        }
       
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
           if(y+height> 800)
              y= 800- height;
           else
              rectangle.move(0, mov);
        }
       
        window.clear();
        window.draw(rectangle);
        window.display();
    }
    return 0;
}

gostron

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Basic collision detection
« Reply #1 on: July 17, 2013, 12:40:56 pm »
I see you are changing values of x and y inside your loop but never actually use them inside. Maybe that's related.

What you can do is have x and y actualized in each loop (with boundaries issues of course) and then call your rectangle.setPosition(x,y) at the end of each loop. Because you are currently continuing to play with x and y but only call move with the amount of movement, so x and y are never read.

Hope this helps.

idontmakesense

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Basic collision detection
« Reply #2 on: July 17, 2013, 01:38:00 pm »
if(x<0)
           {
              x= 0;
              rectangle.setPosition(x, y);
           }


I changed it into the above, still no difference.

datMoka

  • Newbie
  • *
  • Posts: 48
  • Wooo!
    • View Profile
Re: Basic collision detection
« Reply #3 on: July 17, 2013, 02:09:47 pm »
Code: [Select]
x = rectangle.getPosition().x;
        y = rectangle.getPosition().y;

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {

           if(x<0)
              rectangle.move(mov,0);
           else
              rectangle.move(-mov,0);


        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
           if(x + width > xVid)
              rectangle.move(-mov,0);
           else
              rectangle.move(mov,0);
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
           if(y<0)
              rectangle.move(0, mov);
           else
              rectangle.move(0, -mov);
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
           if(y + height> yVid)
              rectangle.move(0, -mov);
           else
              rectangle.move(0, mov);
        }

First off, you weren't manipulating the variable that actually moves the rectangle and secondly you didn't get the x and y variables to update to the current position of the rectangle (the two lines at the top). Just put this into your code and you're good to go :)


If I were you, I'd create a sf::Vector2f playerVelocity variable then adjust the x and y of that then at the end of your code have one single 'rectangle.move()'.

With this, you'll adjust the velocity depending on which key you press and you can even have it so that the velocity builds up to it's maximum, which gives a more natural look:

Code: [Select]
if(keyPressed)
{
    if(velocity < 50) //building up to max speed
    {
        velocity++;
        //or if you want the player to get to max speed faster:
        velocity += 5;
    }
    else if(velocity > 50) //at max speed
    {
        velocity = 50;
        //stops the velocity going over 50
    }
}
else if(!keyPressed) //if key isn't pressed
{
    if(velocity > 0) //if the player doesn't want to move, slow them down gradually
    {
        velocity--;
        //or
        velocity -= 5;
    }
}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Basic collision detection
« Reply #4 on: July 17, 2013, 02:15:42 pm »
x is either 0, 800-50 or 50.
y is either 0, 600-50 or 50.
They should correspond to your rectangle x and y position.

        x = rectangle.getPosition().x;
        y = rectangle.getPosition().y;

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
           
           if(x - mov < 0)
              x = 0;
           else
              x = x - mov;
        }
//... etc. for right up down

        rectangle.setPosition(x, y);


When you post c++ code on the forums, surround it with [ code=cpp][ /code]
« Last Edit: July 17, 2013, 02:17:46 pm by G. »

idontmakesense

  • Newbie
  • *
  • Posts: 13
    • View Profile
    • Email
Re: Basic collision detection
« Reply #5 on: July 17, 2013, 02:19:02 pm »
Thanks a lot. It worked. :)