Hi
I wanted to make the mesh move along with the animation, so that at the end of the animation the position of the mesh will be where I see it, not at 000 or at its beginning position.
I managed to make the mesh follow the animation, but as in video there are some issue which I am not so sure why.
http://www.youtube.com/watch?v=6jImSWUzvMc
if I let the blend time like default (which I think is 0)
then the mesh will fly forward/backward depend on which key I press then slowly change to real animation.
if I let the blend time be 0.01f like in second half of video then the animation is like what I desire but then I get some glitch/flick
are there anyway to get rid of that flick ? what could be the reason and how can I solve it ?
here is my whole code, I can also upload the packed jar file + blender file, but I dont know how to upload attachment here in this new forum.
[java]
package position;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.animation.Skeleton;
import com.jme3.app.SimpleApplication;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
public class main extends SimpleApplication implements AnimEventListener,ActionListener{
Spatial mesh,position,positionCenter;
Node positionNode=new Node("positionNode");
AnimControl animcontrol;
AnimChannel animChannel;
Skeleton skeleton;
boolean left,right,move,continous;
@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-10f, -70f, -10).normalizeLocal());
rootNode.addLight(sun);
cam.setLocation(new Vector3f(0, 50, 150));
cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
initKeys();
//create Materials and ISO box
Material blue = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
blue.setColor("m_Color", ColorRGBA.Blue);
Material red = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
red.setColor("m_Color", ColorRGBA.Red);
Material lightgray = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
lightgray.setColor("m_Color", ColorRGBA.LightGray);
Material orange = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
orange.setColor("m_Color", ColorRGBA.Orange);
Material green = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
green.setColor("m_Color", ColorRGBA.Green);
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
positionCenter = new Geometry("positionCenter", b); positionCenter.setMaterial(red);
position = new Geometry("position", b); position.setMaterial(green);
//create positionNode
positionNode.attachChild(positionCenter);
positionNode.attachChild(position);
mesh=assetManager.loadModel("position/Cylinder.mesh.xml");
mesh.lookAt(new Vector3f(1,0,0), Vector3f.UNIT_Y);
positionNode.lookAt(new Vector3f(1,0,0), Vector3f.UNIT_Y);
animcontrol=(AnimControl) mesh.getControl(0);
animcontrol.addListener(this);
animChannel=animcontrol.createChannel();
skeleton=animcontrol.getSkeleton();
rootNode.attachChild(mesh);
rootNode.attachChild(positionNode);
}
private void initKeys() {
inputManager.addMapping("left", new KeyTrigger(keyInput.KEY_LEFT));
inputManager.addMapping("right", new KeyTrigger(keyInput.KEY_RIGHT));
inputManager.addListener(this, new String[]{"left","right"});
}
@Override
public void simpleUpdate(float tpf) {
combo();
if (move) {
positionCenter.setLocalTranslation(skeleton.getBone("master").getModelSpacePosition());
position.setLocalTranslation(positionCenter.getLocalTranslation().add(skeleton.getBone("position").getModelSpacePosition()));
mesh.setLocalTranslation(position.getWorldTranslation());
}
// cam.lookAt(mesh.getWorldTranslation(), Vector3f.UNIT_Y);
}
@Override
public void onAnimChange(AnimControl arg0, AnimChannel arg1, String arg2) { }
@Override
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String name) {
continous=true;
patch();
}
protected void patch(){
positionNode.detachChild(position);
positionNode.setLocalTranslation(mesh.getWorldTranslation());
positionNode.attachChild(position);
position.setLocalTranslation(Vector3f.ZERO);
move=false;
}
public void combo(){
if (left) {
if (continous) {
continous=false;
animChannel.setAnim("back",0.01f); animChannel.setLoopMode(LoopMode.DontLoop); //animChannel.setSpeed(2);
move=true;
}
}
else if (right) {
if (continous) {
continous=false;
animChannel.setAnim("for",0.01f); animChannel.setLoopMode(LoopMode.DontLoop);// animChannel.setSpeed(2);
move=true;
}
}
}
@Override
public void onAction(String name, boolean keyPressed, float arg2) {
if (name.equals("left")) {
if (keyPressed) { left=true; continous=true;}else{ left=false; }
}
else if (name.equals("right")) {
if (keyPressed) { right=true; continous=true;}else{ right=false; }
}
}
public static void main(String[] args) {
main app = new main(); app.setShowSettings(false); app.start();
}
}
[/java]