This is quite close. Only, my model still flying, and walk animation stop suddenly at start cahnge to idle then to walk again at the end.
I do like this…
/**
* A walking animated character followed by a 3rd person camera on a terrain with LOD.
* @author normenhansen
*/
public class CharWalk extends SimpleApplication implements ActionListener, AnimEventListener {
private BulletAppState bulletAppState;
private static final Logger LOG = Logger.getLogger(CharWalk.class.getName());
//character
CharacterControl character;
Node model;
//temp vectors
Vector3f walkDirection = new Vector3f();
//terrain
TerrainQuad terrain;
RigidBodyControl terrainPhysicsNode;
//Materials
Material matRock;
Material matBullet;
//animation
AnimChannel animationChannel;
// AnimChannel shootingChannel;
AnimControl animationControl;
float airTime = 0;
//camera
boolean left = false, right = false, up = false, down = false;
ChaseCamera chaseCam;
public static void main(String[] args) {
CharWalk app = new CharWalk();
app.start();
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
stateManager.attach(bulletAppState);
bulletAppState.setDebugEnabled(true);
setupKeys();
createLight();
createSky();
createTerrain();
createCharacter();
setupChaseCamera();
setupAnimationController();
// setupFilter();
}
private PhysicsSpace getPhysicsSpace() {
return bulletAppState.getPhysicsSpace();
}
private void setupKeys() {
inputManager.addMapping("wireframe", new KeyTrigger(KeyInput.KEY_T));
inputManager.addListener(this, "wireframe");
inputManager.addMapping("CharLeft", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("CharRight", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("CharUp", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("CharDown", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("CharSpace", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("CharShoot", new KeyTrigger(KeyInput.KEY_X));
inputManager.addListener(this, "CharLeft");
inputManager.addListener(this, "CharRight");
inputManager.addListener(this, "CharUp");
inputManager.addListener(this, "CharDown");
inputManager.addListener(this, "CharSpace");
inputManager.addListener(this, "CharShoot");
}
private void createLight() {
Vector3f direction = new Vector3f(-0.1f, -0.7f, -1).normalizeLocal();
DirectionalLight dl = new DirectionalLight();
dl.setDirection(direction);
dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
rootNode.addLight(dl);
}
private void createSky() {
Texture west = assetManager.loadTexture("Textures/Sky/Lagoon/slice_0_0.jpg");
Texture east = assetManager.loadTexture("Textures/Sky/Lagoon/slice_0_1.jpg");
Texture north = assetManager.loadTexture("Textures/Sky/Lagoon/slice_0_0.jpg");
Texture south = assetManager.loadTexture("Textures/Sky/Lagoon/slice_0_1.jpg");
Texture up = assetManager.loadTexture("Textures/Sky/Lagoon/blue.jpg");
Texture down = assetManager.loadTexture("Textures/Sky/Lagoon/blue.jpg");
Spatial sky = SkyFactory.createSky(assetManager, west, east, north, south, up, down);
rootNode.attachChild(sky);
}
private void createTerrain() {
matRock = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
matRock.setBoolean("useTriPlanarMapping", false);
matRock.setBoolean("WardIso", true);
matRock.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/island512.png");
Texture grass = assetManager.loadTexture("Textures/Terrain/splat/snow.jpg");
grass.setWrap(WrapMode.Repeat);
matRock.setTexture("DiffuseMap", grass);
matRock.setFloat("DiffuseMap_0_scale", 64);
Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
dirt.setWrap(WrapMode.Repeat);
matRock.setTexture("DiffuseMap_1", dirt);
matRock.setFloat("DiffuseMap_1_scale", 16);
Texture rock = assetManager.loadTexture("Textures/Terrain/splat/sand.jpg");
rock.setWrap(WrapMode.Repeat);
matRock.setTexture("DiffuseMap_2", rock);
matRock.setFloat("DiffuseMap_2_scale", 128);
Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/normal.jpg");
normalMap0.setWrap(WrapMode.Repeat);
Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/normal.png");
normalMap1.setWrap(WrapMode.Repeat);
Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/normal.png");
normalMap2.setWrap(WrapMode.Repeat);
matRock.setTexture("NormalMap", normalMap0);
matRock.setTexture("NormalMap_1", normalMap2);
matRock.setTexture("NormalMap_2", normalMap2);
AbstractHeightMap heightmap = null;
try {
heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.25f);
heightmap.load();
} catch (Exception e) {
e.printStackTrace();
}
terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
List<Camera> cameras = new ArrayList<Camera>();
cameras.add(getCamera());
TerrainLodControl control = new TerrainLodControl(terrain, cameras);
terrain.addControl(control);
terrain.setMaterial(matRock);
terrain.setLocalScale(new Vector3f(2, 2, 2));
terrainPhysicsNode = new RigidBodyControl(CollisionShapeFactory.createMeshShape(terrain), 0);
terrain.addControl(terrainPhysicsNode);
rootNode.attachChild(terrain);
getPhysicsSpace().add(terrainPhysicsNode);
}
private void createCharacter() {
CapsuleCollisionShape capsule = new CapsuleCollisionShape(1f, 4f);
character = new CharacterControl(capsule, 0.01f);
// character.setJumpSpeed(20);
// character.setFallSpeed(35);
// character.setGravity(30);
model = (Node) assetManager.loadModel("Models/erika/erika.j3o");
model.setLocalScale(3.5f);
model.addControl(character);
character.setPhysicsLocation(new Vector3f(-140, 40, -10));
rootNode.attachChild(model);
getPhysicsSpace().add(character);
}
private void setupChaseCamera() {
flyCam.setEnabled(false);
chaseCam = new ChaseCamera(cam, model, inputManager);
}
private void setupAnimationController() {
model.depthFirstTraversal(new SceneGraphVisitorAdapter() {
@Override
public void visit(Node node) {
if (node.getControl(AnimControl.class) != null) {
animationControl = node.getControl(AnimControl.class);
animationControl.addListener(new AnimEventListener() {
@Override
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
}
@Override
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
}
});
animationChannel = animationControl.createChannel();
}
}
});
if (animationControl == null) {
LOG.log(Level.SEVERE, "No AnimControl {0}", model);
throw new RuntimeException();
}
// animationControl = model.getControl(AnimControl.class);
// animationControl.addListener(this);
// animationChannel = animationControl.createChannel();
// shootingChannel = animationControl.createChannel();
// shootingChannel.addBone(animationControl.getSkeleton().getBone("uparm.right"));
// shootingChannel.addBone(animationControl.getSkeleton().getBone("arm.right"));
// shootingChannel.addBone(animationControl.getSkeleton().getBone("hand.right"));
}
@Override
public void simpleUpdate(float tpf) {
Vector3f camDir = cam.getDirection().clone().multLocal(0.1f);
Vector3f camLeft = cam.getLeft().clone().multLocal(0.1f);
camDir.y = 0;
camLeft.y = 0;
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());
}
if (!character.onGround()) {
airTime = airTime + tpf;
} else {
airTime = 0;
}
if (walkDirection.length() == 0) {
if (!"idle".equals(animationChannel.getAnimationName())) {
animationChannel.setAnim("idle", 1f);
}
} else {
character.setViewDirection(walkDirection);
if (airTime > .3f) {
if (!"idle".equals(animationChannel.getAnimationName())) {
animationChannel.setAnim("idle");
}
} else if (!"Walk".equals(animationChannel.getAnimationName())) {
animationChannel.setAnim("walk", 1f);
}
}
character.setWalkDirection(walkDirection);
}
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals("CharLeft")) {
if (value) {
left = true;
} else {
left = false;
}
} else if (binding.equals("CharRight")) {
if (value) {
right = true;
} else {
right = false;
}
} else if (binding.equals("CharUp")) {
if (value) {
up = true;
} else {
up = false;
}
} else if (binding.equals("CharDown")) {
if (value) {
down = true;
} else {
down = false;
}
} else if (binding.equals("CharSpace")) {
character.jump();
// } else if (binding.equals("CharShoot") && !value) {
// bulletControl();
}
}
@Override
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
// if (channel == animationChannel) {
// channel.setAnim("idle");
// }
}
@Override
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
}
}
I can’t figure out what happents.