Soundbomber's picture

[Solved] Rotation axis

How do I find a Vector that represents an axis corresponding to a 2D x and y axis in 3D space.
To explain, I have a 3D model which I am viewing through a camera position which is variable (variable eye position).
So I could be looking at my 3D object from above and to the right, or below and to the left, and anywhere inbetween.
I want to rotate my object about what would be the screen x or y axis in 2D.
Any suggestions?

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Entropy's picture

Are you saying you want to confine a 3d object to a single 2d plane?

If you restrict the object to a 2d plane such that a it's z-position never changes, and have it only ever facing in a direction on that plane, the only rotation-axis you'll need is Vector3.UnitZ.

If you need something more general (e,g. if for some reason you had two or more non-parallel 2d planes) then you can still use these basic coordinates to specify the x,y position of units, in these planes, but just carry out a transform on the modelview matrix to set it up prior to drawing each plane.

You should carry out the camera transforms first. All other object transforms will be independent the camera position.

It's all about simplifying the maths as much as possible - it not only makes things easier to understand, it will also optimise your code.

Entropy's picture

Quick basic example:

DrawScene(double Time)
{
   Camera.SetPosition(Time);
   Player.Draw(Time); 
   foreach(BadGuy bad_guy in BadGuys)
      bad_guy.Draw(Time);
}
 
Player.Draw(double Time)
{
   GL.PushMatrix();
   //Transforms
   //Drawing method
   GL.PushMatrix();
}
Soundbomber's picture

I dont want to confine my 3D object to a single plane but I want to confine the rotation of the object (viewed from any position) to the screen x and y planes.
If you look at the attached sketch, I want to be able to rotate my 3d object about the screen x and y planes (GL.Rotate(angle,NewVector)). A little bit like the effect you get with a quaternion camera only clamped to x and y I think.

AttachmentSize
Graphic1.jpg85.85 KB
Entropy's picture

So you'd to find two rotational axes - one corresponding to to the camera-orientation's X-direction, and the other to the camera-orientation's y-direction?

If you have a Matrix4 called Transforms which defines the camera's position and orientation, try rotating around new Vector3(Transforms.Row0), new Vector3(Transforms.Row1) and new Vector3(Transforms.Row2).

These are world position vectors the define the x, y and z axes of the camera.

edit: Why do I keep confusing classes and instances in my forum posts? Never mind. Corrected above.

Soundbomber's picture

Thanks. Yes thats exactly it. I will give it a try.

Soundbomber's picture

Tried it and it just seems to be rotating about world coordinates.
I retrieved my cameras transform matrix using

Dim oMatrix As Matrix4
GL.GetFloat(GetPName.ModelviewMatrix, oMatrix)

should this work?

Entropy's picture
Soundbomber wrote:

Tried it and it just seems to be rotating about world coordinates.
I retrieved my cameras transform matrix using

Dim oMatrix As Matrix4
GL.GetFloat(GetPName.ModelviewMatrix, oMatrix)

should this work?

Depends:
-Is there an overload for GL.GetFloat that takes a Matrix4 by reference?
-You should probably call it immediately after setting up your camera position matrix, but before you make any other transforms.
-If in doubt, output the Matrix4.ToString() as debug text to your screen to see if the matrix you're using does seem to correspond to what you'd expect.

I don't think it's too efficient, but all my 3d objects (and the camera) have a Transforms Matri4 object which I carry out all the required operations on outside of OpenGL, then I call GL.MultMatrix(ref Transforms) (in C#) before rendering. This method does, at least, make it easy to find the world coordinates and relative axes of an object.

Just thought of something: GL.Rotate(...) rotates the specified matrix around the point(0,0,0) so if you translate the matrix to Position(0,0,0), rotate, and translate back to the desired position, does it work then? Very inefficient, I realise, but if it works it takes you a step closer to where you want to be.

Soundbomber's picture

Yes GL.GetFloat does have a Matrix4 overload by reference.
My scene is centred around 0,0,0. Indeed this is where I want to rotate around.
Just tried placing my Matrix calc directly after the camera transform and it works a treat.
Thank you very much.

Entropy's picture
Soundbomber wrote:

Yes GL.GetFloat does have a Matrix4 overload by reference.

I've learnt from you, too :-)

This'll make my OpenGL programs much more efficient...

bindumol's picture

Hi All,
I have a problem with the quad rotation using mouse.
During rotation it hides the quad...

Can you help me to solve this..
I used this code for quad drawing
GL.Color3(System.Drawing.Color.Gold);
GL.Begin(BeginMode.Quads);
GL.Vertex3(-1.0, -1.0, -3.0);
GL.Vertex3(1.0, -1.0, -3.0);
GL.Vertex3(1.0, 1.0, -3.0);
GL.Vertex3(-1.0, 1.0, -3.0);
GL.End();

Thanks,
Bindumol

AttachmentSize
screenshot2.JPG20.27 KB
sreenshot1.JPG25.37 KB