Brick not falling correctly

I have been trying to integrate physics into my new jme3 project. In trying to do so, I have run across a problem, most likely my own. If I place some object above another static object, trying to let gravity pull the object down to the static object, the object behaves erratically.



I have modified double1984's TestBrickWall code to demonstrate my problem. Here's the code:


import com.jme3.app.SimpleBulletApplication;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.nodes.PhysicsNode;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;

/**
 *
 * @author double1984
 */
public class TestBrickWall extends SimpleBulletApplication {

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

    Material mat;
    Material mat2;
    Material mat3;

    public void simpleInitApp(){
        cam.setLocation(new Vector3f(0, 10, 20));
        cam.lookAt(new Vector3f(0, 5, 0), Vector3f.UNIT_Y);
        flyCam.setMoveSpeed(10f);

        initMaterial();
        initFloor();
        initBrick();
    }

    public void initFloor(){
        Box floorBox = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);
        floorBox.scaleTextureCoordinates(new Vector2f(3, 6));

        Geometry floor = new Geometry("Floor", floorBox);
        floor.setMaterial(mat3);
        floor.setShadowMode(ShadowMode.Recieve);
        PhysicsNode floorNode = new PhysicsNode(floor, new BoxCollisionShape(new Vector3f(10f, 0.1f, 5f)), 0);
        floorNode.setLocalTranslation(0, -0.1f, 0);
        floorNode.updateGeometricState();
        rootNode.attachChild(floorNode);
        getPhysicsSpace().add(floorNode);

    }

    public void initBrick(){
        Box brick = new Box(new Vector3f(0, 10, 0), .5f, .5f, .5f);            
       
        Geometry geom = new Geometry("Brick", brick);
        geom.setMaterial(mat);

        PhysicsNode brickNode=new PhysicsNode(geom, new BoxCollisionShape(new Vector3f(.5f, .5f, .5f)), 1);
        brickNode.updateGeometricState();
        rootNode.attachChild(brickNode);
        getPhysicsSpace().add(brickNode);
    }

    public void initMaterial() {
        mat = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");
        TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
        key.setGenerateMips(true);
        Texture tex = assetManager.loadTexture(key);
        mat.setTexture("m_ColorMap", tex);

        mat2 = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");
        TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
        key2.setGenerateMips(true);
        Texture tex2 = assetManager.loadTexture(key2);
        mat2.setTexture("m_ColorMap", tex2);

        mat3 = new Material(assetManager, "Common/MatDefs/Misc/SimpleTextured.j3md");
        TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.png");
        key3.setGenerateMips(true);
        Texture tex3 = assetManager.loadTexture(key3);
        tex3.setWrap(WrapMode.Repeat);
        mat3.setTexture("m_ColorMap", tex3);
    }
}



If you run the program, you'll notice that the brick does not fall straight down to hit the floor. It seems to move up just a bit before falling a bit more and then stopping on top of thin air.

Since I see examples of much more complicated physics setups around here, I know that this is most likely my error. Can someone please show me where it is?

(I think the problem has something to do with the collision shape, because if I set the mass of the floor to one, both objects fall fine.)

With the latest versions you can attach debug shapes to the PhysicsNode via .attachDebugShape(assetManager), I guess its because of the off-center box you create.

Cheers,

Normen

normen said:

With the latest versions you can attach debug shapes to the PhysicsNode via .attachDebugShape(assetManager), I guess its because of the off-center box you create.
Cheers,
Normen


I added the code for both of the objects and I got this:



So the collision shape's coordinates don't match the geometry's coordinates. So then I added

brickNode.setLocalTranslation(0, 5, 0);

and everything works now:



Thank you for the suggestion, and it got me thinking in the right direction. I wish the physics node would take the coordinates from the geometry, then that code wouldn't be necessary.