Search code examples
androidcollision-detectionframe-rate

Android Game Development: Framerate & Collisions


I've created a simple collision detection script, which works this way: When the distance between the hero and an object is x pixels, the hero can "walk" x pixels, when he would not collide with an object (hero + 3px = no collision) he moves by 5 pixels. But I also have to consider the framerate and therefor multiply his speed with the elapsed time /20 My problem is, when the framerate at some time is very low or high, he just moves by an additional pixel (1px) ..the chance is very small, but it still can happen. so what can I do to prevent this?


Solution

  • Add a position correction to the end of post-collision-check or add a velocity correction to the end of pre-collision-check.

    Post-collision: object is translated back to the collision point.

    Pre-collision: object speed is altered temporarily so in the next frame it will be on the point of collision.

    Example:

    • Your object moves 75 pixels and tunnells through the wall. What to do? You need a position history of 1-iteration-back. Looking to history, you see it was actually behind the wall, then the current location-->it is now passed the wall xx pixels. Then you would set its new position next to wall before painting.
    • You cannot know when it will lag your android: better algortihm needed to make it independent of fps. How? you may just pause whole world a bit until fps is steady again or just store the next few iteration before painting then calculate things before painting.