If you create a separate thread for this you also create a lot of complexity that you might not want to deal with. It's easy to handle with one thread and one loop.
Basically what you want to do is have a loop that does both logic and rendering, but not necessarily in every iteration. See this pseudo-code:
while(true) {
oldTime = currentTime;
currentTime = systemTime();
timeStep = currentTime - oldTime;
// Only do logic x times / second
if( currentTime > lastLogicTime + logicRefreshTime ){
doGameLogic( currentTime - lastLogicTime );
lastLogicTime = currentTime;
}
// Extrapolate all movements using timeStep
renderGraphics( timeStep );
wait( screenRefreshTime );
}
void doGameLogic( timeStep ) {
// Update all objects
for each( gameObject obj )
obj.move( timeStep );
}
Let all solid movable objects inherit the class SolidObject. When you call SolidObject.move(timeStep)
that method checks to see how far the object can be moved within the given timeStep
. If there is a wall before this point then the object should stop, bounce and change direction, die or whatever you like.
Edit:
If two objects move you might want to check if and where they collide. Lots of games don't do this very well, but here's how you do it:
First calculate the line of movement between the oldTime
and the currentTime
for every object that moves. Then compare the lines to see if two lines intersect. Note, you need to take the objects' size into account. The intersection point is where the objects collide. Using this method you can accurately detect collisions of moving objects.