Box Problems

I’ve been trying to use the constructor for Box which takes two vectors. I’m having trouble getting that working. I’m making a ball pit toy program, based on the HelloPhysics tutorial. I create the walls, and they appear to go where I want them to go, but the projectiles just go right through. However, interestingly enough, when I shoot off into space, sometimes they’ll bounce off of thin air. So somewhere it must be getting confused about where the walls are.



I’ve read the documentation on the constructor for Box, but I don’t understand what it means.



“The minimum and maximum point are provided, these two points define the shape and size of the box but not it’s orientation or position.”



If you give it the minimum and maximum points on the box, how does that not define its location? And how do I tell it the location in such a way that the physics and graphics will agree on where it is?



I’m trying to avoid the constructor which takes a Vector and 3 floats, because I prefer to specify where the corners of a box go rather than the center and the size in both directions.



Source code:



[java]import com.jme3.app.;

import com.jme3.asset.
;

import com.jme3.bullet.;

import com.jme3.bullet.control.
;

import com.jme3.input.;

import com.jme3.input.controls.
;

import com.jme3.material.;

import com.jme3.math.
;

import com.jme3.renderer.queue.RenderQueue.;

import com.jme3.scene.
;

import com.jme3.scene.shape.;

import com.jme3.scene.shape.Sphere.
;

import com.jme3.texture.*;



/**

  • @author Scott

    *
  • @created at 1:28:29 PM on Jun 16, 2011

    */

    public class BallPit extends SimpleApplication {

    public static void main(String[] args) {

    new BallPit().start();

    }



    private BulletAppState bulletAppState;

    Material wallMat;



    @Override

    public void simpleInitApp() {

    bulletAppState = new BulletAppState();

    stateManager.attach(bulletAppState);



    wallMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");

    key.setGenerateMips(true);

    Texture tex = assetManager.loadTexture(key);

    wallMat.setTexture("ColorMap", tex);



    getCamera().setLocation(new Vector3f(0,10,0));

    getCamera().lookAt(new Vector3f(0,0,0), new Vector3f(0,0,1));

    createWall(new Vector3f(0,0,0), new Vector3f(10,1,10));

    createWall(new Vector3f(-1,0,10), new Vector3f(11,5,11));

    createWall(new Vector3f(-1,0,-1), new Vector3f(11,5,0));

    createWall(new Vector3f(-1,0,0), new Vector3f(0,5,10));

    createWall(new Vector3f(10,0,0), new Vector3f(11,5,10));



    inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

    inputManager.addListener(actionListener, "shoot");

    }



    private void makeCannonBall() {

    Material stoneMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");

    key2.setGenerateMips(true);

    Texture tex2 = assetManager.loadTexture(key2);

    stoneMat.setTexture("ColorMap", tex2);





    Sphere sphere = new Sphere(32,32,.1f, false, false);

    sphere.setTextureMode(TextureMode.Projected);

    Geometry ballGeo = new Geometry("cannon ball", sphere);

    ballGeo.setMaterial(stoneMat);

    rootNode.attachChild(ballGeo);

    ballGeo.setLocalTranslation(cam.getLocation());

    ballGeo.setShadowMode(ShadowMode.CastAndReceive);

    RigidBodyControl ballPhy = new RigidBodyControl(.1f);

    ballGeo.addControl(ballPhy);

    bulletAppState.getPhysicsSpace().add(ballPhy);

    ballPhy.setLinearVelocity(cam.getDirection().mult(25));

    }



    private ActionListener actionListener = new ActionListener() {

    public void onAction(String name, boolean on, float tpf) {

    if ( name.equals("shoot") && on ) {

    makeCannonBall();

    }

    }

    };



    private void createWall(Vector3f v1, Vector3f v2) {

    Box box = new Box(v1, v2);

    Geometry wall = new Geometry("Wall", box);

    wall.setMaterial(wallMat);

    rootNode.attachChild(wall);



    RigidBodyControl r = new RigidBodyControl(0);

    wall.addControl®;

    bulletAppState.getPhysicsSpace().add(wall);

    }

    }

    [/java]

The box is a mesh and not the geometry (object) thus it has no location or rotation.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

OK, thanks. I got it working. Here’s the correct source code in case someone else had the same problem:



[java]import com.jme3.app.;

import com.jme3.asset.
;

import com.jme3.bullet.;

import com.jme3.bullet.control.
;

import com.jme3.input.;

import com.jme3.input.controls.
;

import com.jme3.material.;

import com.jme3.math.
;

import com.jme3.renderer.queue.RenderQueue.;

import com.jme3.scene.
;

import com.jme3.scene.shape.;

import com.jme3.scene.shape.Sphere.
;

import com.jme3.texture.*;



/**

  • @author Scott

    *
  • @created at 1:28:29 PM on Jun 16, 2011

    */

    public class BallPit extends SimpleApplication {

    public static void main(String[] args) {

    new BallPit().start();

    }



    private BulletAppState bulletAppState;

    Material wallMat;



    @Override

    public void simpleInitApp() {

    bulletAppState = new BulletAppState();

    stateManager.attach(bulletAppState);



    wallMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");

    key.setGenerateMips(true);

    Texture tex = assetManager.loadTexture(key);

    wallMat.setTexture("ColorMap", tex);



    getCamera().setLocation(new Vector3f(0,10,0));

    getCamera().lookAt(new Vector3f(0,0,0), new Vector3f(0,0,1));

    createWall(new Vector3f(0,0,0), new Vector3f(10,1,10));

    createWall(new Vector3f(-1,0,10), new Vector3f(11,5,11));

    createWall(new Vector3f(-1,0,-1), new Vector3f(11,5,0));

    createWall(new Vector3f(-1,0,0), new Vector3f(0,5,10));

    createWall(new Vector3f(10,0,0), new Vector3f(11,5,10));



    inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

    inputManager.addListener(actionListener, "shoot");

    }



    private void makeCannonBall() {

    Material stoneMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");

    key2.setGenerateMips(true);

    Texture tex2 = assetManager.loadTexture(key2);

    stoneMat.setTexture("ColorMap", tex2);





    Sphere sphere = new Sphere(32,32,.2f, false, false);

    sphere.setTextureMode(TextureMode.Projected);

    Geometry ballGeo = new Geometry("cannon ball", sphere);

    ballGeo.setMaterial(stoneMat);

    rootNode.attachChild(ballGeo);

    ballGeo.setLocalTranslation(cam.getLocation());

    ballGeo.setShadowMode(ShadowMode.CastAndReceive);

    RigidBodyControl ballPhy = new RigidBodyControl(.1f);

    ballGeo.addControl(ballPhy);

    bulletAppState.getPhysicsSpace().add(ballPhy);

    ballPhy.setLinearVelocity(cam.getDirection().mult(25));

    }



    private ActionListener actionListener = new ActionListener() {

    public void onAction(String name, boolean on, float tpf) {

    if ( name.equals("shoot") && on ) {

    makeCannonBall();

    }

    }

    };



    private void createWall(Vector3f v1, Vector3f v2) {

    Vector3f center = v1.add(v2).multLocal(.5f);

    Vector3f diff = v2.subtract(v1).multLocal(.5f);



    Box box = new Box(Vector3f.ZERO, diff.x, diff.y, diff.z);

    Geometry wall = new Geometry("Wall", box);

    wall.setLocalTranslation(center);

    wall.setMaterial(wallMat);

    rootNode.attachChild(wall);



    RigidBodyControl r = new RigidBodyControl(0);

    wall.addControl®;

    bulletAppState.getPhysicsSpace().add(wall);

    }

    }

    [/java]