4
\$\begingroup\$

So I'm trying to create a simple physics engine for the game I'm creating. I'm trying to add jumping to the game. To do this I used (where jumpPower is 30000):

impulses.emplace_back(0.0f, mass * World::getGravityForce() + jumpPower, 0.0f);

This creates a nice jump effect when using vsync, but when I turn vsync off the jump is low. This is caused by the fact that in lower fps rates, the velocity is less affected by gravity. In the code below, how could I fix this issue? (In a generalized way, so not just using the velocity vector instead)

A possibility could be updating physics at a fixed rate, but I don't know if that is the best solution.

At the moment forces is filled with a downward gravity, applied each frame and impules is filled with a jump force as soon as the player presses Space. No other forces are used and only this code affects force/velocity/acceleration.

for (vec3f& f : forces)
    force += f;

for (vec3f& f : impulses)
    force += f;

impulses.clear();

//Add movement forces
velocity += movementVec/mass * delta;

vec3f new_acceleration = force / mass;
velocity += delta * (acceleration + new_acceleration) * 0.5;
acceleration = new_acceleration;
\$\endgroup\$
0
1
\$\begingroup\$

The best solution was to Use a fixed timestep:

Fix your timestep

\$\endgroup\$

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.