So i followed the tutorial 9 “Collisions” only to discover that the CharacterControl class is deprecated and for what ever reason does not work. It has no unhandled exceptions or errors, but when i run it the character(or the camera i dont know which one it is) floats in the air and will not move only look around. Here is my code.
package Classes;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
/**
*
* @author jeremy
*/
public class Collision extends SimpleApplication implements ActionListener
{
//instance variables
private Spatial sceneModel;
private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false,right = false,up = false,down = false;
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
//methods
public void simpleInitApp()
{
viewPort.setBackgroundColor(new ColorRGBA(0.7f,0.8f,1f,1f));
flyCam.setMoveSpeed(100);
setUpKeys();
setUpLight();
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
assetManager.registerLocator("town.zip", ZipLocator.class);
sceneModel = assetManager.loadModel("main.scene");
sceneModel.setLocalScale(2);
//load the scene
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape((Node)(sceneModel));
landscape = new RigidBodyControl(sceneShape,0);
sceneModel.addControl(landscape);
rootNode.attachChild(sceneModel);
//set up the player
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f,6f,1);
player = new CharacterControl(capsuleShape,0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(0,10,0));
}
public void setUpLight()
{
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(1.3f));
rootNode.addLight(al);
DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(2.8f,-2.8f,-2.8f).normalizeLocal());
rootNode.addLight(dl);
}
public void setUpKeys(){
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Jump");
}
public void onAction(String binding,boolean value,float tpf)
{
if(binding.equals("Left"))
{
if(value)
left = true;
else
left = false;
}
else if(binding.equals("Right"))
{
right = (value) ? true:false;
}
else if(binding.equals("Up"))
{
up = (value)? true:false;
}
else if(binding.equals("Down"))
{
down = (value) ? true:false;
}
else if(binding.equals("Jump"))
{
player.jump();
}
}
public void simpleUpdate(float tpf)
{
camDir.set(cam.getDirection()).multLocal(0.6f);
camLeft.set(cam.getLeft()).multLocal(0.4f);
walkDirection.set(0,0,0);
if(left)
{
walkDirection.addLocal(camLeft);
}
if(right)
{
walkDirection.addLocal(camLeft.negate());
}
if(up)
{
walkDirection.addLocal(camDir);
}
if(down)
{
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}
//main
public static void main(String[] args)
{
Collision me = new Collision();
me.start();
}
}
I am not new to Java just new to the JME enviroment and am trying to complete all of the tutorials before i make a game using it. If anyone knows what is wrong and why the code is not doing what it is supposed to then please reply with how i can fix it.
Thank you, JustinWeq