First, my cube was showing correctly and i could see all three balls. However, the balls never bounced. They just kept flying into space. Now, I’ve added the physics state to the walls of the cube and the balls and the cube doesnt show correctly and I cannot see the balls anymore.
I am trying to get the balls to bounce inside the cube.
Here is the code below:
import com.jme.image.Texture;
import com.jme.scene.shape.Sphere.TextureMode;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
//import com.jme3.font.BitmapFont;
//import com.jme3.font.BitmapText;
//import com.jme3.font.Rectangle;
//import com.jme3.input.MouseInput;
//import com.jme3.input.controls.ActionListener;
//import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
//import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
//import java.io.BufferedWriter;
//import java.io.FileWriter;
//import java.io.IOException;
/**
- test
-
@author normenhansen
*/
public class Main extends SimpleApplication {
private RandomPhysics randomPhys = new RandomPhysics();
//create nodes to appear on screen
private Node cubeNode = new Node(“cubeNode”);
private Node sphereNode = new Node(“sphereNode”);
private Node physicsSpace = new Node(“physicsSpace”);
//prepares boxes
private Box frontWall, floor, leftWall, rightWall, backWall;
//creates Geometry for boxes to be seen
private Geometry geoFloor, geoLeftSide, geoRightSide, geoFront, geoBackWall, sphere1, sphere2, sphere3;
//prepares Materials
Material matSphere1, matSphere2, matSphere3, matFloor, matBackWall, matLeftWall, matRightWall, matFront;
//prepares Sphere
//private Sphere ballSpheres;
private static final Sphere ballSpheres;
//= new Sphere(32,32,.4f, true, false);
//prepares the Physics Application State
private BulletAppState bulletAppState,bulletAppState_front, bulletAppState_left, bulletAppState_right, bulletAppState_back, bulletAppState_floor;
private RigidBodyControl ball_phy1, ball_phy2, ball_phy3, frontWall_phy, floor_phy, leftWall_phy, rightWall_phy, BackWall_phy;
//prepares geometries and physical n
//CollisionShape cubeCollisionShape = CollisionShapeFactory.createMeshShape((Node)cubeNode);
public static void main(String[] args) {
Main app = new Main();
app.start();
}
static {
//prepares Sphere
ballSpheres = new Sphere(32,32,.4f, true, false);
//ballSpheres.setTextureMode(TextureMode.Projected);
}
@Override
public void simpleInitApp() {
//creates Cube environment
initCube();
//creates balls
initSpheres();
//creates Materials
initMaterials();
//creates Nodes
initNodes();
/** Configure cam to look at scene */
cam.setLocation(new Vector3f(2, 4f, 6f));
cam.lookAt(new Vector3f(2, 2, 2), Vector3f.UNIT_Y);
/** Add InputManager action: Left click triggers shooting. */
inputManager.addMapping("shoot",
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "shoot");
}
private void initCube(){
bulletAppState_front=new BulletAppState();
stateManager.attach(bulletAppState_front);
bulletAppState_floor=new BulletAppState();
stateManager.attach(bulletAppState_floor);
bulletAppState_left=new BulletAppState();
stateManager.attach(bulletAppState_left);
bulletAppState_right=new BulletAppState();
stateManager.attach(bulletAppState_right);
bulletAppState_back=new BulletAppState();
stateManager.attach(bulletAppState_back);
//create frontWall
frontWall = new Box(Vector3f.ZERO,.1f,5f,5f);
//frontWall = new Box();
geoFront = new Geometry("My Box", frontWall);
geoFront.setQueueBucket(RenderQueue.Bucket.Translucent);
geoFront.setLocalTranslation(5.1f,5.9f,0);
// this.rootNode.attachChild(geoFront);
frontWall_phy = new RigidBodyControl(0.0f);
geoFront.addControl(frontWall_phy);
bulletAppState_front.getPhysicsSpace().add(frontWall_phy);
//create floor
floor = new Box(Vector3f.ZERO,5f,.1f,5f);
//floor = new Box();
geoFloor = new Geometry("Floor", floor);
geoFloor.setQueueBucket(RenderQueue.Bucket.Translucent);
geoFloor.setLocalTranslation(0,1,0);
floor_phy = new RigidBodyControl(0.0f);
geoFloor.addControl(floor_phy);
bulletAppState_floor.getPhysicsSpace().add(floor_phy);
//create leftWall
leftWall = new Box(Vector3f.ZERO,5f,5f,.1f);
//leftWall = new Box();
geoLeftSide = new Geometry("Left Wall", leftWall);
geoLeftSide.setQueueBucket(RenderQueue.Bucket.Translucent);
geoLeftSide.setLocalTranslation(0,5.9f,5.1f);
leftWall_phy = new RigidBodyControl(0.0f);
geoLeftSide.addControl(leftWall_phy);
bulletAppState_left.getPhysicsSpace().add(leftWall_phy);
//create rightWall
rightWall = new Box(Vector3f.ZERO,5f,5f,.1f);
//rightWall = new Box();
geoRightSide = new Geometry("Left Wall", rightWall);
geoRightSide.setQueueBucket(RenderQueue.Bucket.Translucent);
geoRightSide.setLocalTranslation(0,5.9f,-5.1f);
rightWall_phy = new RigidBodyControl(0.0f);
geoRightSide.addControl(rightWall_phy);
bulletAppState_right.getPhysicsSpace().add(rightWall_phy);
//create backWall
backWall =new Box(Vector3f.ZERO, .1f,5f,5f);
//backWall =new Box();
geoBackWall = new Geometry ("Back Wall", backWall);
geoBackWall.setQueueBucket(RenderQueue.Bucket.Translucent);
geoBackWall.setLocalTranslation(-5.1f,5.9f,0f);
BackWall_phy = new RigidBodyControl(0.0f);
geoBackWall.addControl(BackWall_phy);
bulletAppState_back.getPhysicsSpace().add(BackWall_phy);
}
private void initMaterials(){
//create material for floor
matFloor = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
//matFloor.setColor("Color", ColorRGBA.Blue);
matFloor.setColor("Color", new ColorRGBA(.6f,.3f,0,.5f));
geoFloor.setMaterial(matFloor);
//geoFloor.setMaterial(matFloor);
//geoFloor.setLocationTranslation(0,-0.1f,0);
//create material for backWalls
matBackWall = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matBackWall.setColor("Color", ColorRGBA.Green);
//matBackWall.setColor("Color", new ColorRGBA(0f,.5f,0,.5f));
geoBackWall.setMaterial(matBackWall);
matFront = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
//matFront.setColor("Color", ColorRGBA.Orange);
matFront.setColor("Color", new ColorRGBA(0.5f,.5f,0,.5f));
geoFront.setMaterial(matFront);
//create material for left and right Walls
matLeftWall = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matLeftWall.setColor("Color", new ColorRGBA(.5f,0f,0f,.5f));
//matLeftWall.setColor("Color", ColorRGBA.Yellow);
geoLeftSide.setMaterial(matLeftWall);
matRightWall = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matRightWall.setColor("Color", new ColorRGBA(0f,0f,0.5f,.5f));
//matRightWall.setColor("Color", ColorRGBA.White);
geoRightSide.setMaterial(matRightWall);
//create Sphere1 material
matSphere1 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
matSphere1.setColor("Color",ColorRGBA.Green);
sphere1.setMaterial(matSphere1);
//create Sphere2 material
matSphere2 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
matSphere2.setColor("Color",ColorRGBA.Blue);
sphere2.setMaterial(matSphere2);
//create Sphere3 material
matSphere3 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
matSphere3.setColor("Color",ColorRGBA.Orange);
sphere3.setMaterial(matSphere3);
}
private void initNodes(){
cubeNode.attachChild(geoFront);
cubeNode.attachChild(geoBackWall);
cubeNode.attachChild(geoFloor);
cubeNode.attachChild(geoLeftSide);
cubeNode.attachChild(geoRightSide);
//cubeNode.rotate(1,1,1);
rootNode.attachChild(cubeNode);
sphereNode.attachChild(sphere1);
sphereNode.attachChild(sphere2);
sphereNode.attachChild(sphere3);
rootNode.attachChild(sphereNode);
}
/**
-
Every time the shoot action is triggered, a new cannon ball is produced.
-
The ball is set up to fly from the camera position in the camera direction.
*/
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals(“shoot”) && !keyPressed) {
initSpheres();
}
}
};
private void initSpheres(){
//Sphere.setTextureMode(TextureMode.Projected);
//set up physics game
bulletAppState=new BulletAppState();
stateManager.attach(bulletAppState);//creates 3 Spheres with associated physics controls
sphere1 = new Geometry (“Sphere 1”,ballSpheres);
// sphere1.setLocalTranslation(cam.getLocation());//position the ball
ball_phy1 = new RigidBodyControl(1f);//make ball physicall with mass > 0.0f
sphere1.addControl(ball_phy1);//add ball to physical spacesphere2 = new Geometry (“Sphere 2”,ballSpheres);
// sphere2.setLocalTranslation(cam.getLocation());
ball_phy2 = new RigidBodyControl(1.5f);//make ball physicall with mass > 0.0f
sphere2.addControl(ball_phy2);sphere3 = new Geometry (“Sphere 3”,ballSpheres);
//sphere3.setLocalTranslation(cam.getLocation());//positions the ball
ball_phy3 = new RigidBodyControl(2f);//make ball physicall with mass > 0.0f
sphere3.addControl(ball_phy3);//Adds the sphere spatials to the physics state
bulletAppState.getPhysicsSpace().add(ball_phy1);
bulletAppState.getPhysicsSpace().add(ball_phy2);
bulletAppState.getPhysicsSpace().add(ball_phy3);
ball_phy1.setLinearVelocity(cam.getDirection().mult(15)); //accelerate the physicall ball to shoot it
ball_phy2.setLinearVelocity(cam.getDirection().mult(15)); //accelerate the physicall ball to shoot it
ball_phy3.setLinearVelocity(cam.getDirection().mult(15)); //accelerate the physicall ball to shoot it
}
private void initInputs() {
inputManager.addMapping("shoot",
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "shoot");
}
@Override
public void simpleUpdate(float tpf) {
//geometry
}
@Override
public void simpleRender(RenderManager rm) {
}
}