Game Development Reference
In-Depth Information
collisionBitMask : This is the bitmask that identifies what bodies this body
collides with. You might want some objects to collide with others, and other
objects to pass through others. You will need to set the same bitmasks for
objects that you want to collide.
contactBitMask : This is the bitmask that identifies the body for handling
collisions manually. You might want to have special effects when things
collide; that's why we have this. When bodies have the same bitmask,
a special method will get called for you to handle such collisions manually.
Implementing the physics engine
We might as well start implementing physics in our engine in order to see what the
physics engine gives us. First, we need to set the physics body to our player sprite.
In order to do that, we need more constants in Common.h :
static NSInteger playerMass = 80;
static NSInteger playerCollisionBitmask = 1;
Next, change your init method in ERGPlayer.m to look as follows:
-(instancetype)init
{
self = [super initWithImageNamed:@"character.png"];
{
self.name = playerName;
self.zPosition = 10;
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSi
zeMake(self.size.width, self.size.height)];
self.physicsBody.dynamic = YES;
self.physicsBody.mass = playerMass;
self.physicsBody.collisionBitMask = playerCollisionBitmask;
self.physicsBody.allowsRotation = NO;
}
return self;
}
After creating the sprite and setting the name and z order, we start with the physics
body. We create a physics body with the same size as that of our sprite—it is accurate
enough for our game. We set it as dynamic, as we want to use forces and impulses
on our character. Next up, we set the mass, collision bitmask, and disallow rotation,
as we expect our character sprite to never rotate.
 
Search WWH ::




Custom Search