Weird position shift when applying forces...

JPB18
Posts: 2
Joined: Tue Jun 28, 2016 3:15 pm

Weird position shift when applying forces...

Post by JPB18 »

Hey all!

I'm currently trying to make a ball in a straight line using the bullet version included with libGDX (therefore, Java). I've a very weird issue where the same ball, set at the origin is moved to a wrong position when applied a force and when issued a simulation step. The sphere has been recently created.

For exemple, here's what the log returns:
07-06 16:21:12.160 8285-9100/pt.bluecover.wearable3d I/System.out: UPDATE: Sphere is @ (0.0,0.0,0.0) @ 0.011
07-06 16:21:12.160 8285-9100/pt.bluecover.wearable3d I/System.out: UPDATE: Acceleration is (-9.780663,-0.6153471,0.0)
07-06 16:21:12.160 8285-9100/pt.bluecover.wearable3d I/System.out: UPDATE: Linear Velocity is (0.0,0.0,0.0)
07-06 16:21:12.160 8285-9100/pt.bluecover.wearable3d I/System.out: DELTA TIME: 0.01650481
07-06 16:21:12.170 8285-9100/pt.bluecover.wearable3d I/System.out: UPDATE: Sphere is @ (0.2504366,0.014636279,0.0) @ 0.018
07-06 16:21:12.170 8285-9100/pt.bluecover.wearable3d I/System.out: UPDATE: Acceleration is (-9.780663,-0.6153471,0.0)
07-06 16:21:12.170 8285-9100/pt.bluecover.wearable3d I/System.out: UPDATE: Linear Velocity is (-0.08149039,-0.0053867875,0.0)
07-06 16:21:12.170 8285-9100/pt.bluecover.wearable3d I/System.out: DELTA TIME: 0.014863175
And here's the code, so you can have a point of reference:

Code: Select all


          if(starting) {
            startTime = TimeUtils.millis();
            starting = false;
            return;
           }

        double miliTime = TimeUtils.millis();
        double time = (miliTime - startTime) / 1000;

        //get sphere position
        Vector3 position = new Vector3();
        sphereInstance.transform.getTranslation(position);
        System.out.println("UPDATE: Sphere is @ " + position + " @ " + time);

        spawnTrailPoint(position, time);

        if(loader != null) try {

            DataSet curr = loader.find(time);
            Vector3 acc = curr.acceleration.cpy().scl(9.8f); //acceleration is a 3D vector
            System.out.println("UPDATE: Acceleration is " + acc);
            System.out.println("UPDATE: Linear Velocity is " + sphereInstance.body.getLinearVelocity());
            Quaternion orientation = sphereInstance.body.getOrientation(); //the body attribute is a btRigidBody type
            sphereInstance.body.applyCentralForce(acc);
        } catch (NoMoreElementsException ex) {
            (Stuff to finish movement here)
        }

        float dt = Gdx.graphics.getDeltaTime(); //get time before last frame.
        System.out.println("DELTA TIME: " + dt);
        dynamicsWorld.stepSimulation(dt, 10, 1f/60f);
So this movement seems to be fairly erratic... Why would the ball move in that direction, teleport fashioned, when I apply a force in the oposite direction?

Also, if you need me to add more code, please just say so!
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland

Re: Weird position shift when applying forces...

Post by benelot »

I do not see where you apply the force to move the sphere. Furthermore you should not step the physics with the time the graphics has used. If the graphics is slow, the physics will not be able to keep up. On this issue, you should read the fix your timestep article by gafferongames. You can search the forum for the TimeWarp example that is soon to be part of the ExampleBrowser. I already posted it here for people to use it before it is finally in the browser.

We will see what is wrong with your simulation. You calculated inertia for your sphere when creating it? Otherwise you get weird behavior.