Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit SKPhysicsBody collision in one direction like a door you can only go through but not back

In SpriteKit SKPhysicsBody is it possible to have an object you can pass through but not go back.

The idea is their is no collision in one direction so you go through and not go back, like a trap door.

like image 699
Mike Zriel Avatar asked Feb 12 '16 12:02

Mike Zriel


1 Answers

I'm not quite sure one way physics are possible, but you should be able to mess with a physics body's collision bit mask while the game is running to achieve a similar affect.

So you have have your door in an open state, and when it detects the player is touching it*, it changes the bit mask so the player will collide with it. That should allow the player to go through one way, but not come back.

*In reality, have the door detect when the player is no longer touching the door via the player's physics body, and test the x or y location depending on if this is a trap door, or regular door. If the location is far enough away from the door, change the collision bit mask of the door so that the player can't go through.

like image 110
Gliderman Avatar answered Oct 08 '22 11:10

Gliderman