# Best approach to stable collision?

This topic is 2415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want my objects to update at a stable framerate, even if the framerate dips. So I've tried to have my GameObjectContainer always update at 60fps like so:

(self.myTickTime is 0.017)

 self.myUpdateTicker = self.myUpdateTicker+deltaTime local toRemove = {} while self.myUpdateTicker > self.myTickTime do self.myUpdateTicker = self.myUpdateTicker - self.myTickTime for i, object in ipairs(self.myActiveObjects) do object:OnUpdate(self.myTickTime) self:HandleCollisionsForObject(object, i) if object.myRemovalFlag then table.insert(toRemove, object.myID) end end end 

However, if the framerate dips too low, this leads to a feedback loop where the collision is run so many times it slows down the framerate further (though I have a lower cap at 10fps).

I've tried moving the Collision code to outside the while loop, but that results in weird behaviour, with objects moving through walls.

##### Share on other sites
int framecounter;
if( framecounter >=60)
do colission checks
framecounter =0;
}

Each loop of your engine, increment framecounter. This will do your checks every 60 frames.

Or do it time based

int miliseconds;
if( miliseconds>=60)
do colission checks
miliseconds=0;
}

Each loop of your engine, add how many miliseconds have passed. This will do your checks every 60 miliseconds

##### Share on other sites
That would probably work, though as far as I can see that will only make it so collision is never updated more often than the cap, it doesn't make up for a lower framerate.
My collision is supposed to be able to handle environmental collisions regardless of framerate though, but right now it's a bit bugged. I'll try to fix it, then try it your way, thanks.

##### Share on other sites

That would probably work, though as far as I can see that will only make it so collision is never updated more often than the cap, it doesn't make up for a lower framerate.
My collision is supposed to be able to handle environmental collisions regardless of framerate though, but right now it's a bit bugged. I'll try to fix it, then try it your way, thanks.

If you use a time based approach, then it would work with lower frame rates. You basically have two options: time based, or frame-based. So, you would have to chose one and go with it.

Time based will ensure that if your frame rate goes to 10fps (your game will be unplayable at this level anyway) it will still run collision checks after the time base passed -- this is the best solution.

A frame based option is good too, but if you are running at 200 fps, that would mean the game is wasting time on collision checks that it could skip because the frame rate is so high.
The frame based option will back off when the game is running slow, So if your game is running at 10 fps, you will do few collision checks.

##### Share on other sites

Time based will ensure that if your frame rate goes to 10fps (your game will be unplayable at this level anyway) it will still run collision checks after the time base passed -- this is the best solution.

I'm not sure I get it.
If I make it so the game will check for collisions every 1/60 second, and the game starts running at 10fps, with the code you posted, won't it just check it once, then wait until the next frame to check again? That means I'll miss some checks whenever the framerate gets lower than 60fps.

Though I guess since this is my bottleneck I can't really do it any other way.

• 10
• 14
• 9
• 9
• 11