2

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;
majidarif
  • 18,694
  • 16
  • 88
  • 133

1 Answers1

2

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.

CharlieMezak
  • 5,999
  • 1
  • 38
  • 54
  • 1
    What is the mathematical formula to convert density based on the screen ratio between iPhone and iPad? The iPad screen height is 1.802817 x the iPhone 5 screen height. I'd like to modify the density of my nodes accordingly but I'd rather not resort to empirically determining what density "feels right". – Edward Feb 14 '14 at 06:44
  • dude, just give all of your physics bodies the same mass. Same on both devices. Then they will behave the way you want regardless of their size. – CharlieMezak Feb 17 '14 at 18:25
  • Posted as it's own question here: http://stackoverflow.com/questions/21791255/spritekit-iphone-ipad-different-resolution-need-same-physics – Edward Feb 17 '14 at 20:45