How can I load different animation files using MD5reader2?
the animation will change dynamically depend on user input from keyboard / mouse like:
arm1.md5anim
arm2.md5anim
arm3.md5anim … etc
for loading 1 animation (arm.md5anim) i use following code, but what about I have many (.md5anim) files?
lightState.get(0).setAmbient(new ColorRGBA(1.5f, 1.5f, 1.5f, 1.0f));
bodyInstance = new SkeletalModelInstance(bodyModel);
bodyAnimator = bodyInstance.addAnimationController();
AnimationAnimator anim = bodyAnimator.addAnimation(bodyAnimation);
anim.setInterpolationThreshold(0f);
bodyAnimator.update(0f);
bodyInstance.updateModelBound();
bodyInstance.setLocalScale(0.5f);
bodyInstance.setLocalTranslation(-20, 80, 0);
rootNode.attachChild(bodyInstance);
Heres a snippet that may help, I used it to get a character to stand and run:
public class Player {
Node playerNode;
private static final String BODYMODEL = "models/bender.md5mesh";
private static final String RUNNINGANIM = "models/benderRunningAnimation.md5anim";
private static final String STANDINGANIM = "models/benderStanding.md5anim";
private Model bodyModel;
private Animation runningAnimation;
private Animation standingAnimation;
private SkeletalAnimationController bodyAnimationController;
private SkeletalModelInstance bodyInstance;
AnimationAnimator runningAnimator;
AnimationAnimator standingAnimator;
public Player() {
playerNode = new Node();
buildPlayer();
}
private void buildPlayer() {
try {
bodyModel = loadModel(BODYMODEL);
runningAnimation = loadAnimation(RUNNINGANIM);
standingAnimation = loadAnimation(STANDINGANIM);
} catch (IOException e) {
e.printStackTrace();
}
bodyInstance = new SkeletalModelInstance(bodyModel);
bodyAnimationController = bodyInstance.addAnimationController();
runningAnimator = bodyAnimationController.addAnimation(runningAnimation);
standingAnimator = bodyAnimationController.addAnimation(standingAnimation);
standingAnimator.fadeTo(0,1);
runningAnimator.fadeIn(1);
runningAnimator.setSpeed(.9f);
// bodyInstance.getLocalRotation().fromAngleAxis(FastMath.PI,Vector3f.UNIT_Y);
bodyInstance.getLocalTranslation().set(0, 0, 0);
bodyInstance.setNormalsMode(SceneElement.NM_GL_NORMALIZE_PROVIDED);
bodyInstance.setLocalScale(.28f);
bodyInstance.updateGeometricState(0, false);
bodyInstance.setModelBound(new BoundingBox());
bodyInstance.updateModelBound();
playerNode.attachChild(bodyInstance);
// playerNode.setModelBound(new BoundingBox());
// playerNode.updateModelBound();
playerNode.getLocalTranslation().set(0, 1, 0);
playerNode.updateGeometricState(0, false);
playerNode.updateRenderState();
}
public void setStandingAnimator(){
runningAnimator.fadeOut(.1f, false);
standingAnimator.fadeIn(.5f);
}
public void setRunningAnimator(){
standingAnimator.fadeOut(.2f, false);
runningAnimator.fadeIn(.5f);
}
from update method
if (!KeyInput.get().isKeyDown(KeyInput.KEY_W)
&& !KeyInput.get().isKeyDown(KeyInput.KEY_A)
&& !KeyInput.get().isKeyDown(KeyInput.KEY_S)
&& !KeyInput.get().isKeyDown(KeyInput.KEY_D)) {
player1.setStandingAnimator();
} else {
player1.setRunningAnimator();
}
when I tried the Player code I got null pointer exception
–> bodyInstance.setStandingAnimator();
Player.java
import com.jme.bounding.BoundingBox;
import com.jme.scene.Node;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import md5reader.MD5AnimReader;
import md5reader.MD5MeshReader;
import model.Model;
import model.SkeletalModelInstance;
import model.animation.Animation;
import model.animation.AnimationAnimator;
import model.animation.*;
public class Player {
public Node playerNode;
private static final String BODYMODEL = "models/aaa.md5mesh";
private static final String RUNNINGANIM = "models/aaa.md5anim";
private static final String STANDINGANIM = "models/aaa.md5anim";
private Model bodyModel;
private Animation runningAnimation;
private Animation standingAnimation;
private SkeletalAnimationController bodyAnimationController;
private SkeletalModelInstance bodyInstance;
AnimationAnimator runningAnimator;
AnimationAnimator standingAnimator;
public Player() {
playerNode = new Node();
buildPlayer();
}
private void buildPlayer() {
try {
bodyModel = loadModel(BODYMODEL);
runningAnimation = loadAnimation(RUNNINGANIM);
standingAnimation = loadAnimation(STANDINGANIM);
} catch (IOException e) {
e.printStackTrace();
}
bodyInstance = new SkeletalModelInstance(bodyModel);
bodyAnimationController = (SkeletalAnimationController) bodyInstance.addAnimationController();
runningAnimator = bodyAnimationController.addAnimation(runningAnimation);
standingAnimator = bodyAnimationController.addAnimation(standingAnimation);
standingAnimator.fadeTo(0,1);
runningAnimator.fadeIn(1);
runningAnimator.setSpeed(.9f);
// bodyInstance.getLocalRotation().fromAngleAxis(FastMath.PI,Vector3f.UNIT_Y);
bodyInstance.getLocalTranslation().set(0, 0, 0);
//bodyInstance.setNormalsMode(SceneElement.NM_GL_NORMALIZE_PROVIDED);
bodyInstance.setLocalScale(.28f);
bodyInstance.updateGeometricState(0, false);
bodyInstance.setModelBound(new BoundingBox());
bodyInstance.updateModelBound();
playerNode.attachChild(bodyInstance);
// playerNode.setModelBound(new BoundingBox());
// playerNode.updateModelBound();
playerNode.getLocalTranslation().set(0, 1, 0);
playerNode.updateGeometricState(0, false);
playerNode.updateRenderState();
}
public void setStandingAnimator(){
runningAnimator.fadeOut(.1f, false);
standingAnimator.fadeIn(.5f);
}
public void setRunningAnimator(){
standingAnimator.fadeOut(.2f, false);
runningAnimator.fadeIn(.5f);
}
private Model loadModel(String path) throws IOException {
InputStream in = getClass().getResourceAsStream("/" + path);
if (in == null) {
throw new FileNotFoundException("Cannot find " + path);
}
MD5MeshReader reader = new MD5MeshReader();
reader.setProperty(MD5MeshReader.CLASSLOADER, getClass().getClassLoader());
return reader.readModel(in);
}
private Animation loadAnimation(String path) throws IOException {
InputStream in = getClass().getResourceAsStream("/" + path);
if (in == null) {
throw new FileNotFoundException("Cannot find " + path);
}
MD5AnimReader animReader = new MD5AnimReader();
Animation animation = animReader.readAnimation(in);
return animation;
}
public Node getPlayer(){
return playerNode;
}
}
mainProgram.java
public mainProgram() {
try{
bodyInstance = new Player();
rootNode.attachChild(bodyInstance.getPlayer());
} catch (Exception e){
}
}
protected void simpleUpdate() {
if (!KeyInput.get().isKeyDown(KeyInput.KEY_W)
&& !KeyInput.get().isKeyDown(KeyInput.KEY_A)
&& !KeyInput.get().isKeyDown(KeyInput.KEY_S)
&& !KeyInput.get().isKeyDown(KeyInput.KEY_D)) {
bodyInstance.setStandingAnimator();
} else {
bodyInstance.setRunningAnimator();
}
}
thats not a complete example, just a snippet youre going to have to play around to get it all working
I use a custom Controller class that has:
MD5Animation currentAnimation;
MD5JmeNode jmeNode;
TreeMap<String, MD5Animation> animations;
I'd post the code, but I figure I should let you make your own custom Controller :)
Some source code will be very helpful.
MD5reader has no JavaDoc and I am still learning it slowly.
Thanks again