Hello everybody
I’ve got a problem,and maybe because i am a n00b, i can’t figure out how to solve it.
Ok,let’s begin:i’ve got a cube with RigidBodyControl
It’s position is over another floor (made,of course,by another cube) and in the simpleupdate method i’ve made a condition
that if the first cube (player) is over the floor,it will “float”
At this point is everything good.
But i have implemented a system that create a motionpath from the position of the cube to the point where i click on the floor
The problem is 2:
1)when the cube start travel across the path,is like it’s “losing” the phisics
2)when it finish the travel,it jumps sooo high and return
Any suggestion?here’s the code
[java]
package mygame;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.cinematic.MotionPath;
import com.jme3.cinematic.events.MotionEvent;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
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.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.PointLight;
import com.jme3.light.SpotLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.ssao.SSAOFilter;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
public class FsicaEnon extends SimpleApplication implements ActionListener,AnalogListener,AnimEventListener {
Node terreno = new Node();
Node giocatore = new Node();
Node personaggio = new Node();
Geometry player,terrain;
BulletAppState bas;
RigidBodyControl rgb_player,rgb_personaggio;
SpotLight spot;
PointLight lamp;
MotionPath moth;
MotionEvent moth_ev;
boolean movimento=false;
private AnimChannel chan;
private AnimControl cont;
public static void main(String[] args) {
FsicaEnon app = new FsicaEnon();
app.start();
}
@Override
public void simpleInitApp() {
bas = new BulletAppState();
stateManager.attach(bas);
flyCam.setMoveSpeed(10);
creauncubo("terreno",terreno,0,0,0,8,0.5f,8,true,0);//node,x,y,z,pos_x,pos_y,pos_z
terrain=(Geometry)terreno.getChild(0);
creauncubo("Giocatore", giocatore, 0, 3, 0, 1, 1, 1, false, 1);
player=(Geometry)giocatore.getChild(0);
rgb_player= new RigidBodyControl(1);
player.addControl(rgb_player);
bas.getPhysicsSpace().add(rgb_player);
rootNode.attachChild(giocatore);
rootNode.attachChild(terreno);
hud();
initKeys();
initLight();
// initCam();
}
public void hud(){
setDisplayStatView(false);
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 - ch.getLineWidth()/2, settings.getHeight() / 2 + ch.getLineHeight()/2, 0);
guiNode.attachChild(ch);
}
@Override
public void simpleUpdate(float tpf) {
Ray r = new Ray(player.getWorldTranslation(), new Vector3f(0,-1,0));
CollisionResults cs=new CollisionResults();
terreno.collideWith(r, cs);
float y=terrain.getLocalTranslation().y;
float y2=player.getLocalTranslation().y;
if(cs.size()>0&&((y2-y)<4)&&!movimento){
rgb_player.applyCentralForce(new Vector3f(0,20,0));
}
lamp.setPosition(cam.getLocation());
}
@Override
public void simpleRender(RenderManager rm) {}
public void initCam(){
cam.setLocation(new Vector3f(player.getWorldTranslation().x+5,player.getWorldTranslation().y+5,player.getWorldTranslation().z+5));
ChaseCamera ccam = new ChaseCamera(cam, player, inputManager);
ccam.setSmoothMotion(false);
ccam.setDefaultDistance(8);
ccam.setMaxDistance(10);
ccam.setDragToRotate(false);
}
public void initKeys(){
inputManager.addMapping("up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("r_click", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
//inputManager.addListener(analogListener, new String[]{"up","down","left","right"});
inputManager.addListener(actionListener, new String[]{"jump","click","r_click"});
}
public void initLight(){
AmbientLight al= new AmbientLight();
al.setColor(ColorRGBA.White);
rootNode.addLight(al);
FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
viewPort.addProcessor(fpp);
SSAOFilter ssaoFilter = new SSAOFilter(12.94f, 43.93f, 0.33f, 0.60f);
fpp.addFilter(ssaoFilter);
lamp = new PointLight();
lamp.setPosition(Vector3f.ZERO);
lamp.setColor(ColorRGBA.White);
rootNode.addLight(lamp);
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 3f));
}
public void creauncubo(String name,Node nd,float x,float y,float z,float dim_x,float dim_y,float dim_z,boolean conc,int weight){
Box nc = new Box(dim_x,dim_y,dim_z);
Geometry gm = new Geometry("cubo",nc);
gm.setLocalTranslation(new Vector3f(x,y,z));
Material mt = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
//mt.getAdditionalRenderState().setWireframe(true);
mt.setColor("Color", ColorRGBA.randomColor());
gm.setMaterial(mt);
if(conc){
RigidBodyControl rgb_tmp = new RigidBodyControl(weight);
gm.addControl(rgb_tmp);
bas.getPhysicsSpace().add(rgb_tmp);
}
nd.attachChild(gm);
}
private AnalogListener analogListener = new AnalogListener(){
public void onAnalog(String name, float value, float tpf) {
if(name.equals("up")){
rgb_player.applyCentralForce(cam.getDirection());
}
if(name.equals("down")){
rgb_player.applyCentralForce(cam.getDirection().negate());
}
if(name.equals("left")){
rgb_player.applyCentralForce(cam.getLeft());
}
if(name.equals("right")){
rgb_player.applyCentralForce(cam.getLeft().negate());
}
}
};
private ActionListener actionListener = new ActionListener(){
public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals("jump")){
Geometry tmp = (Geometry) giocatore.getChild(0);
Material mat = tmp.getMaterial();
mat.setColor("Color", ColorRGBA.randomColor());
rgb_player.applyCentralForce(new Vector3f(0,2,0));
}
if(name.equals("click")){
Ray y = new Ray(cam.getLocation(),cam.getDirection());
CollisionResults cs = new CollisionResults();
terreno.collideWith(y, cs);
if(cs.size()>0){
movimento=true;
moth=new MotionPath();
CollisionResult cd= cs.getClosestCollision();
moth.addWayPoint(player.getWorldTranslation());
moth.addWayPoint(new Vector3f(cd.getContactPoint().x,player.getWorldTranslation().y,cd.getContactPoint().z));
//moth.enableDebugShape(assetManager, rootNode);
moth_ev=new MotionEvent(player,moth);
moth_ev.setSpeed(2);
moth_ev.play();
movimento=false;
}
}
}
};
public void onAction(String name, boolean isPressed, float tpf) {}
public void onAnalog(String name, float value, float tpf) {}
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {}
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {}
public void crea_personaggio(){
personaggio = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
rootNode.attachChild(personaggio);
cont=personaggio.getControl(AnimControl.class);
cont.addListener(this);
chan=cont.createChannel();
chan.setAnim("stand");
personaggio.setLocalTranslation(0, 6, 3);
personaggio.setLocalScale(0.5f);
rgb_personaggio=new RigidBodyControl(1);
personaggio.addControl(rgb_personaggio);
bas.getPhysicsSpace().add(rgb_personaggio);
}
}
[/java]