Quaternion lookAt

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);
      }
   }
   

}


lookAt changes the quat to position something to look from the origin.  (0,0,0)

okay. i see what you mean there. so i have to use spatial lookAt? when using spatial lookAt im trying to restrict the rotation to only on the Y axis. how would i do this?

solved it :smiley:

bgilb said:

solved it :D


Yay!  :lol:

How?  Just in case anyone with a similar problem stumbles upon this thread in the future.

well theres ways a couple problems. firstly i was updating the lookat every frame which was uneccesary. and the thing that fixed it was i changed the height of the lookAt to match the players that way the player wouldn't look anywhere on the z axis and remained standing.

:slight_smile:

but theres another problem i noticed. if the objects target is really close to him, it forces him to look up or down, thus rotation the model where i didn't want it to. is there anyway to force the model not to yaw or pitch on the Y axis and only rotate?

you mean rotate only arround the Y axis :slight_smile:



yaw, pitch -> rotate arround X and Z. now think a bit :wink:

how would i do it?