setLocalTranslation only for one axe?

Hi,



I want ton move a 3D modele on X and Z axes, for that, I made a function that it is launched in update()



   private void moveModele(Vector3f newCoordinates) {
      if(_pMD2Model.getWorldTranslation().x != newCoordinates.x || _pMD2Model.getWorldTranslation().z != newCoordinates.z) {
         if(_pMD2Model.getWorldTranslation().x < newCoordinates.x) {
            _pMD2Model.setLocalTranslation(new Vector3f(_pMD2Model.getWorldTranslation().x + 1, _pMD2Model.getWorldTranslation().y, _pMD2Model.getWorldTranslation().z));
         } else {
            _pMD2Model.setLocalTranslation(new Vector3f(_pMD2Model.getWorldTranslation().x - 1, _pMD2Model.getWorldTranslation().y, _pMD2Model.getWorldTranslation().z));
         }
         if(_pMD2Model.getWorldTranslation().z < newCoordinates.z) {
            _pMD2Model.setLocalTranslation(new Vector3f(_pMD2Model.getWorldTranslation().x, _pMD2Model.getWorldTranslation().y, _pMD2Model.getWorldTranslation().z + 1));
         } else {
            _pMD2Model.setLocalTranslation(new Vector3f(_pMD2Model.getWorldTranslation().x - 1, _pMD2Model.getWorldTranslation().y, _pMD2Model.getWorldTranslation().z - 1));
         }
      }
   }



The problem is that the coordinates is updated one after one, before on X axe and after on Y axe, I don't understand why :s Any suggestions ?

its updating one by one is coz u told it to do it that way :wink:



if u just want to move a model to a new position. and u want the movement to be smooth, the best way to calculate a direction, and use that to move.



heres a simple example.



// define a moving speed.
final float speed = 10;

protected void move(float interpolation, Vector3f destination) {
   if(destination != null && destination != model.getLocalTranslation)
     {
         Vector3f currentLocal = (Vector3f) model.getLocalTranslation().clone();
         Vector3f destinationCopy = (Vector3f) destination.clone();
         Vector3f direction = destinationCopy.subtractLocal(currentLocal).normalizeLocal();
         Vector3f displacement = new Vector3f();
         displacement.setX(direction.getX() * interpolation * speed);
         displacement.setZ(direction.getZ() * interpolation * speed);

         model.getLocalTranslation.addLocal(displacement);
     }
}



call this in ur update method in the main game loop each frame.

it will move smoothly :D

oh btw~ u can change the if statement according to the coordinate system u r using.



i like localtranslation better coz its always relative to the moving obj's parent which is easier for me to understand XD

Tank's a lot for your rapid response and it works perfectly !  :smiley:

RorolePro said:

Tank's a lot for your rapid response and it works perfectly !