Physics Problems

Hello all.

I’m trying to use the physics API but I have problems keeping my dynamic objects steady before i even move them.

That is, following the SimpleTest app I  created a Box to use as the floor and a box to stand on the floor, NOT FALLING.

I tried to set the location of the box 1 unit above the floor but the box still falls from above. I also have a problem when loading

models and place them on a TerrainBlock. They keep rotating around without me applying any force on them.

Here’s some sample code:


        Level level = new BoxLevel("TestLevel", 100,1, 100);
        level.init();
        //where the floor is a Box
        level.getFloor().setLocalTranslation(new Vector3f(0, -10, 10));

        rootNode.attachChild(level.getFloor());
        world.addObject(new StaticPhysicsObject(level.getFloor()));

        Box boxGraphics = new Box("Box", new Vector3f(), 10f, 10f, 10f);
        boxGraphics.setLocalTranslation(new Vector3f(0, -9, 10));
        rootNode.attachChild(boxGraphics);

        DynamicPhysicsObject boxPhysics = new DynamicPhysicsObject(boxGraphics, 1f);
        world.addObject(boxPhysics);



Is there something that I'm missing? Shouldn't the default behaviour of a newly created
DynamicPhysicsObject be the same as a StaticPhysicsObject (provided that no forces or torque are set)?

