我正在尝试为 iOS 开发一个基本游戏,其中包含一个类似布娃娃的实体。基本上(如下所示),我有一个头部、一个 body 和一个右臂(这三个都是基本的 Sprite 节点)通过针关节连接,模拟人体的连接(大致)。
头部和 body 完美地工作,当施加力时, body 完美地围绕头部旋转,最终在头部下方垂直静止(见图)。
ARM 的底座通过销关节固定在 body 上,应该围绕它的底座旋转(有点像肩膀),它的初始旋转设置为 45 度,因此在物理引擎接管之前它看起来像一只 ARM .
我的问题是:为什么 ARM 不会由于重力而处于垂直位置(如 body )?重力不应该导致 ARM 围绕其底部旋转,直到 ARM 的尖端直接位于 ARM (肩部)的顶部下方?此外,当一个力被施加到 body 上时(如下面的示例代码所示), body 会围绕颈部关节旋转,正如它应该的那样,但 ARM 不会从其当前方向移动(这是不可取的)。
如果不是这样,我将如何实现这种效果?
感谢您的时间,如果需要,我很乐意提供任何其他信息
静止时的物理模拟图片:
演示问题的相关代码:
//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];
Best Answer-推荐答案 strong>
根据我的经验,这是因为更改 Sprite 上的 anchor 并不会更改物理体的 anchor 。虽然我发誓有时你不必这样做,所以也许这是一个操作顺序。但无论如何,偏移物理体的中心以解释 Sprite anchor 。就像是:
spriteRightArm.anchorPoint = CGPointMake(0, 1);
spriteRightArm.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteRightArm.size center:CGPointMake(spriteRightArm.size.width/2, -spriteRightArm.size.height/2)];
关于ios - SpriteKit 节点旋转问题,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/24641502/
|