2013-10-06 2 views
1

create convex SKPhisycsBodies

 CGPathMoveToPoint (path, NULL, self.size.width/4.0, self.size.height/4.0); 
     CGPathAddLineToPoint(path, NULL, self.size.width/2.0, -self.size.height/4.0); 
     CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, -self.size.height/4.0); 
     CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, self.size.height/4.0); 
     CGPathCloseSubpath (path); 
     self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path]; 
     self.physicsBody.affectedByGravity = YES; 
     self.physicsBody.categoryBitMask = solidCategory; 
     self.physicsBody.dynamic = YES; 
     [self addChild:shape1]; 

     self.auxiliaryShapeNode = [[SKSpriteNode alloc] init]; 
     CGPathMoveToPoint (path_aux, NULL, 3*self.size.width/8.0, 0); 
     CGPathAddLineToPoint(path_aux, NULL, self.size.width/2.0, 1.5*self.size.height/4.0); 
     CGPathAddLineToPoint(path_aux, NULL, self.size.width/2.0, -self.size.height/4.0); 
     CGPathCloseSubpath (path_aux); 
     self.auxiliaryShapeNode.anchorPoint = CGPointMake(-3.0, 1.0/2.5); 
     self.auxiliaryShapeNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path_aux]; 
     self.auxiliaryShapeNode.physicsBody.dynamic = YES; 
     self.auxiliaryShapeNode.physicsBody.categoryBitMask = solidCategory; 


     [self addChild:self.auxiliaryShapeNode]; 
     [self.scene.physicsWorld addJoint:[SKPhysicsJointFixed jointWithBodyA:self.physicsBody bodyB:self.auxiliaryShapeNode.physicsBody anchor:self.position]]; 
     break; 

où l'auto est un nœud de sprite personnalisé i créé qui a un noeud SKSprite comme la forme du corps auxiliaire de sorte que la forme peut être faite par deux polygones convexes, l'on crée les formes comme il est censé, mais quand le scène commence à jouer le joint ne fonctionne pas comme il est censé et déplace le petit triangle loin où il a commencé

Répondre

0

J'ai résolu mon problème, je n'ai pas trouvé la réponse mais je pense que vous pouvez dire que je l'ai travaillé autour

Ce que j'ai réalisé est que le point du Bodyjoint obéit seulement à la coordonnée x, mais a toujours déplacé la coordonnée y à 0, donc je me suis déplacé le corps à la coordonnée 0,0, et déplacé le corps de nouveau à ESt position précédente

CGPoint *prevPosition = self.position; 
    //positioning of the node so that the joint lands exactly 0,0 
    self.position = CGPointMake(-3*self.size.width/8.0,0); 

    CGPathMoveToPoint (path, NULL, self.size.width/4.0, self.size.height/4.0); 
    CGPathAddLineToPoint(path, NULL, self.size.width/2.0, -self.size.height/4.0); 
    CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, -self.size.height/4.0); 
    CGPathAddLineToPoint(path, NULL, -self.size.width/2.0, self.size.height/4.0); 
    CGPathCloseSubpath (path); 
    self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path]; 
    self.physicsBody.affectedByGravity = YES; 
    self.physicsBody.categoryBitMask = solidCategory; 
    self.physicsBody.dynamic = YES; 
    [self addChild:shape1]; 

    self.auxiliaryShapeNode = [[SKSpriteNode alloc] init]; 
    //now we asssign the frame, the position does not matter as it is determined by the joint 
    self.auxiliaryShapeNode.frame = CGRectMake(0,0,sel.size.width/8.0,self.height/2); 
    self.auxiliaryShapeNode.anchor = CGPointMake(0,0.5); 
    CGPathMoveToPoint (path_aux, NULL, 0,0); 
    CGPathAddLineToPoint(path_aux, NULL, self.auxiliaryShapeNode.size.width, self.auxiliaryShapeNode.size.width/2.0); 
    CGPathAddLineToPoint(path_aux, NULL, self.auxiliaryShapeNode.size.width, -self.auxiliaryShapeNode.size.width/2.0); 
    CGPathCloseSubpath (path_aux); 
    self.auxiliaryShapeNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path_aux]; 
    self.auxiliaryShapeNode.physicsBody.dynamic = YES; 
    self.auxiliaryShapeNode.physicsBody.categoryBitMask = solidCategory; 


    [self addChild:self.auxiliaryShapeNode]; 
    [self.scene.physicsWorld addJoint:[SKPhysicsJointFixed jointWithBodyA:self.physicsBody bodyB:self.auxiliaryShapeNode.physicsBody anchor:CGPointZero]]; 
    self.position=prevPosition; 

, je réussi à « faire fonctionner » de cette façon, l'espoir d'une manière utile pour quelqu'un

+0

Avez-vous eu des fuites de mémoire en utilisant ce code? Je crée des corps de physique avec un chemin de forme de polygones et Instruments me montre d'étranges fuites de mémoire dans PhysicsKit (le cadre responsable contient des informations sur les objets PKPoint). Ma mise en œuvre ressemble beaucoup à la vôtre, mais je n'ai pas de joints et après avoir créé le corps, j'appelle 'CGPathRelease (path)'. Je me demande si c'est un bug dans Sprite Kit ou si je fais quelque chose de mal. – Darrarski

+0

J'ai oublié d'ajouter que lorsque je crée des corps en utilisant les méthodes [SKPhysicsBody bodyWithCircleOfRadius: radius] 'ou' [SKPhysicsBody bodyWithRectangleOfSize: size] ', je n'ai pas de fuites de mémoire du tout. – Darrarski

+0

hmm ... peut-être que vous devriez partager votre code, je n'ai détecté aucune fuite de mémoire, et cela fonctionne encore assez bien ... peut-être que cela a quelque chose à voir avec la complexité de votre polygone J'ai utilisé un trapèze, mais si vous partagez votre code je serai heureux de vous aider si je peux – heczaco