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.
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.
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.
The solution is just to change the collisionBitMask
func platformSolid() {
self.physicsBody?.collisionBitMask = kBIT_MASK_PLAYER | kBIT_MASK_WALL | kBIT_MASK_PLATFORM
}
func platformThrough() {
self.physicsBody?.collisionBitMask = kBIT_MASK_PLAYER | kBIT_MASK_WALL
}
self in this example is player Collision Bit Mask
In the delegate:
func didEndContact(contact: SKPhysicsContact) {
if contact.bodyB.categoryBitMask == kBIT_MASK_PLATFORM {
player.platformSolid()
}
}
So once the player has passed ( didEndContact ) you make the platform ( door ) solid.
In your contact test of door and object, you need to check the direction the object is traveling (you can use velocity to get this). If the object is traveling in the direction that the door would block them, then you place a value on the doors categoryBitMask (set it in a way that it will AND with the nodes collisionBitMask to create a value > 0), otherwise you need to remove the doors categoryBitMask (or set it in a way it will not AND the nodes collisionBitMask so that it creates a value of 0)
The most straightforward way to do that would be to add two child sprites to the node representing top and bottom of the trap door. That way you can test which direction a colliding sprite is coming from, and enable/disable dynamics on the other as needed.
© 2022 - 2024 — McMap. All rights reserved.