Hello,
I am trying to create a helper class that will attach camera to an object and allow to do following things:
1. show object from different angles (orbit camera around object)
2. follow the object (ie. when object moves, camera is also moving after it)
3. zoom in / zoom out
4. ignore object rotations (ie. when object is rotating, the camera is rotating with it, so it is looking always at the same point).
With your help (especialy paulscode - thanks a lot btw), I have created a simple class that does the most of that functionality (except 4th point).
Recently I started correcting it to make the 1st point working better and to implement missing one.
Well I have made a first thing working ok, but because of my lacks of 3d math knowledge I stucked in the last task.
I do not know how to get horizontal rotation angle (Y) and vertical rotation angle (Z) by which object3D is rotated. I need those angles to be able to rotate camera.
Can anybody help me with it?
I am asking about angles because matrix operations are still a little bit too difficult for me to understand so I have made my class working in following way:
1. During the initialization, I am positioning the camera after object
SimpleVector vec = new SimpleVector(0, -getDistance(), 0);
vec.add(object.getTransformedCenter());
cam.setPosition(vec);
and setting the camera orientation by:
cam.setOrientation(new SimpleVector(0, 1, 0), new SimpleVector(0, 0, -1));
2. During the update of camera position (just before rendering) I am setting camera to the same point as object is and move it out by getDistance():
cam.setPosition(target.getTransformedCenter());
cam.moveCamera(Camera.CAMERA_MOVEOUT, getDistance());
3. During camera rotation (orbiting effect) I am:
* moving camera in by getDistance(), saving its position, and setting pos to [0,0,0]
* rotating back X and Y by angles the camera was rotated in previous step
* increasing angles by delta values
* rotating Y and X by new angles
* restoring camera last position by moving camera out and adding saved position
private void restoreCameraPosition(Camera cam, SimpleVector shift)
{
cam.moveCamera(Camera.CAMERA_MOVEOUT, getDistance());
shift.add(cam.getPosition());
cam.setPosition(shift);
}
private SimpleVector moveCameraToZeroPosition(Camera cam)
{
cam.moveCamera(Camera.CAMERA_MOVEIN, getDistance());
SimpleVector shift = cam.getPosition();
cam.setPosition(new SimpleVector(0, 0, 0));
return shift;
}
@Override
public void rotate(double deltaX, double deltaY)
{
Camera cam = getCamera();
SimpleVector shift = moveCameraToZeroPosition(cam);
cam.rotateX((float) -vAngle);
cam.rotateY((float) -hAngle);
vAngle = limitVerticalAngle(MathUtil.normalizeAngle(vAngle + deltaY
/ 200));
hAngle = MathUtil.normalizeAngle(hAngle - deltaX / 200);
cam.rotateY((float) hAngle);
cam.rotateX((float) vAngle);
restoreCameraPosition(cam, shift);
}
Now, what I am planning to do is to modify the method that is updating camera position and, get (somehow) the rotation angles from target object and apply them to camera in the same way as I am doing it during camera rotation (ie. I will add them to the stored hAngle and vAngle). Probably I will have to store both angles separately to distinguish what angle is related to change the camera orbit position and what is related to object rotation.
When I finish that work I will post the full code for that class hoping that it may be usefull for others too...
Thanks,
Wojtek