[SOLVED] Why set framerate to 10 then the sphere fall down slow?

I am a Server dev, I need run a world on server with 10 frame per seconds. But a sphere fall down need 6 seconds. setFrameRate to 60. It is just 1.9 seconds.

FrameRate seconds
5 6
10 3
60 1.9

Is something I was miss setting?

First create a simpleApplication TestCollision.java


import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.MeshCollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Quad;
import com.jme3.scene.shape.Sphere;
import com.jme3.system.AppSettings;
import test.aabb.AxisAppState;

import java.text.MessageFormat;

public class TestCollision extends SimpleApplication {
    private Geometry sphere;
    private PhysicsSpace space;
    public static long startDt;
    @Override
    public void simpleInitApp() {
        cam.setLocation(new Vector3f(0f, 60f, 40f));
        cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
        viewPort.setBackgroundColor(ColorRGBA.White);

        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Green);

        this.sphere = new Geometry("sphere", new Sphere(20, 40, 1));
        sphere.setMaterial(mat);
        sphere.move(0, 20, 0);

        mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Pink);

        Geometry floor = new Geometry("floor", new Quad(30, 30));
        floor.rotate(-FastMath.HALF_PI, 0f, 0f);
        floor.center();
        floor.setMaterial(mat);


        rootNode.attachChild(floor);
        rootNode.attachChild(sphere);

        BulletAppState bullet = new BulletAppState();
        stateManager.attach(bullet);
        space = bullet.getPhysicsSpace();
        RigidBodyControl rigid = new RigidBodyControl(new MeshCollisionShape(new Quad(30, 30)), 0);
        floor.addControl(rigid);
        space.add(rigid);

        MyRigidControl rigid0 = new MyRigidControl(new SphereCollisionShape(1), 10);
        sphere.addControl(rigid0);
        space.add(rigid0);

        System.out.println((startDt = System.currentTimeMillis()));
    }

    @Override
    public void simpleUpdate(float tpf) {
    }

    public static void main(String[] args) {
        TestCollision test = new TestCollision();
        test.settings = new AppSettings(true);
        test.settings.setFrameRate(5);
        test.setShowSettings(false);
        test.start();
    }
}

Then create you owner RigidControl MyRigidControl


import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;

/***
 *
 *
 * @author qiunet
 * 2021/10/15 16:34
 **/
public class MyRigidControl extends RigidBodyControl implements PhysicsCollisionListener {

    public MyRigidControl(CollisionShape shape, float mass) {
        super(shape, mass);
    }

    @Override
    public void setPhysicsSpace(PhysicsSpace newSpace) {
        newSpace.addCollisionListener(this);
        super.setPhysicsSpace(newSpace);
    }

    @Override
    public void collision(PhysicsCollisionEvent event) {
        System.out.println("ms = " + (System.currentTimeMillis() - TestCollision.startDt));
    }
}
1 Like

BulletAppState has a limit on the number of physics steps per frame. By default, this limit is 4. So by running at 5 frames per second, you are limiting the physics to 20 steps per second.

The default physics step is 0.0166667 seconds, so at 5 fps your simulation is running at about 1/3 speed.

Since you’re developing a server, you probably don’t want to use BulletAppState. For hints on using JMonkeyEngine to simulate physics without appstates, see the following webpage:

Physics simulation without appstates :: The Minie project

2 Likes

Thank you !

1 Like