SpriteKit ball speed differ on size

爷,独闯天下 提交于 2019-12-11 05:15:26

问题


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;

回答1:


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.



来源:https://stackoverflow.com/questions/21499148/spritekit-ball-speed-differ-on-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!