Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit ball loses all energy hitting wall, restitution=1

Tags:

ios

sprite-kit

If I apply an impulse of 1 in the y direction, the ball bounces back and forth without losing any energy. However, if the initial impulse is 0.5 or below, the ball loses all energy instantly when it hits the wall. Why is this happening? I have a pretty good understanding of the properties of the SKPhysicsBody class. Try this code to see if you get the same behavior on your computer.

-(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;
}
like image 342
Michael Avatar asked Feb 27 '14 03:02

Michael


People also ask

What is the coefficient of restitution of a ball in physics?

You are thinking along the correct lines mostly, in reality a ball will deform more if it impacts with a higher speed. However the coefficient of restitution shows how elestic or plastic a collision is. It is a ratio of velocityin/velocity out and is between 0 and 1 in most practical cases.

What happens to the energy lost when the ball hits the ground?

Therefore, when the ball hits the ground, there will be more KE available for conversion into sound, heat and the ball's deformation so more % PE will be lost. The percent of energy lost wouldnt increase, the percentage loss would stay the same.

How does momentum change when a ball hits a wall?

If you throw the ball at a wall with velocity v, so that its momentum is mv, then, in a perfectly elastic collision with the wall, it bounces back with momentum -mv (in reality, slightly less) and so have momentum change 2mv. That is "balanced" by the change in momentum of molecules in the wall.

What would happen if a bouncing ball hit a wall?

The final velocity of the wall and earth will be changed very little by a bouncing ball. If at all - the energy has to transfer through the wall to the floor and due to the large mass and inelasticity of the wall, most of the energy would likely just be absorbed and converted to heat at the impact site....


1 Answers

When the collision velocity is small enough (such as your CGVector of (0, 0.5)), Box2d underlying the Sprite Kit physics engine will compute the collision as inelastic (i.e. as if the restitution was 0, removing any bounciness), and the node will not bounce.

This is, per the Box2d documentation, to prevent jitter.

In the Box2d source code, you even have this line:

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

Your impulse should be above this threshold for the restitution to be respected.

like image 103
Batalia Avatar answered Oct 06 '22 11:10

Batalia