2
votes

I am making a spritekit platformer game with the help of spritekit objective c. I am using spritekit's physics engine for that, every thing is going well except that I haven't found a way to implement a plaformer style collision of the player with the platform.

What I want is my player should collide with the platform while he is falling and not while jumping. Like in following image. Here the player is jumping so he must not collide with the platform

and as In this image the player is falling so he must stand on the platform. enter image description here

I tried to remove collision of the platform with player in didBeginContact method, but that didn't help as my platform is not dynamic type. Adding and removing collision does work with player but not with platform.

Any help will be greatly appreciated.

Edit: Here it is, example of what will happen if I change player's collision bitmask on contact with two adjacent platforms.

player will fall as soon as it will react with another platform.

Edit: aramusss's second solution is good, But it does create one more problem for me. As I have enemies in my game standing on platforms, if I remove platform's physics body the enemies will fall standing on it.

1
One way would be to check player's velocity.dy... If player is falling,velocity.dy will be less than zero. Or you can check player's position and compare it with platform position, and change player's collisionBitMask accordingly ... Not sure how does all that fits into your current setup though...Whirlwind
I tried that technique but it gives problem when there are two bases adjacent to each other. changing player's collision bitmask is problematic. I was thinking if there is any way to changes respective platform's bitmask or something. it will be much better that way.Abhi

1 Answers

1
votes

You can save the platforms in an array, and then check the player Y position. If player.position.y - (player.size.height/2) < platform.position.y - (platform.size.height/2) you deactivate collisions for this platform (means player is below the platform and we don't want it to collide). You should use:

// You should set collision bit mask to avoid collisioning between player and platforms, but not between other objects:
player.physicsBody.collisionBitMask = 1;
player.physicsBody.categoryBitMask = playerCategory; // An int constant, for example 101
player.physicsBody.contactTestBitMask = platformCategory; //Another constant, for example 102

platform.physicsBody.collisionBitMask = 1;
platform.physicsBody.categoryBitMask = platformCategory;
platform.physicsBody.contactTestBitMask = playerCategory;

Setting the values like this will make both bodies not collide, and changing them will make them collide again.

EDIT

Another solution would be to use a NSTimer that will call a method each 0,5 seconds (for example) and this method will check if there is a platform near the player. If there is and it is behind the player, it will create a physicsBody the same size as the platform. If there is a body created that is no more near to the player, or the player is below this platform, this function will delete it.