SpriteKit节点轮换问题

时间:2014-07-08 20:58:38

标签: ios sprite-kit skphysicsbody

我正在尝试开发iOS的基本游戏,涉及一个类似布娃娃的实体。基本上(正如你在下面所说),我有一个头部,一个身体和一个右臂(所有三个都是基本的精灵节点)通过针接头连接,模拟人体内的连接(大致)。

头部和身体完美地工作,当施加一个力时,身体完全围绕头部旋转,最终垂直地在头部下方休息(见图)。

手臂的底座固定有一个针脚连接到身体上,应该围绕它的底座旋转(有点像肩膀),它的初始旋转设置为45度,所以看起来像一个物理引擎接管之前的手臂。

我的问题是:为什么手臂由于重力而不能垂直放置在垂直位置(如身体)?重力不应导致手臂围绕其基部旋转,直到手臂的尖端直接位于手臂顶部(肩部)的下方?此外,当一个力施加到身体上时(如下面的示例代码所示),身体围绕颈部关节旋转,正如它应该的那样,但是手臂不会从其当前方向移动(这是不可取的)。

如果不是这样,我将如何实现这种效果?

感谢您的时间,如果需要,我很乐意提供任何其他信息

静止时的物理模拟图片: enter image description here

证明问题的相关代码:

//make the head node
SKSpriteNode *head = [SKSpriteNode spriteNodeWithImageNamed:@"head"];
head.size = CGSizeMake(20 * [CFLConstants universalWidthScaleFactor], 20 * [CFLConstants universalWidthScaleFactor]);
head.position = position;
head.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:head.size.width/2];
head.physicsBody.categoryBitMask = CFLPhysicsCategoriesHead;
head.physicsBody.collisionBitMask = 0;
head.physicsBody.dynamic = NO;
[self.ragdollLayer addChild:head];

//make the body node
SKSpriteNode *body = [SKSpriteNode spriteNodeWithImageNamed:@"body"];
body.size = CGSizeMake(head.size.width, head.size.width * 3);
body.position = CGPointMake(head.position.x, head.position.y - head.size.height/2 - body.size.height/2);
body.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:body.size];
body.physicsBody.categoryBitMask = CFLPhysicsCategoriesBody;
body.physicsBody.collisionBitMask = 0;
[self.ragdollLayer addChild:body];

//attach the head and the body (via a neck joint)
SKPhysicsJointPin *neckJoint = [SKPhysicsJointPin jointWithBodyA:head.physicsBody bodyB:body.physicsBody anchor:CGPointMake(head.position.x, head.position.y - head.size.height/2)];
[self.physicsWorld addJoint:neckJoint];

//make the right arm
SKSpriteNode *rightArm = [SKSpriteNode spriteNodeWithImageNamed:@"arm"];
rightArm.size = CGSizeMake(head.size.width/5, head.size.width/5 * 10);
rightArm.anchorPoint = CGPointZero;
CGPoint rightArmPosition = CGPointMake(body.position.x + body.size.width * 1/5, body.position.y + body.size.height * 1/5);
rightArm.position = rightArmPosition;
rightArm.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rightArm.size];
rightArm.physicsBody.categoryBitMask = CFLPhysicsCategoriesRightArm;
rightArm.physicsBody.collisionBitMask = 0;
rightArm.zRotation = -M_PI_4;

//force which makes the arm problem even more noticeable
[body.physicsBody applyImpulse:CGVectorMake(100, 0)];
[self.ragdollLayer addChild:rightArm];

//make the joint which holds the right arm to the body, but should allow the arm to rotate about this point (and doesn't)
SKPhysicsJointPin *rightShoulderJoint = [SKPhysicsJointPin jointWithBodyA:body.physicsBody bodyB:rightArm.physicsBody anchor:rightArmPosition];
[self.physicsWorld addJoint:rightShoulderJoint];

1 个答案:

答案 0 :(得分:2)

根据我的经验,这是因为更改精灵上的锚点,不会改变物理体的锚点。虽然我发誓有时你没有必要,所以也许这是一个操作的顺序。但无论如何,偏移物理体的中心来解释精灵锚点。类似的东西:

spriteRightArm.anchorPoint = CGPointMake(0, 1);

spriteRightArm.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteRightArm.size center:CGPointMake(spriteRightArm.size.width/2, -spriteRightArm.size.height/2)];