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

Author Topic: Collision detection sfml 2.0  (Read 12802 times)

0 Members and 2 Guests are viewing this topic.

Christopher Jozwiak

  • Newbie
  • *
  • Posts: 48
    • View Profile
    • Blog
    • Email
Collision detection sfml 2.0
« on: December 13, 2012, 03:48:20 am »
How would i perform a simple box collision detection in sfml?
Noob C++ Programmer.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Collision detection sfml 2.0
« Reply #1 on: December 13, 2012, 04:14:05 am »
The sf::Rect class has the methods Rect.contains and Rect.intersects, so for basic collision detection I would say that's a good place to start.
DSFML - SFML for the D Programming Language.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Collision detection sfml 2.0
« Reply #2 on: December 13, 2012, 10:11:56 am »
Maybe my small example could give you some insights. ;)
But keep in mind that it's not finished at all.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Christopher Jozwiak

  • Newbie
  • *
  • Posts: 48
    • View Profile
    • Blog
    • Email
Re: Collision detection sfml 2.0
« Reply #3 on: December 14, 2012, 01:13:26 am »
Okay what I'm trying to do is make a classic Snake game.  I think i would need to use something like object.GetGlobalBounds().contains().  Then I'm unsure what to put in the contains function.  I put in a number and a point?  What would the point be?
Noob C++ Programmer.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Collision detection sfml 2.0
« Reply #4 on: December 14, 2012, 02:13:04 am »
You should start using the documentation, which explains what a class/function does.

The contains() function either takes 2 floats or a sf::Vector2f and simply checks if a point (defined by the 2 floats or the sf::Vector2f) is within the sf::Rect that is returned by getGlobalBounds(), which essentially the bounding box of the sprite, describe in the 'world' coordinates.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Christopher Jozwiak

  • Newbie
  • *
  • Posts: 48
    • View Profile
    • Blog
    • Email
Re: Collision detection sfml 2.0
« Reply #5 on: December 14, 2012, 03:04:59 am »
Okay so I tried
if (object.getGlobalBounds().intersects( player.getGlobalBounds())){
            
            touching = true;
         }
         if (touching == true){
            player.move(0,0);
         }


:-\
Noob C++ Programmer.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Collision detection sfml 2.0
« Reply #6 on: December 14, 2012, 03:51:07 am »
move(0,0) moves the object 0 pixels to the right and 0 pixels down = nothing.
Back to C++ gamedev with SFML in May 2023

Christopher Jozwiak

  • Newbie
  • *
  • Posts: 48
    • View Profile
    • Blog
    • Email
Re: Collision detection sfml 2.0
« Reply #7 on: December 14, 2012, 03:51:32 am »
Never mind.  Made a statement print out on the command line if the collision was true.  I just think my player.move method needs to be changed.  When the event polls i think it overruns the collision and the player keeps moving despite the player.move(0,0).
Noob C++ Programmer.

kryton9

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Collision detection sfml 2.0
« Reply #8 on: March 18, 2013, 05:23:12 am »
Maybe my small example could give you some insights. ;)
But keep in mind that it's not finished at all.

Thanks, that is a great example to study!

 

anything