### Author Topic: How to get the "up" - part of the camera?  (Read 4840 times)

#### Sebastian

• byte
• Posts: 6
##### How to get the "up" - part of the camera?
« on: September 21, 2006, 05:29:37 pm »
Hi,

a camera in 3d space can be defined by position, direction and up (the orientation). I found no way to access this up information in JPCT, and this is especially missing in the camera.lookAt(obj) method.

I need this for implementing a camera that can be rotated around a given object with the mouse. I wrote the following code for rotating around a object at the x axis:

Code: [Select]
`      SimpleVector obj = object.getTransformedCenter();      SimpleVector cam = camera.getPosition();      SimpleVector v = cam.calcSub(obj);            Matrix m = new Matrix();      m.rotateX(dx);      v.matMul(m);      v.add(obj);      camera.setPosition(v);      camera.lookAt(obj);`

When I rotate around an object this way, the orientation flips around (upside-down) when I get close to a 180 degree rotation.

I think that this information is somehow stored in the camera.getBack() rotation matrix, but the math part of my has been shrunken since school  :oops:

By the way, did anyone use quaternions? I found some nice articles in the web about this, but I don't know how to integrate this with JPCT right know, I think there wait a lot of little traps like the column-or-row matrix question and such things when converting those examples from DirectX to the java JPCT world.

Thanks for any help

Sebastian[/code][/quote]

#### EgonOlsen

• Posts: 12258
##### How to get the "up" - part of the camera?
« Reply #1 on: September 22, 2006, 12:05:56 am »
Code: [Select]
`camera.getYAxis()` should get you the axis you want...maybe. But keep in mind, that the camera's rotation matrix is actually a rotation matrix to rotate the world in place. So if anything should rotate in world space like the camera does, you have to use the inverse matrix.
Here's some code that may do what you need to get you started:
Code: [Select]
`camera.setPosition(car.getTransformedCenter());camera.rotateX(0.03f);Matrix m=camera.getBack();m=m.invert3x3();camera.moveCamera(m.getZAxis(), -100f);`

About quaternions: I haven't ever really used them and never felt the need to do so. They are (albeit some tutorials may tell you different things) mathematically equivalent to matrices. That doesn't mean that some things may not be easier to implement by using quats. Matrices in jPCT are row major, DX matrices should be row-major too while OpenGL matrices are column-major.

#### Sebastian

• byte
• Posts: 6
##### How to get the "up" - part of the camera?
« Reply #2 on: September 22, 2006, 09:35:42 pm »
Thank you for the quick response, I will keep the inverse matrix in mind, it seems to be realy important. But sadly I don't get it to work. I think it would be great if it would be possible to set the up-orientation as a second parameter to the lookAt - method like it is possible in direct x, what do you think?

I integrated the code you showed me, and it works for the x axis, but funnily not for the other axes. But ok, that may be just my lack of understanding.

I tried to understand my problem from the root, and a lot of it is just my own stupidity. So instead of rotating the camera I tried a simpler task: rotating an object around another. But even this is something I don't get to work. Please have a look at the following code:

Code: [Select]
`      // pointer is a little pyramid that should rotate around an object      SimpleVector p = pointer.getTransformedCenter();      // object is the rotation center      SimpleVector o = object.getTransformedCenter();      // v is the vector from the object to the pointer      SimpleVector v = p.calcSub(o);      // this shows that v.length is not constant!      System.out.println(p+" "+v.length());      // make a copy of v      SimpleVector w = new SimpleVector(v);      // rotate the copy a little bit      w.rotateY(dy);      // set the pointer to the new position = move it the difference      // between old and new position      pointer.translate(v.calcSub(w));`

I let this code run every time I move the mouse. The problem is that it is not stable - the pointer rotates around the object, but it is also moving away from it (length of v is increasing). If I comment the rotation line out, than it is stable (ok, it does nothing then). Can this be a number precision error? What would be the best way to do this task? Another question is how to set an object to a new Position. A camera has a position, but an object does not have a setTransformedCenter-Method or something like that, and that is the reason I came to this translation construct.

Thanks for your help for this bloody beginner

Sebastian

#### EgonOlsen

• Posts: 12258
##### How to get the "up" - part of the camera?
« Reply #3 on: September 22, 2006, 09:47:44 pm »
I haven't looked at the code very closely yet, but why don't you use the rotation pivot for this task instead? That's the point around which an object will be rotated. It's given in object space, so if both objects have a center of (0,0,0) (in object space, not in world space) and you place object1 at (0,0,0) in world space and object2 at (10,10,0) in world space, object2's rotation pivot should be set to (-10,-10,0) to make it rotate around object1's center. One can easily mix up the spaces, so it can be a bit tricky to find the correct pivot, but once you have it, things should be easier. Another possibility: Place a dummy object at the center (in world space) of object1 and make object2 a child of it. Then rotate that dummy object.

#### Sebastian

• byte
• Posts: 6
##### How to get the "up" - part of the camera?
« Reply #4 on: September 25, 2006, 03:36:16 pm »
The rotation pivot is what I was looking for, thank you. Now I use a dummy object to rotate around the other object and place and align the camera to this object.

May I ask why the camera, object3d and light does not have a common interface? If they had a common interface, all could be used the same way with the same code.