Sprite Kitpipe脚关节似乎具有不正确的锚点
我正在用Sprite Kittesting针脚关节,并且发现了一些不寻常的事情。
我想要的设置是这样的:一个宽,扁平的盒子和两个圆圈; 圆圈通过SKPhysicsPinJoints连接到箱子,所以它们可以作为车轮。
这是我的代码。 我试图尽可能简洁:
- (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]; }
我所期望的是,销接头是锚定在其中心点; 但是,当我testing这个时,关节的锚点似乎很遥远。
我错过了一些非常明显的东西吗
我也有这个问题,原因是在设置精灵位置之前设置物理体。
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。
SpriteKit:如何创build基本的物理关节
试试这个代码,我用你的,并得到奇怪的问题,所以从头开始。
- (SKShapeNode*) makeWheel { SKShapeNode *wheel = [[SKShapeNode alloc] init]; CGMutablePathRef myPath = CGPathCreateMutable(); CGPathAddArc(myPath, NULL, 0,0, 16, 0, M_PI*2, YES); wheel.path = myPath; return wheel; } - (void) createCar { self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(0, 0, self.size.width, self.size.height)]; // 1. car body SKSpriteNode *carBody = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(120, 8)]; carBody.position = CGPointMake(200, 200); carBody.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carBody.size]; [self addChild:carBody]; // 2. wheels SKShapeNode *leftWheel = [self makeWheel]; leftWheel.position = CGPointMake(carBody.position.x - carBody.size.width / 2, carBody.position.y); leftWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16]; [self addChild:leftWheel]; SKShapeNode *rightWheel = [self makeWheel]; rightWheel.position = CGPointMake(carBody.position.x + carBody.size.width / 2, carBody.position.y); rightWheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16]; [self addChild:rightWheel]; // 3. Join wheels to car [self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:carBody.physicsBody bodyB:leftWheel.physicsBody anchor:leftWheel.position]]; [self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:carBody.physicsBody bodyB:rightWheel.physicsBody anchor:rightWheel.position]]; // 4. drive car [carBody.physicsBody applyForce:CGVectorMake(10, 0)]; }