So I’ve been coding off and on for about 2-3 years now. I’ve had this ongoing project that is a 2D game engine made with LWJGL. Its in a pretty finished state, but has a few quirks. One of those, happens to be collision response!
This is how I handle collisions between an entity and some other rectangle:
- Check if the two rectangles (java.awt.Rectangle rectangles that is) are intersecting
- Determine the position of the entity relative to the other rectangle (above, below, to the right, or to the left of it)
- Set the velocity of the entity to move in the opposite of the other rectangle, thus correcting the collision.
If I move the entity straight at a wall using the above technique to handle the collision, this can result in the entity moving back and forth rapidly, even at high framerates, instead of appearing still. Sometimes one of the rectangles can even clip into the other, and be stuck (this happens especially on corners).
Obviously this system isn’t sufficient, so what is a better way to do 2D collision response? Thank you!