Frist tắt, cảm ơn bạn @Smick cho mã xe ban đầu như được đăng ở đây: Sprite Kit pin joints appear to have an incorrect anchorSprite Kit - Xuân khớp (giảm xóc)
Tôi đang cố gắng để thêm một phần trượt giữa bánh xe (bánh trái) và khung gầm, cùng với khớp nối lò xo để tạo hiệu ứng giảm chấn.
Với mã của tôi bên dưới, tôi không bị nén. Tôi nhận ra các tài liệu cho thấy một khớp mùa xuân kéo hai bodes với nhau - đảo ngược những gì tôi muốn. Điều này có thể xảy ra ở SK không?
Tôi nghĩ rằng khớp nối pin có thể là thủ phạm? Khi tôi nhận xét ra khớp nối pin, các bộ phận xe hơi đi haywire - tất cả mọi thứ bay xung quanh màn hình. Ban đầu, khớp nối pin gắn bánh xe vào khung xe, nhưng rõ ràng là tôi muốn ghim bánh xe vào bộ giảm xóc.
Ngoài ra, đối số "trục" cho SKPhysicsJointSliding khiến tôi hơi bối rối. Nó muốn một vector. Một vector liên quan đến?
Cảm ơn bạn trước.
- (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]];
}