Sphere passes through boxes

Ok - so I’m pretty new. I’ve been working with jMonkeyEngine for about a week. Having working with AndEngine previously, jMonkeyEngine is very, very impressive!!



Having said that, I’m learning by trying to build a game similar to Doodle Jump. I used the Physics tutorial as a starting point (where you shoot the cannon ball). I took out the brick wall, and instead created 3 barrier walls (a back wall, and a left and right side). Then I created a sphere (jumper). You click the right mouse button to jump (propels sphere upward). You can also use the arrow keys to nudge the sphere left and right.



The problem is, if you nudge the sphere, it rolls right through the barrier walls. I created the walls as RigidBodyControls with a value of 0.0f.



Any ideas why this would be happening? My source code is posted below.



Thanks!

Bob


[java]package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.font.BitmapText;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Sphere.TextureMode;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;

/**
* Example 12 - how to give objects physical properties so they bounce and fall.
* @author base code by double1984, updated by zathras
*/
public class Main extends SimpleApplication
{
public static void main(String args[]) {
Main app = new Main();
app.start();
}

/** Prepare the Physics Application State (jBullet) */
private BulletAppState bulletAppState;

/** Prepare Materials */
Material wall_mat;
Material rabbit_mat;
Material floor_mat;

/** Prepare geometries and physical nodes for bricks and cannon balls. */
private RigidBodyControl wall_phy, ls_wall_phy, rs_wall_phy;
private static final Box box, ls_box, rs_box;
private RigidBodyControl rabbit_phy;
private static final Sphere sphere;
private RigidBodyControl floor_phy;
private static final Box floor;
private Geometry rabbit_geo;

/** dimensions used for bricks and wall */
private static final float wallLength = 0.48f;
private static final float wallWidth = 0.24f;
private static final float wallHeight = 0.12f;

private Boolean is_game_running = false;

static {
/** Initialize the cannon ball geometry */
sphere = new Sphere(32, 32, 0.4f, true, false);
sphere.setTextureMode(TextureMode.Projected);

/** Initialize the wall geometries */
box = new Box(Vector3f.ZERO, wallLength, wallHeight, wallWidth);
box.scaleTextureCoordinates(new Vector2f(1f, .5f));

ls_box = new Box(Vector3f.ZERO, wallLength, wallHeight, wallWidth);
ls_box.scaleTextureCoordinates(new Vector2f(1f, .5f));

rs_box = new Box(Vector3f.ZERO, wallLength, wallHeight, wallWidth);
rs_box.scaleTextureCoordinates(new Vector2f(1f, .5f));

/** Initialize the floor geometry */
floor = new Box(Vector3f.ZERO, 10f, 0.1f, 5f);
floor.scaleTextureCoordinates(new Vector2f(3, 6));
}

@Override
public void simpleInitApp()
{
/** Set up Physics Game */
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
// bulletAppState.getPhysicsSpace().enableDebug(assetManager);

// Disable the default flyby cam
flyCam.setEnabled(false);

/** Initialize the scene, materials, and physics space */
initMaterials();
initWalls();
initFloor();
initRabbit();

// Enable a chase cam for this target (typically the player).
ChaseCamera chaseCam = new ChaseCamera(cam, rabbit_geo, inputManager);
chaseCam.setSmoothMotion(true);

/** Add InputManager action: Left click triggers shooting. */
inputManager.addMapping("jump", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("nudge-right", new KeyTrigger(KeyInput.KEY_RIGHT));
inputManager.addMapping("nudge-left", new KeyTrigger(KeyInput.KEY_LEFT));

inputManager.addListener(actionListener, "jump");
inputManager.addListener(actionListener, "nudge-right");
inputManager.addListener(actionListener, "nudge-left");
}

/**
* Map the actions to functions that will implement those actions
*/
private ActionListener actionListener = new ActionListener()
{
public void onAction(String name, boolean keyPressed, float tpf)
{
if(name.equals("jump"))
{
is_game_running = true;

jumpRabbit();
}

if(is_game_running)
{
if(name.equals("nudge-right"))
{
nudgeRabbit("right");
}

if(name.equals("nudge-left"))
{
nudgeRabbit("left");
}
}
}
};

/** Initialize the materials used in this scene. */
public void initMaterials()
{
wall_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
wall_mat.setColor("Color", ColorRGBA.Blue);

rabbit_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
rabbit_mat.setColor("Color", ColorRGBA.Red);

floor_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.jpg");
key3.setGenerateMips(true);
Texture tex3 = assetManager.loadTexture(key3);
tex3.setWrap(WrapMode.Repeat);
floor_mat.setTexture("ColorMap", tex3);
}

/** Make a solid floor and add it to the scene. */
public void initFloor()
{
Geometry floor_geo = new Geometry("Floor", floor);
floor_geo.setMaterial(floor_mat);
floor_geo.setLocalTranslation(0, -0.1f, 0);
this.rootNode.attachChild(floor_geo);
/* Make the floor physical with mass 0.0f! */
floor_phy = new RigidBodyControl(0.0f);
floor_geo.addControl(floor_phy);
bulletAppState.getPhysicsSpace().add(floor_phy);
}

/** This loop builds a wall out of individual bricks. */
public void initWalls()
{
// Create back (primary) wall
/** Create a cube geometry and attach to scene graph. */
Geometry wall_geo = new Geometry("wall", box);
wall_geo.setLocalTranslation(0, 12, 0);
wall_geo.setMaterial(wall_mat);
wall_geo.scale(10/*width*/,100/*height*/,0.5f/*thickness*/);
rootNode.attachChild(wall_geo);

/** Make cube physical with a mass of 0.0f (barrier) */
wall_phy = new RigidBodyControl(0.0f);

/** Add physical wall to physics space. */
wall_geo.addControl(wall_phy);
bulletAppState.getPhysicsSpace().add(wall_phy);

// Create left side wall
/** Create a cube geometry and attach to scene graph. */
Geometry ls_wall_geo = new Geometry("ls_wall", ls_box);
ls_wall_geo.setLocalTranslation(-6, 12, 0);
ls_wall_geo.setMaterial(wall_mat);
ls_wall_geo.scale(0.5f/*width*/,100/*height*/,10f/*thickness*/);
this.rootNode.attachChild(ls_wall_geo);

/** Make cube physical with a mass of 0.0f (barrier) */
ls_wall_phy = new RigidBodyControl(0.0f);

/** Add physical wall to physics space. */
ls_wall_geo.addControl(ls_wall_phy);
bulletAppState.getPhysicsSpace().add(ls_wall_phy);

// Create right side wall`
/** Create a cube geometry and attach to scene graph. */
Geometry rs_wall_geo = new Geometry("rs_wall", rs_box);
rs_wall_geo.setLocalTranslation(6, 12, 0);
rs_wall_geo.setMaterial(wall_mat);
rs_wall_geo.scale(0.5f/*width*/,100/*height*/,10f/*thickness*/);
this.rootNode.attachChild(rs_wall_geo);

/** Make cube physical with a mass of 0.0f (barrier) */
rs_wall_phy = new RigidBodyControl(0.0f);

/** Add physical wall to physics space. */
rs_wall_geo.addControl(rs_wall_phy);
bulletAppState.getPhysicsSpace().add(rs_wall_phy);

}

public void initRabbit()
{
/** Create a cube geometry and attach to scene graph. */
rabbit_geo = new Geometry("rabbit", sphere);
rabbit_geo.setMaterial(rabbit_mat);
rabbit_geo.setLocalTranslation(0, 0, 1);
rootNode.attachChild(rabbit_geo);

/** Make cube physical with a mass > 0.0f. */
rabbit_phy = new RigidBodyControl(1f);

/** Add physical wall to physics space. */
rabbit_geo.addControl(rabbit_phy);
bulletAppState.getPhysicsSpace().add(rabbit_phy);
}

/** This method makes the rabbit jump */
public void jumpRabbit()
{
/** Accelerate the physcial rabbit upwards */
rabbit_phy.setLinearVelocity(new Vector3f(0f,1f,0f).mult(8));
}

public void nudgeRabbit(String s)
{
if(s.equals("right"))
{
rabbit_phy.setLinearVelocity(new Vector3f(1f,0f,0f).mult(2));
}

if(s.equals("left"))
{
rabbit_phy.setLinearVelocity(new Vector3f(-1f,0f,0f).mult(2));
}
}
}[/java]

If you edit your post and put the code in the little java tags (click the java button to see what they look like when editing your post) then it will be easier for others to read your code.

Updated. Thanks for the good advice!

Try to set the boxes directly to the right size, instead using scale. Also this will make the code simpler.



Also i suggest to use Plane shapes instead of boxes, since tunneling trough them is impossible, while a box with width 1 could be tunneld by anything moving faster than 0.5f