I wanted an object to float on the screen, resisting gravity, not moving at all.
This is the gravity setting of the view.
self.physicsWorld.gravity = CGVector(dx: 0, dy: 5.0)
it's set to 5m/s^2 upwards. So object gets accelerated by 5m upwards per second.
Mass of the object is set to 1.0kg
self.physicsBody?.mass = 1.0
I applied a force to the object so it can resist the gravity. So I did the following.
func update(delta: TimeInterval) {
...
let force = CGVector(dx: 0.0, dy: -5.0)
self.physicsBody?.applyForce(force)
}
I applied -5N because I thought the gravitational force applied to the object is 1kg * 5m/s^2 = 5N. Applying -5N will make the object gets accelerated by -5m/s^2, floating on the screen as a result with the gravity.
But it did not work. Instead I had to do this.
let force = CGVector(dx: 0.0, dy: -5.0 * 150.0)
-5 multiplied by 150 is -750. So, where does this 150 come from? Why do I have to apply -750N instead of -5N to make the object resist gravity?
I also tested out different masses and forces on different gravity settings.
self.physicsBody?.mass = 2.0
let force = CGVector(dx: 0.0, dy: -5.0 * 150.0 * 2)
self.physicsWorld.gravity = CGVector(dx: 0, dy: 15.0)
self.physicsBody?.mass = 2.0
let force = CGVector(dx: 0.0, dy: -15.0 * 150.0 * 2)
and they all worked find. F=ma.
Question is the mysterious factor of 150. Where the hell does it come form?
OK, so it was all about wrong documentation of Apple. Here's the truth of 150.
this seems to be little bit stupid, but applyForce is measured in ((points * kilo) / sec ^ 2), but the gravity acceleration is in Newtons ((kilo * meter)/ sec ^ 2) (despite the fact it's described as meters per second in documentation. Meters per second! Acceleration!). Multiply it by mass and get the force.
https://stackoverflow.com/a/31868380/5752908
Come on, Apple... It's been out there for 4 years.