Frist fuori, grazie @Smick per il codice del veicolo iniziale come postato qui: Sprite Kit pin joints appear to have an incorrect anchorSprite Kit - Primavera Giunti (shock absorber)
Sto cercando di aggiungere un giunto scorrevole tra la ruota (ruota sinistra) e telaio, insieme a un giunto a molla per creare un effetto ammortizzante.
Con il mio codice qui sotto, non ottengo alcuna compressione. Mi rendo conto che la documentazione mostra un'articolazione della molla che tira insieme due bodes: il contrario di ciò che voglio. È possibile in SK?
Penso che il perno comune possa essere il colpevole? Quando commento l'articolazione del perno, le parti della macchina vanno in tilt: tutto vola sullo schermo. Originariamente, l'articolazione del perno bloccava la ruota al telaio, ma ovviamente volevo fissare la ruota all'ammortizzatore.
Inoltre, l'argomento "asse" per SKPhysicsJointSliding mi ha un po 'confuso. Vuole un vettore Un vettore relativo a?
Grazie in anticipo.
- (SKShapeNode*) makeWheel
{
SKShapeNode *wheel = [[SKShapeNode alloc] init];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 16, 0, M_PI*2, YES);
wheel.path = myPath;
wheel.physicsBody.mass = 0.5;
return wheel;
}
- (void) createCar{
// 1. car body
SKSpriteNode *carBody = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(120, 8)];
carBody.position = CGPointMake(200, 700);
carBody.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:carBody.size];
carBody.physicsBody.mass = 1.0;
[self addChild:carBody];
// 2. wheels
SKShapeNode *leftWheel = [self makeWheel];
leftWheel.position = CGPointMake(carBody.position.x - carBody.size.width/2, carBody.position.y-40);
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];
/* Build left shock absorber and attach wheel */
CGVector av =CGVectorMake(0.0, 5.0);
SKPhysicsJointSliding *leftSlide = [SKPhysicsJointSliding jointWithBodyA:carBody.physicsBody
bodyB:leftWheel.physicsBody
anchor:leftWheel.position
axis:av];
SKPhysicsJointSpring *leftSpring = [SKPhysicsJointSpring jointWithBodyA:carBody.physicsBody bodyB:leftWheel.physicsBody
anchorA:CGPointMake(carBody.position.x - carBody.size.width/2, carBody.position.y)
anchorB:leftWheel.position];
SKPhysicsJointPin *leftPin = [SKPhysicsJointPin jointWithBodyA:leftSpring.bodyA
bodyB:leftSpring.bodyB
anchor:leftWheel.position];
[self.physicsWorld addJoint:leftSlide];
[self.physicsWorld addJoint:leftSpring];
[self.physicsWorld addJoint:leftPin];
[self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:carBody.physicsBody bodyB:rightWheel.physicsBody anchor:rightWheel.position]];
}