Sprite Kit引脚接头似乎有一个不正确的锚点 [英] Sprite Kit pin joints appear to have an incorrect anchor

查看:85
本文介绍了Sprite Kit引脚接头似乎有一个不正确的锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sprite Kit测试针接头,我发现了一些不寻常的事情。

I'm testing out pin joints with Sprite Kit, and I'm finding something unusual happening.

我想要的设置是这样的:一个宽扁盒,和两个圆圈;圆圈通过SKPhysicsPinJoints连接到框中,因此它们可以充当轮子。

My desired setup is this: one wide, flat box, and two circles; the circles are connected via SKPhysicsPinJoints to the box, so they can act as wheels.

这是我的代码。我试图尽可能简洁:

Here's my code. I've tried to make it as concise as possible:

- (SKNode*) createWheelWithRadius:(float)wheelRadius {
    CGRect wheelRect = CGRectMake(-wheelRadius, -wheelRadius, wheelRadius*2, wheelRadius*2);

    SKShapeNode* wheelNode = [[SKShapeNode alloc] init];
    wheelNode.path = [UIBezierPath bezierPathWithOvalInRect:wheelRect].CGPath;

    wheelNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:wheelRadius];

    return wheelNode;
}


- (void) createCar {

    // Create the car
    SKSpriteNode* carNode = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(150, 50)];
    carNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carNode.size];
    carNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    [self addChild:carNode];

    // Create the left wheel
    SKNode* leftWheelNode = [self createWheelWithRadius:30];
    leftWheelNode.position = CGPointMake(carNode.position.x-80, carNode.position.y);
    [self addChild:leftWheelNode];

    // Create the right wheel
    SKNode* rightWheelNode = [self createWheelWithRadius:30];
    rightWheelNode.position = CGPointMake(carNode.position.x+80, carNode.position.y);
    [self addChild:rightWheelNode];

    // Attach the wheels to the body
    CGPoint leftWheelPosition = leftWheelNode.position;
    CGPoint rightWheelPosition = rightWheelNode.position;

    SKPhysicsJointPin* leftPinJoint = [SKPhysicsJointPin jointWithBodyA:carNode.physicsBody bodyB:leftWheelNode.physicsBody anchor:leftWheelPosition];
    SKPhysicsJointPin* rightPinJoint = [SKPhysicsJointPin jointWithBodyA:carNode.physicsBody bodyB:rightWheelNode.physicsBody anchor:rightWheelPosition];

    [self.physicsWorld addJoint:leftPinJoint];
    [self.physicsWorld addJoint:rightPinJoint];
}

我所期待的是针接头固定在它们的中心点;然而,当我测试这个时,关节的锚点看起来很远。

What I'm expecting is that the pin joints are anchored at their centre points; however, when I test this, the anchors for the joints appear to be far off.

我错过了一些非常明显的东西吗?

Am I missing something really obvious?

推荐答案

我也有这个问题,原因是在设置精灵位置之前设置物理体。

I also had this issue and the cause is setting the physics body before setting the sprites position.

carNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carNode.size];
carNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

将上述内容更改为

carNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
carNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carNode.size];

它应该有效。谢谢Smick。

It should work. Thanks Smick.

SpriteKit:如何创建基本物理关节

这篇关于Sprite Kit引脚接头似乎有一个不正确的锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