2
\$\begingroup\$

When my player jumps, I start a rotation and I want to finish it when he gets back on the ground.

My Player is jumping off the ground and landing back on in 1 second.

My physics are updating at 10 FPS (10 times per second).

So I want to complete a 180 degree rotation for exactly 1 second. I add 18 degree to my player at every world update. I convert the degrees to radians, but the result is this:

enter image description here

I want him to jump with a 0 degree angle, rotate and land with 0 degree angle. Here is my code:

world.step(step, 8, 3);

if(isJumping) {
    angle -= (18*Math.PI)/180; // conver degrees to radians
    player.body.setTransform(player.body.getPosition(), angle);
}
\$\endgroup\$
  • \$\begingroup\$ Or option 3, keep as is. I think it kind of looks cool :P \$\endgroup\$ – Coburn Oct 11 '16 at 18:52
1
\$\begingroup\$

By your logic you're applying your math correctly. However, I doubt that it's really taking the rectangle 1 second to jump and also that your engine is giving a constant 10 fps.

Your physics framerate timed with your jump event will fluctuate based on a ton of different things (when the thread is scheduled, how the engine prioritizes it, PC load, etc) and I don't think you should rely on a specific amount of physics frames to rotate your character.

  • If you want, you can cheat the effect without using physics. Make the rotation an animation such that it will always complete its rotation regardless of the physics frames. I think that Impossible Game uses this effect

Impossible game gif

  • You can set the rotational angle to the desired amount once you hit the ground and tweak the amount your rotating by to make it look smooth. This is less desirable if your physics fps ever becomes really unstable but if you need the physics engine to know about your rotation that can't be done from the animation, I'd do this.
\$\endgroup\$
  • \$\begingroup\$ I use interpolation for rendering the position of the character, so I tough I can use interpolation for rendering angle also. I implemented fixed timestep and interpolation as the famous article "Fix your timestep!". Also I track the time the player is in the air with custom contact listener. It is 1000ms, but sometimes 998 or 1002. The animation idea is good, but how to make it look smooth on 30fps, 60fps and maybe 75fps at the same time? \$\endgroup\$ – Sharpirate Oct 11 '16 at 19:12
  • \$\begingroup\$ Sounds like you've pretty thoroughly tested this then. With a fixed timestep that is some odd behavior. Are you sure it's not just taking one frame extra for your timesteps because it only looks like its rotating one extra increment of 18 degrees? You could also just do if rotation+18 > 180, don't continue rotating. Lastly, I was thinking an animation solely on the rotation, not an actual raster animation. It might be a bit of a headache to try to do it in a raster format. \$\endgroup\$ – Coburn Oct 11 '16 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.