Pop Flamingo
Pop Flamingo

Reputation: 2191

Make a moving physics body collide with others without being affected itself

I have a SpriteKit scene with physics bodies, at some point, I add a sprite and I want a "one way collision" with others : I want them to collide with it but I want it to be unaffected and to continue its trajectory exactly as if it didn't hit anything.

A solution that partially works is to affect a really high mass to this physics body so that the collisions on it make negligible changes to its direction and velocity, but unfortunately this causes other objects to bounce really strongly from my object as it carries a lot of energy.

Is there a way to get the behaviour I expect ?

Thank you

Upvotes: 1

Views: 311

Answers (2)

Souritra Das Gupta
Souritra Das Gupta

Reputation: 130

Steve Ives' answer is perfect. But I would just like to add that in future if you do need to use a body with much higher mass and don't want other objects to bounce off it at incredible speeds, modify the restitution of the physics body of the object with smaller mass.

The default restitution value is 0.2 and it signifies how much energy is lost after the body collides with another.

Upvotes: 0

Steve Ives
Steve Ives

Reputation: 8134

All you have to do is to set the collisonBitMask of the other body to include the categoryBitMask of the body you want to be unaffected, and then make sure that the collsionBitMAsk of the body to be unaffected does NOT include any of the categoryBitMasks of the bodies that it is not to be affected by.

It will then happily smash everything out of it's way 😀

Upvotes: 1

Related Questions