2013-10-13 11 views
9

Sto testando i giunti a perno con Sprite Kit e sto trovando qualcosa di insolito.I giunti a perno del kit Sprite sembrano avere un'ancora non corretta

La configurazione desiderata è questa: una casella larga, piatta e due cerchi; i cerchi sono collegati tramite SKPhysicsPinJoints alla scatola, in modo che possano agire come ruote.

Ecco il mio codice. Ho cercato di renderlo il più conciso possibile:

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

Quello che mi aspetto è che i giunti a perno sono ancorati ai loro punti centrali; tuttavia, quando provo questo, le ancore per le articolazioni sembrano essere lontane.

Mi manca qualcosa di veramente ovvio?

risposta

10

Ho riscontrato questo problema e la causa è l'impostazione del corpo della fisica prima di impostare la posizione degli sprites.

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

Modificare il sopra per

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

dovrebbe funzionare. Grazie Smick.

SpriteKit: How to create Basic Physics Joints

+0

Un piccolo piccolo esempio, nel tuo TVC, puoi commentare numberOfSectionsInTableView, e per numberOfRowsInSection, usa solo [conteggio _dataSourceArray]. È un buon esempio, dovresti aggiungere [head.physicsBody applyTorque: 0.2]; al pin si unisce per mostrare davvero la sua differenza con il giunto fisso. Buon lavoro - grazie per la condivisione. – DogCoffee

+0

Grazie Smick .. Ho aggiornato l'esempio. Ma, TVC è codificato in questo modo, perché ha due sezioni. Non ho potuto aggiungere il tuo esempio di auto come tipo comune. :) Questo esempio è inserito in una sezione diversa. – Bavan

+0

Non riesco a vedere l'auto nell'esempio ora, ha ancora solo l'elenco delle articolazioni. Inserirò tutto il mio codice di test in una TVC: un'idea interessante! – DogCoffee

5

Prova questo codice, ho usato il tuo e ho avuto problemi strani, quindi ho iniziato da zero.

- (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

Grazie mille! Ho contrassegnato la risposta di Bavan come corretta, poiché è più concisa e più semplice da seguire per i futuri lettori, ma prima hai fornito la soluzione corretta. –

+0

@DogCoffee: ho utilizzato il codice per collegarsi a SpriteNodes. Ma cambia la posizione del mio spritenode. Potete per favore aiutarmi ? – Nirav

+0

@Nirav Ti suggerisco di inviare una nuova domanda. Non ho usato il kit sprite per un po '. – DogCoffee

Problemi correlati