How to make SKSpritekit nodes bounce off each other

I know how to set up the physical attributes, but I don't know what code I need to do to make them reflect off each other in a realistic way, like balls on a pool table. I have a didBeginContact method ready,

-(void)didBeginContact:(SKPhysicsContact*)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision == (gearCategory | gear1Category)) {
    [
}
}

      

but i have no idea what code i need, can someone with good math and vectorization skills help me?

+3


source to share


1 answer


This is a very simple task in a set of sprites.

//Add to top of scene
static const uint32_t spriteCategory =  0x1 << 0;

//declare when creating sprite
sprite.physicsBody.categoryBitMask = spriteCategory;
sprite.physicsBody.collisionBitMask = spriteCategory;

      

To do this, a sprite is set that collides with all sprites given spriteCategory as their categoryBitMask. You can customize your sprite to several different categories using the following code.

sprite.physicsBody.collisionBitMask = spriteCategory|otherCategory|anotherCategory;

      



Contact trigger is used to execute some code when nodes are touched. You must use the contactTestBitMask property to detect when elements are touched. collisionBitMask automatically handles conflicts for you.

I have attached a link containing the entire section on collision and contact.

http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners

+4


source







All Articles