Why korab and stancia are not collidе?
[java]
import com.jme3.renderer.RenderManager;
import com.jme3.system.AppSettings;
import com.jme3.math.FastMath;
import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.bullet.BulletAppState;
import com.jme3.scene.Node;
import com.jme3.light.SpotLight;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.scene.Spatial;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.scene.shape.Box;
/**
- test
-
@author normenhansen
/
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
AppSettings settings=new AppSettings(true);
settings.setRenderer(AppSettings.LWJGL_OPENGL1);
app.setSettings(settings);
app.start();
}
Boolean isRunning=true;
float acel=0;
private RigidBodyControl landscape; private RigidBodyControl landscape2;
float x=0;
Spatial korab;
Spatial stancia;
private BulletAppState bulletAppState;
public void simpleInitApp() {
/* create a blue box at coordinates (1,-1,1) /
Box box1 = new Box( new Vector3f(1,-1,1), 1,1,1);
Geometry blue = new Geometry("Box", box1);
Material mat1 = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.Blue);
blue.setMaterial(mat1);
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
/* create a red box straight above the blue one at (1,3,1) /
Box box2 = new Box( new Vector3f(1,3,1), 1,1,1);
Geometry red = new Geometry("Box", box2);
Material mat2 = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", ColorRGBA.Red);
red.setMaterial(mat2);
korab = assetManager.loadModel("Models/KORAB.obj");
stancia = assetManager.loadModel("Models/Space.obj");
Material mat_default = new Material(
assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
korab.setMaterial(mat_default);
rootNode.attachChild(korab);
stancia.setMaterial(mat_default);
rootNode.attachChild(stancia);
stancia.setLocalRotation(new Quaternion(4f,0.5f,0.6f,1));
stancia.setLocalTranslation(0, 0, -1000);
/* Create a pivot node at (0,0,0) and attach it to the root node /
Node pivot = new Node("pivot");
rootNode.attachChild(pivot); // put this node in the scene
cam.lookAt(new Vector3f(1,3,1), new Vector3f(10,30,20));
/* Attach the two boxes to the pivot node. /
pivot.attachChild(blue);
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape( stancia);
landscape = new RigidBodyControl(sceneShape, 0);
stancia.addControl(landscape);
CollisionShape sceneShape2 =
CollisionShapeFactory.createMeshShape( korab);
landscape2 = new RigidBodyControl(sceneShape2, 0);
korab.addControl(landscape2);
pivot.attachChild(red);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(korab);
/* Rotate the pivot node: Note that both boxes have rotated! /
pivot.rotate(.4f,.4f,0f);
SpotLight spot = new SpotLight();
spot.setSpotRange(100f); // distance
spot.setSpotInnerAngle(15f * FastMath.DEG_TO_RAD); // inner light cone (central beam)
spot.setSpotOuterAngle(35f * FastMath.DEG_TO_RAD); // outer light cone (edge of the light)
spot.setColor(ColorRGBA.White.mult(1.3f)); // light color
spot.setPosition(cam.getLocation()); // shine from camera loc
spot.setDirection(cam.getDirection()); // shine forward from camera loc
rootNode.addLight(spot);
initKeys();
}
private void initKeys() {
// You can map one or several inputs to one named action
inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
inputManager.addMapping("Acel", new KeyTrigger(KeyInput.KEY_R));
inputManager.addMapping("DEC", new KeyTrigger(KeyInput.KEY_F));
inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
// Add the names to the action listener.
inputManager.addListener(actionListener, new String[]{"Pause"});
inputManager.addListener(analogListener, new String[]{"Left", "Right", "Rotate","DEC","Acel"});
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Pause") && !keyPressed) {
isRunning = !isRunning;
}
}
};
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (name.equals("Rotate")) {
korab.rotate(0, valuespeed, 0);
}
if (name.equals("Acel")) {
acel-=1;
}
if (name.equals("DEC")) {
// if(acel!=5){
acel+=1;
// }
}
if (name.equals("Right")) {
Vector3f v = korab.getLocalTranslation();
korab.setLocalTranslation(v.x + valuespeed, v.y, v.z);
}
if (name.equals("Left")) {
Vector3f v = korab.getLocalTranslation();
korab.setLocalTranslation(v.x - valuespeed, v.y, v.z);
}
}
};
public void simpleUpdate(float tpf) {
x=tpfacel0.1f + x;
korab.setLocalTranslation(0, 0, x);
cam.setLocation(new Vector3f(0,4,x+14));
cam.lookAt(korab.getLocalTranslation(), new Vector3f(0,0,0));
// cam.setRotation(korab.getLocalRotation());
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}
[/java]