Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprite Kit: One node with two physics body

It's possible for one node to have two physics body paths? I want to create a node that has two (circle) physics bodies on the sides of the node.

If it's not possible, is there are any workaround to achieve that? thank you

enter image description here

like image 933
novalagung Avatar asked Dec 05 '25 19:12

novalagung


2 Answers

You want to use [SKPhysicsBody bodyWithBodies:...]. From the docs :

The shapes of the physics bodies passed into this method are used to create a new physics body whose covered area is the union of the areas of its children. These areas do not need to be contiguous. If there is space between two parts, other bodies may be able to pass between these parts. However, the physics body is treated as a single connected body, meaning that a force or impulse applied to the body affects all of the pieces as if they were held together with an indestructible frame.

It would look something like this :

SKPhysicsBody *leftCircle = [SKPhysicsBody bodyWithCircleOfRadius:leftCircleRadius center:leftCircleCenter];
SKPhysicsBody *rightCircle = [SKPhysicsBody bodyWithCircleOfRadius:rightCircleRadius center:rightCircleCenter];

node.physicsBody = [SKPhysicsBody bodyWithBodies:@[leftCircle, rightCircle]];
like image 86
Mike S Avatar answered Dec 07 '25 08:12

Mike S


Here's an example of how to connect to sprite nodes using SKPhysicsJointFixed. First, create two sprites:

    SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(64, 64)];
    // position must be set before creating physics body to avoid bug in iOS 7.0.x
    sprite1.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));
    sprite1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite1.size];
    sprite1.physicsBody.restitution = 1.0;

    [self addChild:sprite1];

    SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(64, 64)];
    sprite2.position = CGPointMake(CGRectGetMidX(self.frame)-sprite2.size.width*2,
                                   CGRectGetMidY(self.frame));

    sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite2.size];
    sprite2.physicsBody.restitution = 1.0;

    [self addChild:sprite2];

then connect the nodes by calling this method:

    [self connectNode1:sprite1 toNode2:sprite2];

This method joins two nodes at their midpoint. Note that both physic bodies must be in the scene prior to calling this method.

- (void) connectNode1:(SKSpriteNode *)node1 toNode2:(SKSpriteNode *)node2
{
    CGPoint midPoint = CGPointMake((node1.position.x + node2.position.x)/2,
                    (node1.position.y + node2.position.y)/2);

    SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:node1.physicsBody
                                                               bodyB:node2.physicsBody
                                                              anchor:midPoint];
    [self.physicsWorld addJoint:joint];
}
like image 37
0x141E Avatar answered Dec 07 '25 09:12

0x141E