There are a few good ways of solving this scenario when doing movement on delta time.
1) One of the most common ways is to do "fixed time steps". This is essentially an implementation that forces your update()
(or a movement()
in this case) on the affected object to only run once the "time step" has elapsed. Let's say you have a variable constant for FIXED_STEP:int = 10
, this value of 10 represents 10ms. If your delta time (interval
) has elapsed 10ms or more you issue the update()
however many times your (elapsed time / FIXED_STEP
) and any remaining time gets added to the next elapsed time value.
An example would be saying your delta time is 45ms, so you would run update()
4 times and add 5 to your next delta time.
This will allow your collisions to still be detected. Here is a C# explanation that I think explains it pretty well also: Fixed Time Step C# XNA
2) Another way you can do it is to calculate your distance to move your object between frames and then subdivide the distance to create movement steps. i.e. - Your object (a 40x40 rectangle) at point A will move to point B, the distance is 200px on the X axis, you move your object by 200 / object.width (which is 40) = 5, so you will call your movement()
5 times, and move by the objects width of 40px. This will cover the area of movement and your collision can be detected at any one of those movement updates.
There are other ways of doing this but probably not as good for performance reasons. Ray casting and finding the point of intersection on the given path of the object is very accurate but very performance intensive, and would require a bit of digging to learn and implement.
I've also read that some create a new object from the origin before the update()
to the position after and use that to do the detection. i.e. - object A at point( 0, 0 ) moves to ( 200, 0 ) after the next frame, you create an area from (0, 0) to (200 + object.width, 0 + object.height) and run the collision test against that object instead.
Anyway I hope this helps, good luck!