gpt4 book ai didi

ios - SpriteKit 节点旋转问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:50 24 4
gpt4 key购买 nike

我正在尝试为 iOS 开发一款涉及布娃娃类实体的基本游戏。基本上(如下所示),我有一个头、一个 body 和一个右臂(这三个都是基本的 sprite 节点)通过针关节连接,模拟人体中的关节(大致)。

头部和 body 完美配合,当施加力时, body 围绕头部完美旋转,最终在头部下方垂直静止(见图)。

ARM 的底部用一个销接头固定在 body 上,应该围绕其底部旋转(有点像肩膀),并且它的初始旋转设置为 45 度,所以它看起来像物理之前的 ARM 引擎接管。

我的问题是:为什么 ARM 不会因重力而停留在垂直位置(就像 body 一样)?难道重力不应该导致 ARM 围绕其底部旋转,直到 ARM 尖端直接位于 ARM 顶部(肩部)下方吗?此外,当对 body 施加力时(在下面的示例代码中显示), body 会完全按照它应该的方式围绕颈部关节旋转,但 ARM 不会从其当前方向移动(这是不可取的)。

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

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

静止物理模拟图片: 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];

最佳答案

根据我的经验,这是因为更改 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com