SpriteKit中的简单针脚连接没有按预期工作

时间:2017-07-22 14:04:58

标签: ios objective-c sprite-kit skphysicsjoint

我想摆一个钟摆。从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];
}

如果我没有添加针脚逻辑,正如预期的那样,支点停留在场景的中间,扫帚柄落到地板上,所以我知道扫帚柄受到重力的影响。添加引脚后,我明白了:

enter image description here

没有动议。为什么?这导致没有动作......

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

我希望扫帚摇摆和摆动,因为它被固定在支点上。我已经看过关于在关节之前先定位节点的文章,但我已经做到了。期待扫帚摆动我错了吗?我怎么能这样做呢?

2 个答案:

答案 0 :(得分:4)

节点2定义了一个错误的anchorPoint,这里是一个上传示例:

Full code of example

图像:

3 Seconds running code

Swift 3代码:

    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)

目标-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];

答案 1 :(得分:0)

感谢@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];