user4909608
user4909608

Reputation:

categoryBitMask Collision Situation

I am currently using a collisionBitMask between the player and ground which works 100%. I am turning the ground bit mask on and off.

I use this code to turn the ground mask OFF

ground.physicsBody?.categoryBitMask = 0

and I use this code to turn the ground mask ON

ground.physicsBody?.categoryBitMask = kCategoryBitMaskGround

this works 100%! HOWEVER, if the player is already inside the ground when the code turns ON the ground bitmask, it does NOT register the collision between the player and the ground and therefore does not run ANY code i have put in the "collision" part between the player and the ground. The player still "interacts" with the ground from a physics aspect, but no collision is registered.

I have tried to fix this problem, but it seems it is a limitation of swift...

Anyone can think of a way around this problem?

Upvotes: 1

Views: 63

Answers (1)

Luca Angeletti
Luca Angeletti

Reputation: 59496

It really depends by how you defined the physics bodies.

I assume the "ground" is a rectangle and you created a physics body around its perimeter.

Something like this

ground.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: 0, y: 0, width: 100, height: 200))

If this (or something equivalent) is what you did then the behaviour you are experiencing is perfectly normal because physics collision will happen only when the physics body of the player does intersect the perimeter of the ground.

Look at the following 3 scenarios.

No contact

In the following image the player and the perimeter of the ground do not intersect.

enter image description here

Contact

Now the player does intersect the perimeter of the ground.

enter image description here

No contact

Again no intersection between the perimeter of the ground and the player.

enter image description here

Upvotes: 1

Related Questions