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

Author Topic: Where to calculate collisions and physics  (Read 1312 times)

0 Members and 1 Guest are viewing this topic.

bovacu

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Where to calculate collisions and physics
« on: March 18, 2019, 10:18:32 am »
Hi! This is my first question in the forum as I'm new in SFML. I usually use Libgdx as 2d gamedev framework and I wanted to start coding games in C++. In Libgdx there's a method call render(), where you get the input, do updates and then render, all in the same method, but in a specific order.

Now I have this nice game loop that I've learnt from the book SFML Game Development:
void Game::run(){
      sf::Clock clock;
      sf::Time timeSinceLasUpdate;
     
      while(this->renderWindow.isOpen()){
              this->handleEvents();
              timeSinceLastUpdate += clock.restart();

              while(timeSinceLastUpdate > this->timePerFrame){
                      timeSinceLastUpdate -= this->timePerFrame;
                      this->handleEvents();
                      this->update(this->timePerFrame);
              }

              this->render();
      }
}
 

Where timePerFrame is 1.f/fps, and in my case fps=60.f.
Where handleInput() is a method that detects the input of the player (mouse and keys)
Where update() is the method to update positions of player...
Where render() is the method that draws on screen.

Here is my question, given my game loop (btw, if there's any problem with it, pls tell me), where should I calc if entities are colliding and the physics? It's better in the handleInput, update or render?

I think handleInput is not really the method where it should be calculated, but due to the frame I've been using, I'm not too sure.

Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Where to calculate collisions and physics
« Reply #1 on: March 18, 2019, 05:16:00 pm »
Welcome! :)

The general philosophy (with SFML) is to keep rendering (the actual drawing/displaying of stuff) and game logic separated from each other as much as possible.
As such, you'd be doing the physics & collision handling in the update phase.

For your game loop the most important part (as explained in the book) is to do the updating inside the inner while loop. As that one gets executed for as long and as many times as possible, before the next frame should be rendered (see also the famous Fix Your Timestep article).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bovacu

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Where to calculate collisions and physics
« Reply #2 on: March 18, 2019, 07:30:43 pm »
Welcome! :)

The general philosophy (with SFML) is to keep rendering (the actual drawing/displaying of stuff) and game logic separated from each other as much as possible.
As such, you'd be doing the physics & collision handling in the update phase.

For your game loop the most important part (as explained in the book) is to do the updating inside the inner while loop. As that one gets executed for as long and as many times as possible, before the next frame should be rendered (see also the famous Fix Your Timestep article).

Thank you so much eXpl0it3r!