Skip to content

RigidBodyControl doesn't read/write velocities #970

@stephengold

Description

@stephengold

The following test fails in master jme3-bullet because PhysicsRigidBody.write() doesn't serialize the linear and angular velocities and PhysicsRigidBody.read() doesn't set them. I haven't tested jme3-jbullet to see if it has this issue, but a quick code inspection leads me to believe it does.

This one looks like an easy 8-line fix.

Here's my test case:

package jme3test.bullet;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetNotFoundException;
import com.jme3.asset.ModelKey;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.export.JmeExporter;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.math.Matrix3f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import java.io.File;
import java.io.IOException;

/**
 * Test saving/loading a RigidBodyControl.
 *
 * @author Stephen Gold sgold@sonic.net
 */
public class TestIssueZZZ extends SimpleApplication {

    private int fileIndex = 0;

    public static void main(String[] args) {
        TestIssueZZZ app = new TestIssueZZZ();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        assetManager.registerLocator(".", FileLocator.class);

        CollisionShape shape = new SphereCollisionShape(1f);
        RigidBodyControl rbc = new RigidBodyControl(shape, 1f);
        setParameters(rbc);
        verifyParameters(rbc);
        RigidBodyControl rbcCopy = (RigidBodyControl) saveThenLoad(rbc);
        verifyParameters(rbcCopy);

        stop();
    }

    /**
     * Clone a body that implements Control by saving and then loading it.
     *
     * @param sgc the body/control to copy (not null, unaffected)
     * @return a new body/control
     */
    private PhysicsRigidBody saveThenLoad(PhysicsRigidBody body) {
        Control sgc = (Control) body;
        Node savedNode = new Node();
        /*
         * Add the Control to the Node without altering its physics transform.
         */
        Vector3f pl = body.getPhysicsLocation(null);
        Matrix3f pr = body.getPhysicsRotationMatrix(null);
        savedNode.addControl(sgc);
        body.setPhysicsLocation(pl);
        body.setPhysicsRotation(pr);

        String fileName = String.format("tmp%d.j3o", ++fileIndex);
        File file = new File(fileName);

        JmeExporter exporter = BinaryExporter.getInstance();
        try {
            exporter.save(savedNode, file);
        } catch (IOException exception) {
            assert false;
        }

        ModelKey key = new ModelKey(fileName);
        Spatial loadedNode = new Node();
        try {
            loadedNode = assetManager.loadAsset(key);
        } catch (AssetNotFoundException e) {
            assert false;
        }
        file.delete();
        Control loadedSgc = loadedNode.getControl(0);

        return (PhysicsRigidBody) loadedSgc;
    }

    private void setParameters(PhysicsRigidBody body) {
        body.setAngularVelocity(new Vector3f(0.04f, 0.05f, 0.06f));
        body.setLinearVelocity(new Vector3f(0.26f, 0.27f, 0.28f));
    }

    private void verifyParameters(PhysicsRigidBody body) {
        Vector3f w = body.getAngularVelocity();
        assert w.x == 0.04f : w;
        assert w.y == 0.05f : w;
        assert w.z == 0.06f : w;

        Vector3f v = body.getLinearVelocity();
        assert v.x == 0.26f : v;
        assert v.y == 0.27f : v;
        assert v.z == 0.28f : v;
    }
}

Metadata

Metadata

Assignees

Labels

PhysicsIssues with all kinds of Physics and their BindingsbugSomething that is supposed to work, but doesn't. More severe than a "defect".

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions