Hi guys I am trying to make a dice throwing simulation but it takes a significant amount of time to generate the dice and throw it. When the dice is generated it lands on the corner of the dice for some reason. Wondering if any of you would have any input? If any of you could run this code with your own assets and it goes fast then I could narrow it down to my assets.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.font.BitmapText;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
/**
*
* @author Shane.P.Drafahl
*/
public class DiceSim extends SimpleApplication {
public static void main(String[] args){
DiceSim app = new DiceSim();
app.start();
}
/** Prepare the Physics Application State (jBullet) */
private BulletAppState bulletAppState;
/** Prepare geometries and physical nodes for bricks and cannon balls. */
private RigidBodyControl dice_phy;
private Spatial dice;
private RigidBodyControl floor_phy;
private Spatial floor;
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(20);//temp
//assets
dice = assetManager.loadModel("Models/dice_w.j3o");
floor = assetManager.loadModel("Models/platform.j3o");
//physics stuff
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//Stuff to set camera location
cam.setLocation(new Vector3f(-1f, 4f, 6f));// 0, 4f,6f
cam.lookAt(new Vector3f(2, 2, 0), Vector3f.UNIT_Y);
//testing controls
inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "shoot");
initCrossHairs();
platForm();
//light
/*DirectionalLight sun = new DirectionalLight();
sun.setColor(ColorRGBA.White);
sun.setDirection(new Vector3f(-.5f,-.5f,-.5f).normalizeLocal());
rootNode.addLight(sun);*/
PointLight lamp_light = new PointLight();
lamp_light.setColor(ColorRGBA.Yellow);
lamp_light.setRadius(4f);
lamp_light.setPosition(new Vector3f(0,0,0));
rootNode.addLight(lamp_light);
}
public void platForm(){
this.rootNode.attachChild(floor);
floor_phy = new RigidBodyControl(0.0f);
floor.addControl(floor_phy);
bulletAppState.getPhysicsSpace().add(floor_phy);
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("shoot") && !keyPressed) {
throwDice();
}
}
};
public void throwDice(){
this.rootNode.attachChild(dice);
System.out.println("1");
dice.setLocalTranslation(cam.getLocation());
System.out.println("2");
dice_phy = new RigidBodyControl(1f);
System.out.println("3");
dice.addControl(dice_phy);
System.out.println("4");
bulletAppState.getPhysicsSpace().add(dice_phy);
System.out.println("5");
dice_phy.setLinearVelocity(cam.getDirection().mult(25));
System.out.println("6");
}
protected void initCrossHairs() {
guiNode.detachAllChildren();
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+");
ch.setLocalTranslation(
settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
guiNode.attachChild(ch);
}
}