2013-10-13 23 views
9

我正在測試與雪碧套件鍼關節,我發現一些不尋常的事情發生。雪碧套件針腳關節似乎有一個不正確的錨點

我想要的設置是這樣的:一個寬的扁平盒子和兩個圓圈;圓圈通過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]; 
} 

我期待的是,銷接頭固定在它們的中心點;然而,當我測試這個時,關節的錨點似乎很遙遠。

我是否錯過了一些非常明顯的東西?

回答

10

我也有這個問題,原因是在設置精靈位置之前設置物理體。

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: How to create Basic Physics Joints

+0

不錯的小例子,在你的TVC中,你可以註釋掉numberOfSectionsInTableView,對於numberOfRowsInSection,只需使用[_dataSourceArray count]。它是一個很好的例子,你應該添加[head.physicsBody applyTorque:0.2];以引腳連接真正向固定連接顯示其不同。好工作 - 感謝分享。 – DogCoffee

+0

謝謝Smick ..我更新了示例。但是,TVC是這樣編碼的,因爲它有兩個部分。我無法將您的汽車示例添加爲聯合類型。 :)這個例子被放入不同的部分。 – Bavan

+0

現在我不能在示例中看到汽車,仍然只有關節列表。我將把我所有的測試代碼放入TVC - 很棒的主意! – DogCoffee

5

試試這段代碼,我使用過你的代碼,發現了奇怪的問題,所以從零開始。

- (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)]; 
} 
+0

非常感謝!我將Bavan的答案標記爲正確,因爲它對未來的讀者來說更簡潔和容易,但您首先提供了正確的解決方案。 –

+0

@DogCoffee:我已經使用你的代碼來連接到SpriteNodes。但它改變了我的spritenode的位置。你能幫我麼 ? – Nirav

+0

@Nirav我建議你發佈一個新問題。我有一段時間沒有使用sprite套件。 – DogCoffee