Sunday 22 July 2007

Camera please look there, right?

Huh, after some hours and hours spending with jme Camera settings finally I got it to the needs of a Classic Input Handling turning/looking this and that way... In the end (after many rethinking how it may work and how it should be done and several different coding approach that failed) I could create the right rotation matrix calculated from an origo direction vector and the new direction vector, thus I could multiply (rotate) the Camera's Left and Up axis (which is a must for culling the 3D scenario). The Left and Up and the Direction must be a valid X/Y/Z axis and must describe a correct view of the scenario in the given direction, or the culling will remove parts of the scenario that should be visible...Now let me share my final code that was good for turning AND for looking up and down (the last one needed a two step rotation) (notice that there is a tricky -1 multiplication if the to direction is the original direction's opposite):

/**
* Sets a camera's direction to a new x,y,z dir, setting its Up and Left too with rotation matrix with an internal direction setting,
* good for look up/down (needs a two step rotation).
*/
private void setCameraDirection(Camera camera, Vector3f internalDirection,
float x, float y, float z) {
Matrix3f rotMat = new Matrix3f();
Vector3f dirOrigo = new Vector3f(0, 0, -1);
Vector3f left = new Vector3f(-1, 0, 0);
Vector3f up = new Vector3f(0, 1, 0);

if (internalDirection != null) {
Vector3f dirNew = internalDirection;
dirNew.normalizeLocal();
rotMat.fromStartEndVectors(dirOrigo, dirNew);

rotMat.mult(left, left);
rotMat.mult(up, up);

up.normalize();
left.normalize();
dirOrigo = dirNew;
}

Vector3f dirNew = new Vector3f(x, y, z);
dirNew.normalizeLocal();
rotMat.fromStartEndVectors(dirOrigo, dirNew);

rotMat.mult(left, left);
rotMat.mult(up, up);

// this code is needed for Y axis bottom-down problem...
if (internalDirection != null && internalDirection.x == 0
&& internalDirection.y == 0 && internalDirection.z == 1) {
left.multLocal(-1);
up.multLocal(-1);

} else if (dirNew.x == 0 && dirNew.y == 0 && dirNew.z == 1) {
left.multLocal(-1);
up.multLocal(-1);
}

up.normalize();
left.normalize();

camera.setDirection(dirNew);
camera.setUp(up);
camera.setLeft(left);
camera.normalize();

}

To summarize this will mean correct culling (now with look up and down too!) and more FPS! :-D

For those who are under windows and want to try the pre-alpha snapshot release and don't want to buy rar to uncompress the jcrpg.tar.bz2 file I will post here a link to a free bz2 utility: http://gnuwin32.sourceforge.net/packages/bzip2.htm

No comments:

Twitter