majidarif
majidarif

Reputation: 20105

SpriteKit ball speed differ on size

I am creating a ball by SKShapeNode and CGPathAddArc.
The thing is if I use a larger radius the ball moves slower.
If I use a lower radius the ball moves faster.

// width depends on screen size (iphone, ipad)
// on iPhone the speed of the ball is something I like
// when on the ipad the speed is very slow
CGPathAddArc(ballPath, NULL, 0, 0, self.frame.size.width/80, 0, M_PI*2, YES);

How do I make it(movement speed) constant whatever the radius is?

_ball.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:ballPath];
_ball.physicsBody.friction       = 0.f;
_ball.physicsBody.restitution    = 1.002f;
_ball.physicsBody.linearDamping  = 0.f;
_ball.physicsBody.affectedByGravity = NO;
_ball.physicsBody.allowsRotation    = NO;

Upvotes: 2

Views: 658

Answers (1)

CharlieMezak
CharlieMezak

Reputation: 5999

Physics bodies have a mass property that determines how forces are translated into accelerations. Specifically, they follow Newton's law: F = ma. Since mass is inversely proportional to acceleration, balls with a smaller mass will have a greater acceleration in response to the same force.

When you create a physics body with a path, the body's mass is set to a value proportional to the area of that path. That's why your smaller balls have less mass.

But you can set the mass property to be whatever you like, regardless of the area of the body. Just set all of your balls to have some standard mass value and they'll respond the same way to the same forces.

The other way to modify the mass of a physics body would be to change its density property. Smaller, denser balls could have the same mass as larger, less dense ones. But it's probably simpler to just set mass directly.

Upvotes: 2

Related Questions