question about object3d.translate

Started by lancew, January 27, 2012, 10:33:02 PM

Previous topic - Next topic

lancew

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:

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

EgonOlsen

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.

lancew

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.

K24A3

#3
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.


// 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().

lancew

OK, now it makes sense.  I'll try it out in a bit.  thanks for your help guys.

lancew

Worked like a charm, thanks =P  Heres the code for reference:


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

lancew

Alright, I'm having another issue.  Heres the code in question:


//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?

EgonOlsen

This depends on how moveVec is calculated. How might want to try to use horse.getZAxis() instead and use the result as moveVec.

K24A3

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


lancew

Thanks, that did the trick.  Heres the code for reference:


//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...

horse.translate(moveVec);