1

我正在尝试使用 SKPhysicsJointLimit 在场景中将两个 SKSpriteNode 连接在一起。一个节点不受物理模拟的影响,另一个受物理模拟的影响。这应该导致钟摆运动。但是,当使用 SKPhysicsJointLimit 连接节点时,似乎会创建三个锚点。

可以在这里看到http://imgur.com/a/hQqVP绿色节点不受物理模拟的影响。

// set scene up
self.anchorPoint = CGPointMake(0.5, 0.5);
self.backgroundColor = [SKColor grayColor];
self.view.showsPhysics = YES;


// set up the two bodies to be connected
SKSpriteNode* testSpriteOne = [[SKSpriteNode alloc] initWithColor:[SKColor yellowColor] size:NODE_SIZE];
testSpriteOne.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:NODE_SIZE];
testSpriteOne.position = CGPointMake(-20, -10);
testSpriteOne.physicsBody.dynamic = YES;

[self addChild:testSpriteOne];

SKSpriteNode* testSpriteTwo = [[SKSpriteNode alloc] initWithColor:[SKColor greenColor] size:NODE_SIZE];
testSpriteTwo.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:NODE_SIZE];
testSpriteTwo.position = CGPointMake(93, 166);
testSpriteTwo.physicsBody.dynamic = NO;

[self addChild:testSpriteTwo];

// set up the joint
SKPhysicsJointLimit* ropeJoint = [SKPhysicsJointLimit jointWithBodyA:testSpriteTwo.physicsBody bodyB:testSpriteOne.physicsBody anchorA:testSpriteTwo.position anchorB:testSpriteOne.position];

[self.physicsWorld addJoint:ropeJoint];

我的场景的锚点是 (0.5, 0.5),但如果它设置为 (0, 0),物理模拟可以工作,但位置不正确。另一个解决方法是创建临时替代位置,这些位置被场景的缩放宽度和高度偏移,并在创建关节时使用这些位置。下面的代码显示了这一点。

// works with offset
CGPoint AlternatePosition1 = CGPointMake(testSpriteOne.position.x + self.scene.size.width * self.scene.anchorPoint.x, testSpriteOne.position.y + self.scene.size.height * self.scene.anchorPoint.y);
CGPoint AlternatePosition2 = CGPointMake(testSpriteTwo.position.x + self.scene.size.width * self.scene.anchorPoint.x, testSpriteTwo.position.y + self.scene.size.height * self.scene.anchorPoint.y);
SKPhysicsJointLimit* ropeJoint = [SKPhysicsJointLimit jointWithBodyA:testSpriteOne.physicsBody bodyB:testSpriteTwo.physicsBody anchorA:AlternatePosition1 anchorB:AlternatePosition2];

首先,我不确定为什么会这样。结果点不在场景坐标中。此外,如果精灵包含在世界节点中并且世界节点正在改变其在场景中的位置,则此“解决方案”将不起作用。即使场景中的节点位置用于 SKPhysicsJoint。

那么,有没有办法让 SKPhysicsJointLimit 在锚点为 (0.5,0.5) 的场景中正常运行,而不必偏移节点的位置。

4

0 回答 0