I am looking for some examples about using BoneAnimation and BoneTransform to animate a Bone.
I try to migrate the MD5 Reader 2 to a full jME support, but I am unable to understand exactly how to:
- animate Bones;
- create a skeletal system/hierarchy;
- using BoneDebugger to make Bones visible.
I made a simple test, but it does not work. At least I am not sure. It runs and something happened, as I have been able to test using a simple System.out.println() at the end of simpleUpdate() method. But I am not sure of what is happening.
Thanks.
package org.md5reader2.test;
import com.jme.animation.Bone;
import com.jme.animation.BoneAnimation;
import com.jme.animation.BoneTransform;
import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.math.Matrix3f;
import com.jme.math.Matrix4f;
import com.jme.math.Quaternion;
import com.jme.math.TransformMatrix;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Capsule;
import com.jme.util.BoneDebugger;
public class TestBoneAnimation extends SimpleGame {
private Bone bone;
private BoneAnimation boneAnim;
protected void simpleInitGame() {
boneAnim = new BoneAnimation(“boneAnim”);
BoneTransform boneTrans = new BoneTransform();
boneAnim.setInterpolate(true);
// Setup times (each keyframe has a position in time).
float[] times = { 0f, 1f, 2f };
boneAnim.setTimes(times);
// Setup start/end frames.
boneAnim.setStartFrame(0);
boneAnim.setEndFrame(2);
// Setup transforms.
Matrix4f m1 = new Matrix4f();
m1.angleRotation(new Vector3f(0f, 10f, 0f));
Matrix4f m2 = new Matrix4f();
m2.angleRotation(new Vector3f(0f, 20f, 0f));
Matrix4f m3 = new Matrix4f();
m3.angleRotation(new Vector3f(0f, 30f, 0f));
Matrix4f[] transforms = new Matrix4f[3];
transforms[0] = m1;
transforms[1] = m2;
transforms[2] = m3;
boneTrans.setTransforms(transforms);
// Setup interpolations.
int[] interpolations = { BoneAnimation.BEZIER, BoneAnimation.BEZIER,
BoneAnimation.BEZIER };
boneAnim.setInterpolationTypes(interpolations);
bone = new Bone(“bone”);
bone.setLocalTranslation(new Vector3f(0f, 0f, 0f));
// bone.setLocalRotation(new Quaternion().fromAngleAxis(2, new
// Vector3f(0f, 1f, 0f)));
boneTrans.setBone(bone);
boneAnim.addBoneTransforms(boneTrans);
rootNode.attachChild(bone);
}
protected void simpleUpdate() {
super.simpleUpdate();
boneAnim.update(tpf, com.jme.scene.Controller.RT_CYCLE, tpf);
System.out.println("Frame: "
- boneAnim.getCurrentFrame()
- "; Rot: "
- bone.getLocalRotation()
- "; tRot: "
- boneAnim.getBoneTransforms().get(0).getRotations()[boneAnim
.getCurrentFrame()]);
BoneDebugger.drawBones(new Box("Box", new Vector3f(-1f, -1f, -1f),
new Vector3f(1f, 1f, 1f)), display.getRenderer());
}
public static void main(String[] args) {
TestBoneAnimation app = new TestBoneAnimation();
app
.setDialogBehaviour(AbstractGame.FIRSTRUN_OR_NOCONFIGFILE_SHOW_PROPS_DIALOG);
app.start();
}
}