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

Author Topic: What comes first? Collision or update?  (Read 1479 times)

0 Members and 1 Guest are viewing this topic.

Bizarreofnature

  • Newbie
  • *
  • Posts: 48
    • View Profile
What comes first? Collision or update?
« on: October 24, 2017, 04:13:33 pm »
Hey there!

I have 3 routines.

- update
- collision
- draw

I wonder what comes first in a game loop.

In "Update" im updating the positions of objects like player, enemy etc. .
In collision, im checking for collisions.
And in Draw im simply drawing.

I though about this for quite a while and i have no clue.

Thanks in advance,
Bizarreofnature

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: What comes first? Collision or update?
« Reply #1 on: October 24, 2017, 04:43:41 pm »
Collision handling is basically part of updating.
There are different approaches to collision handling, so it's best to just try one and see if it works for your situation.

I often tend to update entity positions, then check collision and move the colliding entities to a proper position (i.e. not overlapping) and trigger collision action code. Depending on the game, you might have to run the collision checking code multiple times, in case the re-positioning triggers another collision, but make it a fixed number of times, to not run into an infinite loop.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Bizarreofnature

  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: What comes first? Collision or update?
« Reply #2 on: October 24, 2017, 05:07:36 pm »
Thanks for your help!

It did give me a clearer view of what to do and what not.

Actually I think I'll put collision at first. And that is because:

If id update first and then check for collisions, it wouldnt be drawn in time. Which means for example my ship will explode without actually touching anything that is drawn yet.

But maybe ppl can still help me even more, im new to this (collision)
« Last Edit: October 24, 2017, 05:28:38 pm by Bizarreofnature »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: What comes first? Collision or update?
« Reply #3 on: October 24, 2017, 06:00:05 pm »
If you check for collisions before updating, the collision won't actually happen until after it's been drawn. Therefore, the (standard) updates should be done before collision, which allows the collisions to calculate if these new positions are collides.

You could think of it like this:
- update
- collision
- response
- draw
where response is a kind of extra update that 'responds' to collisions by moving things so they no longer collide (in the case of walls/floors etc.) or signal to your game that they are colliding and should be dealt with (in the case of dying, for example). Dealing with that is also a part of the response. Basically, it either moves things so they are no longer colliding or it changes some other state (player = dead).

As mentioned by Exploiter above, it may be required to loop back to collision (doing both collision and response) from response until there are no collisions. This is similar in concept to the while loop for polling events.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Bizarreofnature

  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: What comes first? Collision or update?
« Reply #4 on: October 24, 2017, 06:36:54 pm »
Thank you very much for your answer.

I see. The problem I have is overlapping sprites. That shouldn't be too hard to solve.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: What comes first? Collision or update?
« Reply #5 on: October 24, 2017, 09:55:32 pm »
You're welcome. :)

Remember that determining if they overlap is the collision detection and dealing with it when they do is the collision response.

Basically, decide what should when they are overlapping (colliding) and perform that in the response section.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything