Almost working 2D Collisions

| | August 11, 2015

I’m terribly sorry I’m asking this question YET AGAIN, but I can almost guarantee that this will be the last time I’ll have to ask.

I’m currently on the verge of FINALLY getting these collisions to work for my game, made with libGDX in Java. My collisions use the same method as (and are basically copied and modified code from) the XNA Platformer example (here) where the direction of the collision is based on the rectangle where two objects are overlapping.

The collisions themselves almost work perfectly, but for some reason, holding down/up and left and colliding with the floor/ceiling while doing so doesn’t seem to work well. I’m not at all sure why.

Instead of vaguely giving my problem and snippets of code, I’ve decided to instead provide a binary and the source of the game I have so far so you can see for yourself what my problem is. Link. (Note: make sure you unzip everything into a folder somewhere or it will not work)

You’ll find the collision code in the method workingCollisions() in Please excuse the messy code and terrible graphics as this whole thing is in pre-pre-alpha.

If anyone is kind enough and helps me out here, you are the best person ever. I’m completely desperate; I’ve been trying this on and off for months and I just can’t get it to work. I cannot thank you enough.

One Response to “Almost working 2D Collisions”

  1. I’m not going to go through your code, but one thought is that, if you’re “holding down” navigation buttons and moving relatively quickly, your bounding box may skip from one side of the object you’re colliding with to the other in a single step.

    One way to avoid this is to calculate a ray from the original position to the new one, and check that for intersections with the other objects in your scene. Since you’re checking the path the object takes instead of just it’s new position, it’ll catch anything that should have triggered a collision.

Leave a Reply