Scaling Physics Bodies in Xcode Spritekit

后端 未结 2 406
终归单人心
终归单人心 2020-12-10 19:38

I recently came across an issue where I would increase the size of a node, but the physics body would remain the same size. I tried to look up solutions to this with no succ

相关标签:
2条回答
  • 2020-12-10 19:52

    this is strange you can do it add physics body before adding it to the SKNode or SKScene for e.g.

    SKTexture  *texture=[_gameTexture textureNamed:@"mysprite"];
    SKSpriteNode *bubble=[SKSpriteNode spriteNodeWithTexture:texture];
    bubble.anchorPoint=CGPointMake(0.5,0.5);
    //_bubble.name=[@"bubble" stringByAppendingFormat:@"%d ",index];
    bubble.name=newcolor;
    

    // _bubble.userInteractionEnabled=YES; _bubble.position=CGPointMake(newPosition.x, newPosition.y);

    //
    bubble.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:60];
    bubble.physicsBody.dynamic=NO;
    bubble.physicsBody.linearDamping=0;
    bubble.physicsBody.restitution=0;
    
    
    bubble.physicsBody.density=1;
    bubble.physicsBody.allowsRotation=NO;
    bubble.physicsBody.affectedByGravity=NO;
    //_bubble.physicsBody.velocity=CGVectorMake([self getRandomNumberBetween:-10 to:7], [self getRandomNumberBetween:-10 to:7]);
    //  _bubble.physicsBody.usesPreciseCollisionDetection=YES;
    
    
    bubble.physicsBody.categoryBitMask=ballCategory;
    bubble.physicsBody.collisionBitMask=ballCategory | pathCategory ;
    bubble.physicsBody.contactTestBitMask=ballCategory | pathCategory ;
    
    [self addChild:_bubble];
    
    
    bubble.xScale=_bubble.yScale=2;
    bubble.alpha=0.5;
    
    0 讨论(0)
  • 2020-12-10 20:01

    I thought this won't work, but it looks like that physics body is scaled after all. Here is the code which can reproduce physics body scaling on iOS8 :

    - (void)didMoveToView:(SKView *)view {
      /* Setup your scene here */
    
        SKSpriteNode *s = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(100,100)];
        s.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame)+100);
        s.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:s.size];
    
        s.physicsBody.dynamic = YES;
        
        s.physicsBody.affectedByGravity = NO;
    
        [self addChild:s];
        
        [s runAction:[SKAction scaleTo:0.3f duration:5]];
    
        [s.physicsBody applyImpulse:CGVectorMake(0,30)];
      
    }
    

    If I recall correctly , this wasn't worked earlier, but I just tried it and it looks like it work. Still, this is not fully tested and I am not sure if this will mess up the physics simulation. Contact detection will probably work though. I just wanted to show that actual physics bodies are scaled when sprite is scaled . Here is the result:

    From the gif above it can be seen that physics body is scaled along with sprite (without re-creating another different sized physics body). The blue bounding box around the sprite is visual representation of sprite's physics body (enabled in view controller with skView.showsPhysics = YES);

    0 讨论(0)
提交回复
热议问题