How to delay physics engine ticks to match application frame count?

I am trying to make the physics engine wait for the application frame to match its frame/step/tick count.

My application is geared towards 60 fps (as jbullet is too).

Yes, if the application frame drops, it is for jbullet get slower too, thats the idea here.

if I count the frames and ticks and try to match them by thread sleeping at prePhysicsTick(), the application will simply wait too, it seems to be waiting the prePhysicsTick() to complete…

Any idea what I could do?

Ok I’m not 100% sure this will work Bullet, but to achieve the same thing with animation I created a custom Timer-

public class CustomTimer extends Timer {
    
    private static final long TIMER_RESOLUTION = 1000000000L;
    private static final float INVERSE_TIMER_RESOLUTION = 1f/1000000000L;
    
    private long startTime;
    private long previousTime;
    private float tpf;
    private float fps;
    
    public CustomTimer() {
        startTime = System.nanoTime();
    }

    /**
     * Returns the time in seconds. The timer starts
     * at 0.0 seconds.
     *
     * @return the current time in seconds
     */
    @Override
    public float getTimeInSeconds() {
        return getTime() * INVERSE_TIMER_RESOLUTION;
    }

    public long getTime() {
        return System.nanoTime() - startTime;
    }

    public long getResolution() {
        return TIMER_RESOLUTION;
    }

    public float getFrameRate() {
        return fps;
    }

    public float getTimePerFrame() {
        //return tpf;//stops it from running at different speeds
        return 0.016f;
    }

    public void update() {
        tpf = (getTime() - previousTime) * (1.0f / TIMER_RESOLUTION);
        fps = 1.0f / tpf;
        previousTime = getTime();
    }
    
    public void reset() {
        startTime = System.nanoTime();
        previousTime = getTime();
    }
}

This is literally a copy pasted file, you can see I’ve commented out a bit and made it return 0.016. Then I create an instance and

application.setTimer(myTimer);

This means if the game slows down, say from 60 to 30fps, the animations run at half speed. (I’m making the assumption this is ok for you since you want physics to follow the framerate exactly - anything that uses timing will also run in a similar way, which is what I was going for when I made it)

Providing bullet is using the same timer, it might work…? Just a shot in the dark, who knows there might be a much easier way :slight_smile:

1 Like

you can override this behaviour of bullet, see the documentation of the update/step method.

Eg. in case of my server I have fixed 20fps, and thus step 50 ms always. If the server overloads, everything just slows down.

@JESTERRRRRR cool, the bullet tick count now it always less than the frame time, I think it will help a lot thx!

Btw, I think you can replace it by this:

public class NanoTimerCustom extends NanoTimer {
  @Override
  public float getTimePerFrame() {
      //return tpf;
      return 0.016f; //framte time for 60fps
  }
}

@Empire_Phoenix mmm, I just read the BulletAppState.update() code, it seems I just need to override and pass 0.016 to it, I will try as soon I wakeup (:D), thx!

PS.: Btw, I tried also to find the JBullet sources, but I cant be sure the JBullet.jar I have here (jme31) matches the 2 different sources I found, it seems a bit hard to find the matching sources and javadoc for it :(, the forum threads here related to jbullet sources, most have broken links, any idea?

Actually a pretty good question, we have 2 mistery jars in the lib folder on github, probably @normen can share some insight, where are they from.

1 Like

I hope this can help you understand how sources are stored :

jme3-bullet have two parts :

  • /src/common define classes used by both jbullet & bullet-native
  • /src/main is for “native” bullet (only java classes)
    jme3-jbullet is the java “biding” for jBullet (almost the same as jme3-bullet/src/main )
    jme3-bullet-native is for JNI - C++ for native binding of Bullet lib

Hm, actually the source for “our” jbullet was here: https://code.google.com/p/jme-jbullet/source/browse/#svn%2Fbranches

But it seems “archiving” in googlecode means that all branches get deleted :confused:

Ouch, hopefully someone has still a checkout lying around?

I guess I do yeah

1 Like