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

Author Topic: SFML collision detection not working..  (Read 1260 times)

0 Members and 1 Guest are viewing this topic.

AStar

  • Guest
SFML collision detection not working..
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML collision detection not working..
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AStar

  • Guest
Re: SFML collision detection not working..
« Reply #2 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)
« Last Edit: March 08, 2018, 10:40:48 am by AStar »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML collision detection not working..
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AStar

  • Guest
Re: SFML collision detection not working..
« Reply #4 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?

 

anything