Are you doing this at initialization time?  You might say, boxPhysics.syncWithGraphics() (or something to that effect, I don't have the code in front of me) to make sure it is in sync.  You might paste your entire contents of you class in here and it will provide a bit more information.



darkfrog

prowler7 said:
Shouldn't the default behaviour of a newly created
DynamicPhysicsObject be the same as a StaticPhysicsObject (provided that no forces or torque are set)?

No, as there is gravity. You can switch off gravity in the physics world of you like (just set it to (0,0,0)).

Is gravity your concern, or is there other behaviour that is unexpected?

Just tried to use syncWithGraphical() but makes no difference. After disabling gravity thebox never shows,

probably because it never falls down(so I've commented it out).

Heres the whole thing:


public class TestLevel extends SimpleGame {

    private PhysicsWorld world;

    /**
     * Called near end of initGame(). Must be defined by derived classes.
     */
    protected void simpleInitGame() {
        initPhysicsWorld();
        initLevel();
        initObjects();
    }

    private void initPhysicsWorld() {
        world = PhysicsWorld.create();
        world.setUpdateRate(100);
        world.setStepSize(2 / 100f);
        //world.setGravity(new Vector3f(0,0,0));
    }

    private void initLevel() {
        Box floor = createFloor();
        floor.setLocalTranslation(new Vector3f(0, -10, 10));

        rootNode.attachChild(floor);
        StaticPhysicsObject floorPhysicsObject = new StaticPhysicsObject(floor);
        world.addObject(floorPhysicsObject);
        floorPhysicsObject.syncWithGraphical();
    }

    private void initObjects() {
        Box boxGraphics = new Box("Box", new Vector3f(), 10f, 10f, 10f);
        boxGraphics.setLocalTranslation(new Vector3f(0, -9, 10));
        boxGraphics.setRenderState(createBoxTextureState());
        rootNode.attachChild(boxGraphics);

        DynamicPhysicsObject boxPhysics = new DynamicPhysicsObject(boxGraphics, 1f);
        world.addObject(boxPhysics);
        boxPhysics.syncWithGraphical();
    }

    protected void simpleUpdate() {
        PhysicsWorld.getInstance().update(tpf);
    }

    public static void main(String[] args) {
        TestLevel app = new TestLevel();
        app.setDialogBehaviour(BaseGame.ALWAYS_SHOW_PROPS_DIALOG);
        app.start();
    }

    protected Box createFloor() {
        Box floor = new Box("Floor", new Vector3f(), 100, 1, 100);
        floor.setModelBound(new BoundingBox());
        floor.updateModelBound();
        floor.setLightCombineMode(LightState.OFF);

        FloatBuffer tBuf = floor.getTextureBuffer();
        tBuf.clear();
        tBuf.put(0).put(20);
        tBuf.put(0).put(0);
        tBuf.put(20).put(0);
        tBuf.put(20).put(20);

        floor.setRenderState(createFloorTextureState());
        return floor;
    }

    protected TextureState createFloorTextureState() {
        Texture regTexture =
                TextureManager.loadTexture(
                        BoxLevel.class.getClassLoader().getResource(
                                "jmetest/data/texture/grass.jpg"),
                        Texture.MM_LINEAR_LINEAR,
                        Texture.FM_LINEAR);
        regTexture.setWrap(Texture.WM_WRAP_S_WRAP_T);

        TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        ts.setEnabled(true);
        ts.setTexture(regTexture);
        return ts;
    }

    protected TextureState createBoxTextureState() {
        Texture regTexture =
                TextureManager.loadTexture(
                        BoxLevel.class.getClassLoader().getResource(
                                "jmetest/data/images/monkey.jpg"),
                        Texture.MM_LINEAR_LINEAR,
                        Texture.FM_LINEAR);

        TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        ts.setEnabled(true);
        ts.setTexture(regTexture);
        return ts;
    }
}




I think your problem has to do with the fact that you're setting the position of your box at y=-9.0f and 10.0f tall, yet your floor is at y=-10.0f and only one tall.  This would make the box start embeded into the floor which will make it pop out with force applied to it because they are overlayed.



darkfrog

Yep! You are right darkfrog. Thanks!



But what about my models rotating? Does it have to do with the center of gravity of the model, cause

the model rotates around it's feet? If so, how can I change it?



EDIT: Is it obligatory to add a spatial to the scene before creating the physics object and adding to the world?



EDIT: Forcing the bounding volume to be a box instead of a sphere wheb using JmeBinaryReader seems to

solve the problem of rotation, BUT my model now lays down on it's back. What can I do to fix it?

Also it's my understanding that physics uses the bounding volumes in order to work, correct? So do I have

to change the location of the model inside the bounding box or something in order to get it working?

After the specified post

irrisor said:


Example from above:

irrisor said:
Create a sphere, attach it to a node, change the local translation to move the center of the sphere away from the center of the mass, then create a physics object from the node.

The center of mass is the point of reference of the spatial for which the DynamicPhysicsObject is created.
I wrote the following code and it seems to work correctly(only when the bounding volume of the model is a box).
Is that what I'm supposed to do or there's a better way of handling this?


        Node q2Model = null;
        try {
            q2Model = ResourceManager.getInstance().loadJmePlayerModel("ai");
            q2Model.setLocalTranslation(new Vector3f(-5, 0, 10));
            q2Model.setLocalScale(0.05f);

            //attach sphere and translate in order to change the center of mass
            Sphere sphere = new Sphere("Sphere", 10, 10, 10);
            q2Model.attachChild(sphere);
            sphere.setCullMode(Spatial.CULL_ALWAYS);
            sphere.setLocalTranslation(new Vector3f(0,10,0));
        } catch (Exception e) {
            e.printStackTrace();
        }
        rootNode.attachChild(q2Model);

        DynamicPhysicsObject boxPhysics = new DynamicPhysicsObject(q2Model, 1f);
        world.addObject(boxPhysics);
        boxPhysics.syncWithGraphical();

Yes, in fact I've had very similar problems recently having to do with the center of gravity on the model I loaded.  I've had specific problems with 3D Studio exporting models and them just being totally screwed up.  I found it seems to be a problem with scaling in 3DS.  Anyway, you'll either have to get the center of gravity correct before you export it or fix it after you import it.  I think someone can probably explain how to do it in jME.



darkfrog

darkfrog said:

I've had specific problems with 3D Studio exporting models and them just being totally screwed up.  I found it seems to be a problem with scaling in 3DS.

darkfrog


when do you scale the model as an object or as an editablemesh/poly sometimes depending where you dp your scaling strange things happen might also consider adjusting the pivot centers before exporting