okay so i implement a look at thingy. it works fine if the i comment out moveTowardsTarget(), but if i add that, for some reason the model doesn't always look at the target. any ideas?
package model;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.XMLparser.Converters.Md2ToJme;
import com.jmex.model.animation.KeyframeController;
public class Model extends Node
{
Vector3f target = new Vector3f();//the target to run to
KeyframeController keyFramer=new KeyframeController();;
TextureState texState;
float distance = 0;//0 =start 1=end
Vector3f oldPos = new Vector3f();
Vector3f currentPos = new Vector3f();
boolean moving=false;
public Model(String modelLoc, String texLoc, DisplaySystem display)
{
Node tempNode=new Node();
URL model=Model.class.getClassLoader().getResource(modelLoc);
Md2ToJme converter=new Md2ToJme();
ByteArrayOutputStream BO=new ByteArrayOutputStream();
try
{
converter.convert(model.openStream(),BO);
}
catch (IOException e)
{
System.out.println("damn exceptions:" + e.getMessage());
}
try
{
tempNode = ((Node)BinaryImporter.getInstance()
.load(new ByteArrayInputStream(BO.toByteArray())));
}
catch (IOException e)
{
System.out.println("damn exceptions:" + e.getMessage());
}
URL textu=Model.class.getClassLoader().getResource(texLoc);
texState = display.getRenderer().createTextureState();
texState.setEnabled(true);
texState.setTexture(
TextureManager.loadTexture(
textu,
Texture.MM_LINEAR_LINEAR,
Texture.FM_LINEAR));
tempNode.setRenderState(texState);
keyFramer = (KeyframeController)tempNode.getChild(0).getController(0);
keyFramer.setSpeed(8);
Quaternion x = new Quaternion();
x.fromAngleAxis(-FastMath.PI/2, new Vector3f(0,1,0));
tempNode.setLocalRotation(x);
this.attachChild(tempNode);
}
public void setTarget(Vector3f newTarget, int x1, int x2)
{
if(!moving)
{
oldPos=this.getLocalTranslation();
moving=true;
Quaternion x = new Quaternion();
x.lookAt(new Vector3f(newTarget.x,newTarget.y,newTarget.z),new Vector3f(0,1,0));
setAnimation(x1,x2);
target=newTarget;
this.setLocalRotation(x);
this.setLocalRotation(x);
}
}
public void setAnimation(int x1, int x2)
{
keyFramer.setNewAnimationTimes(x1,x2);
}
public void update()
{
moveTowardsTarget();
}
public void moveTowardsTarget()
{
Vector3f currentPos = new Vector3f();
if(moving)
{
Quaternion x = new Quaternion();
x.lookAt(new Vector3f(target.x,target.y,target.z),new Vector3f(0,1,0));
this.setLocalRotation(x);
this.setLocalRotation(x);
distance = distance+0.002f;
currentPos.x = oldPos.x*(1-distance)+target.x*distance;
currentPos.z = oldPos.z*(1-distance)+target.z*distance;
currentPos.y = oldPos.y;
this.setLocalTranslation(currentPos);
}
if(distance>=1.0f)
{
distance=0;
moving=false;
setAnimation(0,8);
}
}
}