Recommended Posts

I've been working on a 2D (tile based) 2D platformer for iOS and I've got basic entity collision detection working, but there's just something *not right* about it and I can't quite figure out how to solve it.

There are 2 forms of collision between player entities as I can tell, either the two players (human controlled) are hitting each other side-to-side (i. e. pushing against one another), or one player has jumped on the head of the other player (naturally, if I wanted to expand this to player vs enemy, the effects would be different, but the types of collisions would be identical, just the reaction should be a little different).

In my code I believe I've got the side-to-side code working: If two entities press against one another, then they are both moved back on either side of the intersection rectangle so that they are just pushing on each other.

I also have the "landed on the other player's head" part working.

The real problem is, if the two players are currently pushing up against each other, and one player jumps, then at one point as they're jumping, the height-difference threshold that counts as a "land on head" is passed and then it registers as a jump. As a life-long player of 2D Mario Bros style games, this feels incorrect to me, but I can't quite figure out how to solve it.

My code: (it's really Objective-C but I've put it in pseudo C-style code just to be simpler for non ObjC readers)

void checkCollisions() {

// For each entity in the scene, compare it with all other entities (but not with one it's already compared against)
for (int i = 0; i < _allGameObjects.count(); i++) {

// The NUDGE just offsets either object back to the left or right
// After the nudging, they are exactly pressing against each other with no intersection
firstGameObject.nudgeToRightOfIntersection(intersection);
secondGameObject.nudgeToLeftOfIntersection(intersection);

I think my collision detection code is pretty close, but obviously I'm doing something a little wrong. I really think it's to do with the way my jumps are checked (I wanted to make sure that a jump could happen from an angle (instead of if the falling player had been at a right angle to the player below).

Can someone please help me here? I haven't been able to find many resources on how to do this properly (and thinking like a game developer is new for me). Thanks in advance!

0

Share this post

Link to post

Share on other sites

You'll probably want to make the direction of movement a factor in collision detection. For example, if a character is moving up (i.e. has started jumping), he can't really be landing on his opponent's head (since "landing" implies that the character is falling or has passed the peak of his jump).