SFML community forums

Help => General => Topic started by: AStar on March 08, 2018, 08:38:52 am

Title: SFML collision detection not working..
Post by: AStar on March 08, 2018, 08:38:52 am
The box collision provided by SFML doesn't work. My enemy sprite simply does not trigger the game over. It just goes through the player sprite.

My code: https://pastebin.com/G9BK4Chc (https://pastebin.com/G9BK4Chc)
Title: Re: SFML collision detection not working..
Post by: eXpl0it3r on March 08, 2018, 08:45:15 am
It's more that your logic doesn't work. ;)

You set the rects once and then never change them. You need to update the rects when you change the positions of the sprites.

Additionally, you shouldn't have thst collision check inside your event loop.
Title: Re: SFML collision detection not working..
Post by: AStar on March 08, 2018, 10:38:19 am
How to update the rects? And where should I put the collision checks? (Thanks for the fast reply)
Title: Re: SFML collision detection not working..
Post by: eXpl0it3r on March 08, 2018, 10:51:31 am
The same way you set the rects in the first place. ;)

I usually go with the following procedure: process events, update/physics/collision, draw stuff

So you'd have it between your event loop and the drawing.
This however means that you'll probably want to look into making things independent of your refresh rate.
Title: Re: SFML collision detection not working..
Post by: AStar on March 08, 2018, 11:00:40 am
Just a last request - Can you give me an example code of what do you mean by the above reply?