3

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

如果我不添加引脚逻辑,支点会如预期的那样停留在场景的中间,并且扫帚柄会掉到地板上,所以我知道扫帚柄会受到重力的影响。添加引脚后,我得到了这个:

在此处输入图像描述

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

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

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

4

2 回答 2

4

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

完整的示例代码

图片:

3秒运行代码

斯威夫特 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];
于 2017-07-22T16:35:23.010 回答
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];
于 2017-07-22T17:09:47.427 回答