Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SpriteKit: applyForce to physics body totally different effect depending on device model

Tags:

ios

sprite-kit

A friend and I are prototyping some stuff in SpriteKit. This is not my friend's first time using it but it is my first time.

We have a simple control of a square. You can run back and forth on a "ground" node and you can touch a jump button to "jump".

With a given set of numbers for mass, gravity, etc, the following

[self.physicsBody applyForce:CGVectorMake(0, 1500000)];

causes a normal looking jump (almost a small bunnyhop) when run on an iPad4 in iPhone mode (app is iPhone only for now), but on my 5S the jump is about 20 to 40 times higher and on a 4S the jump is literally like 100 or 200 times higher. (the "times higher" are a guesstimate based on visually watching, but the iPad looks normal, the 5S looks like a superman jump, and the 4S looks like superman ate his spinach and his wife put out before he jumped)

The exact same code is running on all devices. I am not sure where to look or what could be causing this.

like image 541
chadbag Avatar asked Jul 08 '15 13:07

chadbag


1 Answers

You should applyImpulse instead of applyForce if you are applying it instantaneously. Regarding applyForce, Apple docs state

"The acceleration is applied for a single simulation step (one frame)."

It could be the very small timing differences of the step is leading to large differences because the force is very large over just 1 frame. If you are using applyForce you should be doing it at each step in the simulation to keep the force applied over a period of time. Typically the motion of a jump is over a very short period of time so applyImpulse is often used.

Here is another quote from Apple docs regarding force vs impulse:

"A force is applied for a length of time based on the amount of simulation time that passes between when you apply the force and when the next frame of the simulation is processed. So, to apply a continuous force to an body, you need to make the appropriate method calls each time a new frame is processed. Forces are usually used for continuous effects An impulse makes an instantaneous change to the body’s velocity that is independent of the amount of simulation time that has passed. Impulses are usually used for immediate changes to a body’s velocity."

like image 66
Epic Byte Avatar answered Sep 18 '22 00:09

Epic Byte