sprite kit - one node with two physics body

落爺英雄遲暮 提交于 2019-12-22 09:33:11

问题


It's possible for one node having two physics body path ? how to do that ? I want to create a node that having two (circle) physics body on sides of the node.

If it's not possible, is there are any tricks to achieve that ? thanks


回答1:


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]];



回答2:


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];
}



回答3:


Here is an easy way to achieve the behavior you are looking for:

How to detect contact on different areas of a physicsbody

//Create SpriteNode1 & physicsBody
//Create SpriteNode2 & physicsBody
[SpriteNode1 addChild: SpriteNode2]

You can position SpriteNode2 relative to SpriteNode1. Any movement, etc. performed on SpriteNode1 will also move SpriteNode2.

Set: SpriteNode2.PhysicsBody.Dynamic=NO;

You could also create a SpriteNode that acts as the main object and add both SN1 and SN2 as children if you find that easier.



来源:https://stackoverflow.com/questions/25178390/sprite-kit-one-node-with-two-physics-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!