# Physics in my game confused after restructuring the Game loop

I'm on my way with making a game in Java. Now I have some trouble with an interpolation based game loop in my calculations.

Before I used that system the calculation of a falling object was like this:

• Delta based system

private static final float SPEED_OF_GRAVITY = 500.0f;

@Override
public void update(float timeDeltaSeconds, Object parentObject) {

parentObject.y = parentObject.y +
(parentObject.yVelocity * timeDeltaSeconds);

parentObject.yVelocity -= SPEED_OF_GRAVITY  * timeDeltaSeconds;
......


What you see here is that I used that delta value from previous frame to the current frame to calculate the physics. Now I switched and implement a interpolation based system and I actually left the current system where I used delta to calculate my physics.

However, with the interpolation system the delta time is removed - but now are my calculations screwed up and I've tried the whole day to solve this:

• Interpolation based system

private static final float SPEED_OF_GRAVITY = 500.0f;

@Override
public void update(Object parentObject) {

parentObject.y = parentObject.y +
(parentObject.yVelocity);

parentObject.yVelocity -= SPEED_OF_GRAVITY;
......


I'm totally clueless - how should this be solved? The rendering part is solved with a simple prediction method. With the delta system I could see my object be smoothly rendered to the screen, but with this interpolation/prediction method the object just appear sticky for one second and then it's gone.

The core of this game loop is actually from here deWiTTERS Game Loop, where I trying to implement the last solution he describes.

Shortly - my physics are in a mess and this need to be solved.

• Shouldn't you be working on exposing government corruption and solving your personal sexual assault case? :p – ShrimpCrackers Jan 4 '11 at 5:12
• ShrimpCrackers ... :-D – Notabene Jan 4 '11 at 9:42
• @ShrimpCrackers: First I must code my super game so I can earn money and escape from the earth. – Curtain Jan 4 '11 at 10:22
• You should always use delta time in calculations, even if your framerate is fixed. – Mephistofeles Feb 11 '14 at 23:35

Shortly put, your gravity speed is very, very different in the two systems.

In the first, your delta time is probably around a few milliseconds (let's say 0.15 seconds for simplicity's sake).

You are adding 500 * 0.15 to the y position which is 75.

In the second, you're adding 500. Always. And increasing the velocity by 500 in each update is going to cause MASSIVE problems! So for the second reduce your gravity speed drastically, and see what happens.

Hope that helps :)

• Thanks. Will test this tomorrow since I'm at a different computer. I let you know then. – Curtain Jan 4 '11 at 1:05
• Hm, the rendering become kinda sticky when I changed the gravity. Like the prediction is messed up, although I followed the dewitters guide... – Curtain Jan 4 '11 at 13:10

As pointed out by Ray, you need to adjust the gravity. I'd like to add that your life will be easier if you adapt metric units to your game. That means:

• In the coordinate system 1.0 should equal one meter
• For time 1.0 should equal one second
• For mass 1.0 should equal one kilogram

If you follow these rules, you can intuitively relate values from the game physics to real world units. What also follows is that you can use 9.81 for the gravity constant to get natural gravity.