Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Camera pointed at Origin

Started by kop48 Aug 24, 2006 at 1:41 AM 6 replies 850+ views
Original Post
kop48
kop48
Yeah, last duplicate - not sure where to post this... :-/ Hey, I've been working in Direct3D and am trying to make a camera. I know that D3D supplies a function to do this via LookAtxH, but I would like my camera to rotate the view around the X-axis when the user drags the mouse up and down, but rotate left (not around an axis, but dependent to the current viewer). Currently I have the up and down motion working, but I cannot for the life of me figure out how to get the adapted rotation working. I was originally trying to set a unit vector in the Y-direction, rotate it around the X-axis and then do a RotateAroundAxis transform as well as my RotateAroundX and Translation. This just gives me some weird results and I know that I'm doing something way wrong. The only other thing I can think of is to use polar coordinates and to calculate the position of the camera, then to rotate it around its local x and z axes in order to point it back to the origin, but I'm not too sure how to approach this. Any help would be appreciated.
ToohrVyk
ToohrVyk
A rotation always happens around an axis. What axis do you want to rotate around when you look left and right? The vertical axis, or the transformed vertical axis? Depending on which, you'll have to apply a rotation around Y before or after the rotation around X. Applying it before will perform the rotation around the transformed axis (since the axis is transformed, too) while applying it after will use the untransformed axis.

Also, do you realize than by posting three times in three forums, you mostly annoy people, which decreases your chances of an answer? Post once, and if you didn't find the correct place, your post will be moved (no sweat, it's never a problem).
kop48
kop48
Good point :$.

I would like to rotate around the transformed axis.

At the moment, I'm doing it by rotating around X, then rotating around what I THINK is the transformed axis, but it's not liking it at all, so I'm obviously doing something really wrong.

I'm getting the tranformed axis by rotating a Y-unit vector(0,1,0) around the x axis and using that vector to rotate my view around. However, the results are not even close to what I should be getting.

Thanks for your reply,

ToohrVyk
ToohrVyk
Rotate around the Y axis, then around the X axis. This way, the axis of the first rotation will be transformed by the second rotation.
webjeff
webjeff
kop48,

From where ever you are, if you take the current lookat vector and do a cross product with your right vector, you will get the up vector you can rotate about so it will be left and right around that up vector, not necessarily y.

I was also thinking about what your trying to do. It seems odd, for example, if you had a plane in front of the sun, looking at the sun, then you rotated the plane left 90 degrees. If you were to rotate about the x-axis up and down, it would look weird from the perspective of the plane... is that what you want?

What are you trying to accomplish with the rotations? Maybe that would help me understand what your going for.
Jeff.
kop48
kop48
device.Transform.View = Matrix.RotationY(rotateX) * Matrix.RotationX(rotateY) * Matrix.Translation(0.0f, 0.0f, zoomDistance);


This is what ToohrVyk suggested and what I've already tried to do (not I don't appreciate the help, I just should have said it first).

This doesn't work, as the rotation is still done around the axes themselves and THEN the translation is done. If I do the translation first, the lookAt point is no longer the origin as that is what you end up rotating.

Basically, I would like the functionality to be similar to this (mouse drag, up and down, left and right) - http://jmol.sourceforge.net/demo/atoms/ (needs Java).

Pretty much, I need to have the camera with its own set of axes, still centered on the origin, but with the Z axis being the vector from the camera's position to the origin and the X and Y axes as normals to the new Z plane (can be calculated from current axes to create the plane).

I know my variables say rotateX and rotateY and that's my bad - they actually mean mouseDeltaY, mouseDeltaX. :)

Thanks for the help, guys!
ToohrVyk
ToohrVyk
My approach to this is:

1° Translate the camera back from the origin by the zoom factor.
2° Rotate vertically (around the X-axis)
3° Rotate horizontally (around the Y-axis)
4° Translate so the origin becomes the lookAt point.

And, obviously:

5° Take the inverse of this matrix, and use this as the View matrix.
kop48
kop48
I'm not sure if that's going to accomplish what I need it to do. The reason I think this is that it is still doing the rotations around the Y axis of the world, not the locational axis of the camera. I've been thinking about this, and I *think* what I need to do is to calculate the camera's relative axes and the do rotations around those... This doesn't mean I won't listen to what you're saying btw, it's just I either don't understand it well enough, or it's simply doing what the lookAt function does already.

The other option, which I read in an OpenGL textbook, but didn't seem to work too well, was to calculate the camera's location compared to the old location, then use the normal of the plane between the two points to rotate around the angle between the two vectors. I tried to do it, but I really can't remember my maths very well. :'(

Thanks for your help!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.