Author Topic: question about object3d.translate  (Read 3689 times)

Offline lancew

  • byte
  • *
  • Posts: 11
    • View Profile
question about object3d.translate
« on: January 27, 2012, 10:33:02 pm »
I basically want to move the horse based upon user input, and have the camera follow the horse.  moveVec.y will always be 0 so that the horse will always move in the x-z plane.  The problem is that the horse moves, but the camera doesn't follow it.  The camera just sits at its initial position.  Heres the code:
Code: [Select]
horse.translate(moveVec);
horse.translateMesh();
SimpleVector horsePos = horse.getTransformedCenter();
horsePos.add(horse.getTranslation());
cam.setPosition(horsePos);
//want cam up to match horse up
SimpleVector up = horse.getYAxis();
up.y *= -1;
//horse is facing along x axis
SimpleVector dir = horse.getXAxis();
cam.setOrientation(dir, up);
//move back according to zoom input
cam.moveCamera(Camera.CAMERA_MOVEOUT, currentZoom);
//move up, but move up less if looking up
cam.moveCamera(Camera.CAMERA_MOVEUP, (currentZoom - lookUp) * .5f);
SimpleVector look = horse.getXAxis();
//look at position ahead of horse
look.scalarMul(currentZoom * 2f);
//look at position above horse if user input indicated to do so
look.y -= lookUp;
cam.lookAt(look);
horse.clearTranslation();

I'm assuming I'm doing the translation wrong in some way, but have no idea how, or what else it could be.
Thanks in advance
/lance

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12297
    • View Profile
    • http://www.jpct.net
Re: question about object3d.translate
« Reply #1 on: January 28, 2012, 12:08:58 am »
This looks a little more complicated than it has to be IMHO, which makes it a little hard to follow...it seems to me, that you are basing the camera's position mainly on the transformed center of the horse. But at the point you are accessing it, the horse's translation is always almost resetted (apart from the added moveVec) by the clearTranslation() call at the end.
Your horse only moves, because you are calling translateMesh() all the time...don't do this. translateMesh() isn't meant to be used to translate an object at runtime. That's what translate() is meant for. translateMesh() is for setup work only, where you want to translate the physical vertices in object space.

Offline lancew

  • byte
  • *
  • Posts: 11
    • View Profile
Re: question about object3d.translate
« Reply #2 on: January 28, 2012, 05:16:39 pm »
Thanks for the reply.  So am I supposed to be calling clearTranslation() at all?  I still don't get why the camera isn't following the horse.

Offline K24A3

  • long
  • ***
  • Posts: 231
    • View Profile
Re: question about object3d.translate
« Reply #3 on: January 28, 2012, 05:40:25 pm »
horse.clearTranslation() will reset the horse back to 0,0,0. You probably don't want to do that at every frame unless you are using your own SimpleVector to keep track of the horses location.

Basically you need to move the horse in 3D space, then set the camera position to the horses translation, call Camera.align to align the cam to the horses rotation, and then adjust the camera using moveCamera().

Edit: Also remove translateMesh() as mentioned by Egon. You don't need to call that function unless you are adjusting the origin of the 3D object during initialization.

Code: [Select]
// Move the horse based on its current position
horse.translate(moveVec);

// Set the camera's to the horse's location
cam.setPosition(horse.getTranslation());

// Align the cam's rotation to the horse's rotation
cam.align(horse);


Try the above first to keep things simple. After that works, adjust the camera position offset based on your currentZoom and lookUp variables, determine the SimpleVector to look at, then finally call Camera.lookAt().
« Last Edit: January 28, 2012, 06:24:11 pm by K24A3 »

Offline lancew

  • byte
  • *
  • Posts: 11
    • View Profile
Re: question about object3d.translate
« Reply #4 on: January 28, 2012, 06:43:39 pm »
OK, now it makes sense.  I'll try it out in a bit.  thanks for your help guys.

Offline lancew

  • byte
  • *
  • Posts: 11
    • View Profile
Re: question about object3d.translate
« Reply #5 on: January 28, 2012, 09:39:41 pm »
Worked like a charm, thanks =P  Heres the code for reference:

Code: [Select]
horse.translate(moveVec);
cam.setPosition(horse.getTranslation());
cam.align(horse);
//horse's 'forward' is actually his side.  rotate to compensate
cam.rotateCameraY(0.0174532925f * 90f);
//move back according to zoom input
cam.moveCamera(Camera.CAMERA_MOVEOUT, currentZoom);
//move up, but move up less if looking up
cam.moveCamera(Camera.CAMERA_MOVEUP, (currentZoom - lookUp) * .5f);

Offline lancew

  • byte
  • *
  • Posts: 11
    • View Profile
Re: question about object3d.translate
« Reply #6 on: January 28, 2012, 10:06:38 pm »
Alright, I'm having another issue.  Heres the code in question:

Code: [Select]
//rotate horse according to user input
horse.rotateY(turn);
if(moveVec.x > 0 || moveVec.z > 0)
{
        //rotate the move vec to match the horses orientation so the movement is propagated properly
moveVec.rotateY(turn);
horse.translate(moveVec);
}

The problem was that the horse was always moving according to his original orientation.  ie, moving forward moved the horse in the same direction (along x axis) regardless of which way the horse was facing.  So I added moveVec.rotateY(turn), thinking that would fix the problem, but it didn't.  Any ideas?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12297
    • View Profile
    • http://www.jpct.net
Re: question about object3d.translate
« Reply #7 on: January 28, 2012, 10:23:39 pm »
This depends on how moveVec is calculated. How might want to try to use horse.getZAxis() instead and use the result as moveVec.

Offline K24A3

  • long
  • ***
  • Posts: 231
    • View Profile
Re: question about object3d.translate
« Reply #8 on: January 29, 2012, 01:22:20 am »
What you want is an fSpeed variable and an fRotation variable to control the horse movement.

Then at each frame:

- Rotate the Y-Axis of the Object3D using rotateY() or rotateAxis() using your fRotation variable.
- Get the Object3D's Z-Axis SimpleVector
- Use ScalarMul on that SimpleVector parsing your fSpeed variable
- Translate the Object3D using that SimpleVector


Offline lancew

  • byte
  • *
  • Posts: 11
    • View Profile
Re: question about object3d.translate
« Reply #9 on: January 29, 2012, 02:46:26 pm »
Thanks, that did the trick.  Heres the code for reference:

Code: [Select]
//only move if the touch move was greater than the tolerance.
//this eliminates the minute moves that occur when holding finger
//still.
if(Math.abs(yd) > MOVE_TOLERANCE || Math.abs(xd) > MOVE_TOLERANCE)
{
SimpleVector xMove = horse.getXAxis();
xMove.scalarMul(-yd * MOVE_INCREMENT);
SimpleVector zMove = horse.getZAxis();
zMove.scalarMul(-xd * MOVE_INCREMENT);
moveVec = xMove;
moveVec.add(zMove);
}

and later...
Code: [Select]
horse.translate(moveVec);