We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › how do i get the rotations of a camera
Page Index Toggle Pages: 1
how do i get the rotations of a camera? (Read 1835 times)
how do i get the rotations of a camera?
Mar 27th, 2010, 4:34am
 
okay i'm trying to build something a bit similar to Rhonda Forever, i.e. a 3D drawing tool. So you basically move a cam around in 3D space like in a first person shooter, using the plane of the monitor as drawing surface (the "crosshair" is not fixed but you can move it around the screen). For this i need to translate the drawing surface according to where the camera looks at, and then rotate it correctly so it appears as a HUD. But obviously i cannot use push-/popMatrix() and translate()/rotate(), because i need to be able to use the actual position of the cursor on screen to gather the according spot in 3D space in order to draw into it.

So i'm using vectors… the easy part was to translate the center of the drawing surface so it always appears in front of the camera:

(using a custom camera class expanded from http://www.zacharyseldess.com . it behaves mostly like a standard FPS, i.e. doesn't roll)

Code:

PVector diff = PVector.sub(cam.lookAt, cam.position);
diff.normalize();
diff.mult((height/2.0) / tan(PI*60.0 / 360.0));
centerPos.set(cam.position.x+diff.x, cam.position.y+diff.y, cam.position.z+diff.z);


so since i can let something float in front of the cam, this diff vector contains the orientation of the cam in 3D space, right?

i would now get the 3D coordinate for drawing by *somehow* combining the rotations of the cam with the x/y position of the cursor and adding that to the centerPos vector?

but how?  Cheesy
Re: how do i get the rotations of a camera?
Reply #1 - Mar 28th, 2010, 1:43pm
 
Hello!

Do you know peasyCam? It is a cool library.

It is not exactly what you need, but it's nice -
I use it all the time.

You can rotate, pan and zoom - all with the mouse.
As long as you don't have your code working, use it.
You'll be impressed.

Greetings,

Chrisir   Wink
Re: how do i get the rotations of a camera?
Reply #2 - Mar 28th, 2010, 4:03pm
 
Or you can use OCD lib
Code:
Camera cam;
cam = new Camera(this, 0,0,300);


in draw
Code:
cam.circle(0.01);
cam.feed();



thats it Smiley
Re: how do i get the rotations of a camera?
Reply #3 - Mar 29th, 2010, 6:25am
 
One bit of info that will probably be useful, is that by default the view angle is 60degrees. I believe this is 60' on whichever screen-dimension is largest, so if your width is greater than height, the angle is 60' across and the height angle is 60/width*height.

If you take those angles, and some assume distance from the camera to the drawing plane, and some vector from the camera towards it (and an up-vector), then it becomes just some trigonometry to work out what screen x/y position.

It won't be the easiest trig, but should be doable.

The "left" vector (that is going from the centre of the screen towards the left) will be the cross-product of the camera direction vector, and the camera up-vector.

The "width" of the canvas is simple too, if it's 200 units away, and the FOV is 60' horizontally, then split it into two right angled triangles, you have 200 on the adjacent edge, and an angle of 30', so you the half-width is arctan(30' in radians)*200. Double that and you have the total width.

Normalise your "left" vector.. that's now one unit along the width of the canvas.

Find out what one pixel on screen means in terms of canvas width (e.g. if the canvas width is 800, and your screen width is 400, then 1 pixel = 2 canvas width units)

The camera position plus the distance*camera look-at vector = middle of canvas.

Work out how many pixels left (or right) of centre the cursor is, and add that *left vector (or that *(-left) ) to the middle of the canvas and voila, you have the world co-ordinates of the mouse's left/right position.

Repeat the same for the up/down position and you have it all in world coordinates.
Re: how do i get the rotations of a camera?
Reply #4 - Mar 29th, 2010, 11:19am
 
okay thanks for the answers. i was able to solve this another way, not using the camera rotations though, but JohnG's implementation of gluUnproject:

Back to top
 
 
Page Index Toggle Pages: 1