SpriteKit球失去了所有的能量打墙,恢复= 1

如果我在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; } 

当碰撞速度足够小的时候(例如( CGVector )的CGVector),Sprite Kit物理引擎下的Box2d会将碰撞计算为非弹性的(例如,如果restitution为0,消除任何跳动),节点不会反弹。

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

在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 

你的冲动应该高于这个门槛,以恢复尊重。