SpriteKit 中的简单销接头无法按预期工作

Simple pin joint in SpriteKit doesn't work as expected

我想做一个钟摆。从 SKScene 开始,一切都默认,我执行以下操作...

- (void)createSceneContents {
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.view.bounds];

    // object 1 is the fulcrum
    SKSpriteNode *object1 = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(5, 5)];
    [self addChild:object1];
    object1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object1.frame.size];
    object1.physicsBody.dynamic = NO;
    object1.position = self.view.center;

    // object2 is like a broomstick, which I will pin to the fulcrum
    SKSpriteNode *object2 = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(90, 2)];
    [self addChild:object2];
    object2.anchorPoint = CGPointMake(0.0, 0.5);
    object2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object2.frame.size];
    object2.position = self.view.center;

    // pin the physics bodies
    SKPhysicsJointPin *pinJoint = [SKPhysicsJointPin jointWithBodyA:object1.physicsBody bodyB:object2.physicsBody anchor:object1.position];
    [self.physicsWorld addJoint:pinJoint];
}

如果我不加pin逻辑,果然支点在场景中间,扫帚掉在地上,所以我知道扫帚是受重力的。添加图钉后,我得到这个:

没有动作。为什么?这也不会导致运动...

[object2.physicsBody applyForce:CGVectorMake(0, -5)];

我预计扫帚会摆动和摆动,因为它固定在支点上。我看过关于先定位节点,然后再定位关节的文章,但我已经做到了。我期望扫帚摆动是错误的吗?我怎样才能做到这一点?

节点 2 有错误定义的锚点,这里上传一个例子:

Full code of example

图片:

Swift3个代码:

    let nodeSize = CGSize(width: 10, height: 10)
    let node = SKSpriteNode(color: .red, size: nodeSize)
    node.physicsBody = SKPhysicsBody(rectangleOf: nodeSize)
    node.physicsBody?.isDynamic = false
    self.addChild(node)

    let node2Size = CGSize(width: 60, height: 8)
    let node2 = SKSpriteNode(color: .green, size: node2Size)
    node2.position = CGPoint(x: 30, y: 0)
    node2.physicsBody = SKPhysicsBody(rectangleOf: node2Size)
    node2.physicsBody?.mass = 1.0
    self.addChild(node2)


    let a = SKPhysicsJointPin.joint(withBodyA: node.physicsBody! , bodyB: node2.physicsBody!, anchor: CGPoint(x: 0.0, y: 0.0))
    self.physicsWorld.add(a)

Objective-C:

SKSpriteNode *object1 = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(5, 5)];
[self addChild:object1];
object1.position = self.view.center;
object1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object1.frame.size];
object1.physicsBody.dynamic = NO;

SKSpriteNode *object2 = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(90, 2)];
[self addChild:object2];
object2.position = CGPointMake(self.view.center.x+45, self.view.center.y);

object2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object2.frame.size];

SKPhysicsJointPin *pinJoint = [SKPhysicsJointPin jointWithBodyA:object1.physicsBody bodyB:object2.physicsBody anchor:self.view.center];
[self.physicsWorld addJoint:pinJoint];

感谢@Maetschl,我能够按如下方式使其在 Objective-c 中工作(删除锚点并将摆动节点定位到一个边缘)...

SKSpriteNode *object1 = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(5, 5)];
[self addChild:object1];
object1.position = self.view.center;
object1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object1.frame.size];
object1.physicsBody.dynamic = NO;

SKSpriteNode *object2 = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(90, 2)];
[self addChild:object2];
object2.position = CGPointMake(self.view.center.x+45, self.view.center.y);

object2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:object2.frame.size];

SKPhysicsJointPin *pinJoint = [SKPhysicsJointPin jointWithBodyA:object1.physicsBody bodyB:object2.physicsBody anchor:self.view.center];
[self.physicsWorld addJoint:pinJoint];