SpriteKit球失去所有能量击中墙壁,恢复原状= 1

时间:2014-02-27 03:14:53

标签: ios sprite-kit

如果我在y方向施加1的冲量,球会来回反弹而不会失去任何能量。但是,如果初始脉冲为0.5或更低,球在撞到墙壁时会立即失去所有能量。为什么会这样?我非常了解SKPhysicsBody类的属性。尝试使用此代码查看您是否在计算机上获得相同的行为。

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
    /* Setup your scene here */
    self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
    SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    self.physicsBody = borderBody;
    self.physicsBody.friction = 0.0f;
    self.backgroundColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];

    SKShapeNode *ball = [SKShapeNode node];
    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    [ball setStrokeColor:[UIColor blackColor]];
    CGPathMoveToPoint(pathToDraw, NULL, 0, 0);
    CGPathAddEllipseInRect(pathToDraw, NULL, CGRectMake(-16, -16, 32, 32));
    ball.path = pathToDraw;

    ball.position = CGPointMake(size.width / 2, size.height / 2);
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];
    ball.physicsBody.friction = 0.0f;
    ball.physicsBody.restitution = 1.0f;
    ball.physicsBody.linearDamping = 0.0f;
    ball.physicsBody.allowsRotation = NO;

    [self addChild:ball];

    [ball.physicsBody applyImpulse:CGVectorMake(0, 0.5)];
}
return self;
}

1 个答案:

答案 0 :(得分:3)

当碰撞速度足够小时(例如CGVector (0, 0.5)),Sprite Kit物理引擎底层的Box2d会将碰撞计算为无弹性(即好像restitution是0,消除任何弹性),节点不会反弹。

根据Box2d documentation,这是为了防止抖动。

在Box2d源代码中,您甚至可以使用以下行:

/// A velocity threshold for elastic collisions. Any collision with a relative linear
/// velocity below this threshold will be treated as inelastic.
#define b2_velocityThreshold            

你的冲动应该高于这个阈值,以便恢复原状。