Sign in to follow this  
Exorph

Best approach to stable collision?

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)

[code]
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
[/code]

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.

Any advice?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='Exorph' timestamp='1310637138' post='4835189']
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.
[/quote]
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 this post


Link to post
Share on other sites
[quote name='smasherprog' timestamp='1310660121' post='4835314']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this