Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

ios - Scaling Physics Bodies in Xcode Spritekit

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 success. How can I make the body scale with the size of the node?

CGPoint location = CGPointMake(randX, -self.frame.size.height - expander.size.height);

SKAction *moveAction = [SKAction moveTo:location duration:randDuration];

SKAction *expandAction = [SKAction resizeToWidth:(expander.size.width * 1.4) height:(expander.size.width * 1.4) duration:1.0];

SKAction *collapseAction = [SKAction resizeToWidth:(expander.size.width) height: (expander.size.height) duration:1.0];

SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^() {
    expander.hidden = YES;
}];

SKAction *expandCollapseAction = [SKAction repeatActionForever:[SKAction sequence:@[expandAction, collapseAction]]];

SKAction *moveExpandAction = [SKAction group:@[moveAction, expandCollapseAction]];

SKAction *moveExpanderActionWithDone = [SKAction sequence: @[moveExpandAction, doneAction ]];
[expander runAction:moveExpanderActionWithDone withKey: @"expanderMoving"];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

scaling physics body

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);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...