6
\$\begingroup\$

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.

Any ideas? Thanks in advance!

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

2 Answers 2

5
\$\begingroup\$

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 :)

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

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .