SKPhysicsJointLimit在场景中创建三个锚点

时间:2015-02-07 19:14:23

标签: sprite-kit skphysicsjoint

我正在尝试使用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)的场景中正常运行,而不必偏移节点的位置。

0 个答案:

没有答案