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