2013-10-13 13 views
9

Sprite Seti ile pim eklemlerini test ediyorum ve olağandışı bir şey buluyorum.Sprite Takımı pin eklemlerinin yanlış bir ankrajı var gibi görünüyor

İstenilen kurulum şudur: bir geniş, düz kutu ve iki daire; Daireler SKPhysicsPinJoints ile kutuya bağlanır, böylece tekerlek gibi davranabilirler.

İşte kodum. Ben mümkün olduğunca kısa ve öz olmasını sağlamaya çalıştık:

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

Ne bekliyorum pim eklemler kendi merkez noktalarında demirlemiş olmasıdır; Ancak, bunu test ettiğimde, eklemler için ankrajların uzak olduğu anlaşılıyor.

Gerçekten belirgin bir şey eksik mi?

cevap

10

Ben de bu sorunu vardı ve neden sprite pozisyon ayarlamadan önce fizik vücudu ayarlıyor.

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

için yukarıdaki Bu çalışması gerekir

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

değişikliği. Teşekkürler Smick.

SpriteKit: How to create Basic Physics Joints

+0

Güzel bir örnek, TVC'nizde, numberOfSectionsInTableView öğesini ve numberOfRowsInSection değerini bildirebilir, sadece [_dataSourceArray count] kullanın. Güzel bir örnek, [head.physicsBody applyTorque: 0.2] eklemelisiniz; Farkı sabit bağlantıya göstermek için pime katılmak. İyi iş - paylaşım için teşekkürler. – DogCoffee

+0

Teşekkür ederim Smick .. Örneği güncelledik. Ancak, TVC bu şekilde kodlanır, çünkü iki bölümden oluşur. Araba örneğinizi Ortak Tip olarak ekleyemedim. :) Bu örnek farklı bir bölüme konur. – Bavan

+0

Şimdi bir örnekte araba göremiyorum, hala sadece eklem listesi vardır. Tüm test kodumu TVC - cool fikrine koyacağım! – DogCoffee

5

Bu kodu deneyin, sizinkileri kullandım ve garip sorunlar aldım, bu yüzden sıfırdan başladım.

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

Çok teşekkürler! Bavan'ın cevabını doğru olarak işaretledim, çünkü gelecekteki okuyucular için daha özlü ve kolay bir yol var, ama önce doğru çözümü sağladınız. –

+0

@DogCoffee: Kodunuzu SpriteNodes ile birleştirmek için kullandım. Ama spritenode'imin pozisyonunu değiştirir. Lütfen bana yardım edebilir misin ? – Nirav

+0

@Nirav Yeni bir soru göndermenizi öneriyorum. Bir süre sprite kiti kullanmamıştım. – DogCoffee

İlgili konular