Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with game development. Render loop?

I'm working on a simple game, this is my first game project.

Most of the samples I find have a Render Loop where all the game logic is made too and I just don't like this. Let's say I have a ball with X=0, and a wall in X=10 and in a slow machine, the first loop places the ball in X=7 and in a second loop, it places the ball in X=14. It would just crash the game!

Is this "render loop" the right way to make games? Should I write code to check for things like this in every frame? Example, new frame X=14, last frame have X=7, so I should check if there's anything from X=7 to X=14??

I was thinking that I should have a separated thread for the game logic and in the render loop, I should just "take a snapshot" of the current game logic and display that, no?

How do you guys, experienced game developers work around this?

thanks!

like image 971
John Avatar asked May 06 '10 20:05

John


People also ask

How do you make a good game loop?

Give the Player Something to Work Towards, and Keep It Simple. The best way to utilize a core game loop is to give players objectives—the fewer goals at once, the better. Once again, work on the balance of attractive and intuitive designs while keeping a robust core element of gameplay for the players.

Can you tell me what a game loop is?

A game loop runs continuously during gameplay. Each turn of the loop, it processes user input without blocking, updates the game state, and renders the game. It tracks the passage of time to control the rate of gameplay. This pattern decouples progression of game time from user input and processor speed.

What is a render loop?

The Render Loop is a continuous process by which touch events are handed to an app, and then changes to the UI are sent to the operating system where the frame is finalized. It's a loop, and it happens at the device's refresh rate.

What should a main game loop do?

Every game that you've ever played—from Pong to Fortnite—uses a game loop to control its game play. The game loop does four very important things. 00:13 It processes user input, it updates the state of all the game objects, it updates the display and your audio output, and it also maintains the speed of the game.


1 Answers

As another answer stated, the problem you're seeing is called "tunneling" It's the "bullet through paper" problem, the bullet is moving fast, the paper is thin, how do you know that a collision happened?

It's easy if your world boundaries are simple. E.g. in Tetris, the blocks are only allowed to move left and right until they hit the sides, and it's easy to test if the bottom-most coordinate is hitting the "ground." These tests are simple because you can do one axis at a time, and collisions against the sides means something different than collisions against the bottom of the pit. If you have a rectangular room, just "stop" the moving object if its movement has put it outside the room by clamping its coordinates. I.e. if the room width is from -3 to +3, and your object has an X of 5, just change it to 3 and you're done.

If you want to handle more complicated worlds, it's a bit trickier. You'll want to read up on "swept" geometry collision. Basically, if you have a circle, you need to do collision tests with a capsule instead, the shape that would be made by "sweeping" the circle from its start point to its end point. It'll be like a rectangle with semicircles on either end. The math is surprisingly straight forward (IMHO), but it can be tricky to get it right and to truly understand what's going on. It's worth it though!

Edit: On the thread issue- no need to complicate things. One thread is fine. Skipping update frames can get messy too, and is pretty advanced since you actually need to figure out "the future" and then do interpolation of all interesting values up to that point. I don't call it the "render" loop, myself, as the render loop is just one part of the process.

def GameLoop():
   while True:
      ReadInputs()
      FigureOutWhatStuffDoes()
      DrawItAll()

Edit 2: This seems like an interesting discussion: http://www.gamedev.net/community/forums/topic.asp?topic_id=482397

like image 130
dash-tom-bang Avatar answered Sep 27 '22 20:09

dash-tom-bang