jPCT-AE - a 3d engine for Android > Support

question about object3d.translate

(1/2) > >>

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:

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

--- End code ---

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


--- End code ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version