Do not insert it - you will cause more problems than you solve. You can perform threads and separate logical updates and rendering, but it is difficult to get the correct and large parts of the game loops, essentially single-threaded.
Instead, take a look at advancing your game loop using delta time to scale so that the logic update is largely independent of the machine’s ability to twitter through frames.
In simplified terms, if you use a delta to scale things, no matter how long it takes to go through the frame, a ball moving from one side of the room to the other will take the same amount of time to do this on a really fast PC and slow.
eg. If the ball moves 10 units in one second, and you can determine that 0.1 seconds have passed since the last update (use a high performance timer or something that is available to you), you simply scale the movement by 0.1, and the ball moves by 1 unit.
eg.
private const float BallSpeedInMetresPerSecond = 10; public void Update(float deltaTimeInSeconds) { float adjustedSpeed = deltaTimeInSeconds * BallSpeedInMetresPerSecond;
This will not completely solve your problem (if something is really fast, it will get stuck in the walls independently!), But it is a simple and effective way to keep things predictable and consistent until you run into more complex problems.
If you get this working, and then want to solve a more complex problem, as dash-tom-bang said, look at the expanded collision detection.
Mark simpson
source share