Any 3D programmers? Need some help...

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
I'm currently building a 3D game editor at work, and I'm stuck at object rotating.

I am trying to get a similar object rotating system as 3D Studio MAX's, where you rotate the objects around the viewport's axis. However, each of my objects is defined by its own rotation parameters, which revolves around the object's axis.

I can't figure out the conversions required to convert the rotation parameters so as to make the object rotate around the viewport's axis instead of its own axis.

Anyone?


Thanks in advance,
:)atwl
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
assuming cartesian coordinate system ...

(x, y, z) - original coordinates
(xp, yp, zp) - translated coordinates
phi - angle of rotation


x axis rotation ( on the yz plane )

xp = x
yp = y*cos(phi) - z*sin(phi)
zp = z*sin(phi) + z*cos(phi)

y axis rotation ( xz plane )

xp = z*sin(phi) + x*cos(phi)
yp = y
zp = z*cos(phi) - x*sin(phi)

z axis ( xy plane )

xp = x*cos(phi) - y*sin(phi)
yp = x*sin(phi) + y*cos(phi)
zp = z

that should do it ( as MAX rotation is around major axis ). Things get a little more complicated when you go into arbitrary axis rotation. anyhow, good luck

btw, what API are you using for the engine?
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Thanks for your detailed explanation, but that's not exactly what I wanted because I am not directly manipulating the object's individual vertices.

I basically have an entire mesh stored encapsulated in a class, sort of like this:
class CMeshObject
{
CMesh m_mesh;
CVector m_vRotate;
CVector m_vTranslate;
CVector m_vScale;
CMatrix GetMatrix();
};

When GetMatrix() is called, it will return the object matrix based on the rotate, scale and translate functions. Hence, when I am doing a rotation I am only altering the m_vRotate variable (which is basically a D3DVECTOR type). The renderer will handle all the rest when the object is rendered to screen.

I obtain the rotation parameters via mouse X-Y movements, which I map to X-Y, X-Z or Y-Z plane based on the viewport (whether it is Top, Front or Left, etc). From there, I am stuck because I don't know how to convert the rotation parameters further to simulate the object rotating about the viewport's axis.

BTW I'm using D3D for the renderer.


Thanks in advance again,
:)atwl
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
ahh, i see, well my previous post was almost helpfull, but let me explain first :) ... tranformation matrix based on rotation depends on the pivot point. In your case you are assuming that the rotation occurs always around the center of the object - this would be a local reference coordinate system, hence when someone calls the Rotate(...) method the object translation as well as scale vectors stay fixed, while the new values are applied to the rotation vector member. In case of world ( absolute ) reference coordinate system both rotation and translation vectors get affected. The translated coordinates formulae are located in my prior post, for example an object with original rotaion vector <90, 0, 0> and translation v <50, 10, 0> rotated around the z axis by 90 degrees holds ( like expected ) <90, 0, 90> rotation vector, but also <-10, 50, 0> translation one. why?

z axis ( xy plane )

xp = x*cos(phi) - y*sin(phi)
yp = x*sin(phi) + y*cos(phi)
zp = z

or

xp = 50*cos(90) - 10*sin(90) = -10
yp = 50*sin(90) + 10*cos(90) = 50
zp = z = 0

hope, this was what you wanted :)
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Well, now you've got me confused! ;)

Actually no, that's not what I wanted either. Sorry... but I'll try again here:

Imagine I have a pyramid, pointing 45degrees to the right and 45degrees downwards. This would be equivalent to the pseudo-function m_Object.SetRotate(-45.0f, 45.0f, 0.0f), assuming my numbering system is in degrees and not radians for simplicity.

Now in 3D Studio MAX, when I apply a Y-axis rotation to the object, I would expect the object to rotate around a vertical line (Y-axis) that passes through the object's center.

However, in my editor, if I just added another x degrees to the rotation, i.e. SetRotate(-45.0f, 45.0f + x, 0.0f), it would rotate around a vector in the direction of (1.0f, 1.0f, 0.0f) which is not what I wanted.

So I need to calculate, from x, three angles a, b, c so that the rotation SetRotate(-45.0f + a, 45.0f + b, 0.0f + c) will make the object rotate around the vertical axis.

Okay, sounds messy doesn't it? To tell you the truth, I'd rather manipulate individual vertices instead of having to juggle around with transform matrices, but I wasn't the one who built the renderer in the first place so I can't do much about it. So here I am stuck trying to juggle with meshes and matrices instead.

BTW Thanks for your input so far. I hope you can help me with this because I'm totally stumped!


Thanks again,
:)atwl