local vs world coordinate system for object manipulation

Started by
11 comments, last by Nercury 11 years, 1 month ago

I used to do everything using vectors defined in the world coordinate system, but i'm reading up more and more about manipulating objects in their local coordinates.

For example, define every object looking towards the + Z axis, and the up direction the +Y axis, so as to simplify actions like "move forward" or "jump".

Do you have any tips/material/advice on how that's implemented?

I'm guessing i'll have to center the object to the world as mentioned above to begin with?

Advertisement

Usually objects rotations are represented using matrices.

Matrices basically contain the direction of each local axis of an object in world space.

So if your object is not rotated, its local Y axis direction is 0,1,0, X 1,0,0 and X 0,0,1.

If you rotated it upside down the local y axis would be 0,-1,0 (as from the objects perspective, up is down) and possibly X and Z changing (so it wont mirror on some axis somehow...)

You can use them for things like "where in world coordinates is this point, that is at these coordinates from the objects point of view?"

So yeah, read up on matrices for object positioning/rotation.

o3o

Usually objects rotations are represented using matrices.

Matrices basically contain the direction of each local axis of an object in world space.

So if your object is not rotated, its local Y axis direction is 0,1,0, X 1,0,0 and X 0,0,1.

If you rotated it upside down the local y axis would be 0,-1,0 (as from the objects perspective, up is down) and possibly X and Z changing (so it wont mirror on some axis somehow...)

You can use them for things like "where in world coordinates is this point, that is at these coordinates from the objects point of view?"

So yeah, read up on matrices for object positioning/rotation.

Thanks for the reply!

Yeah, i know about matrices :) , but as i'm having problems due to doing everything in world space, using a left,forward..etc vector to perform rotations and displacements for each objects,

as i unsuccessfully inquired about here.

So i thought that it'd be easier doing the above around the (local object axes, and then simply transforming them to world space representation.


You can use them for things like "where in world coordinates is this point, that is at these coordinates from the objects point of view?"

Isn't that a single translation,using the object's position?

I am also a bit newbie in this, but let me describe the way I do it.
I can convert angle (Euler) rotation to quaternion like this:


Quaternion qHeading(Vector3(0.0f, 0.0f, 1.0f), headingRadians); // my "up" is Z
Quaternion qPitch(Vector3(1.0f, 0.0f, 0.0f), pitchRadians); // my "up down" rotates around X
Quaternion qRoll(Vector3(0.0f, 1.0f, 0.0f), rollRadians); // my "roll" around Y
auto rot = qHeading * qPitch * qRoll;


I can convert quaternion rotation to analogical matrix transformation with my math lib:


Matrix4 rotMatrix;
rot.to4x4Matrix(&rotMatrix);


I can convert my position to another matrix transformation:


Vector3 pos(15, 0, 0); // example position

Matrix4 posMatrix;
pos.to4x4Matrix(&posMatrix);
 


Now, I can get a single matrix which represents BOTH transformations by multiplying them:


Matrix4 finalMatrix = rotMatrix * posMatrix;


And I can use this matrix in OpenGL to do both transformations in one call:


glMultMatrixf(finalMatrix.element); // "element" here is matrix in OpenGL compatible 4x4 array


Now, back to rotations: Note how Quaternion rotation is just composition of different rotations about arbitrary vectors.
You can easily rotate Quaternion around any axis just like so:


Quaternion newRot(Vector3(1.0f, 0.0f, 0.0f), PI / 2); // 90 degrees around X
rot *= newRot;


If you read more about matrices you can do a lot of optimizations to these operations.
You can get back the Euler angles from Quaternion if needed. I find Euler angles still required for objects such as camera.

Now, if my object is actually a collection of other objects rotated and positioned (transformed) with this matrix, this is the way to render it:


glPushMatrix(); // save current OpenGL matrix to stack

glMultMatrixf(objectPosAndRot); // move into object's position and rotation
// do the same for each object in collection! (push, transform, pop)

glPopMatrix(); // restore previous matrix


And for camera's positioning and rotation, you do the same with inverted matrix at the beginning of the scene.
Hopefully I covered it right.

So i thought that it'd be easier doing the above around the (local object axes, and then simply transforming them to world space representation.


That is exactly what happens. But think of it like this:


glPushMatrix(); // saved current world transformation matrix
glTranslate(); // moved into object's position
glRotate(); // rotated in local space
glPopMatrix(); // cancel all transformations, move back to world space

And, ugh, to avoid any confusion, I am doing exactly the same with math lib (Matrix4 finalMatrix = rotMatrix * posMatrix). Well, order of operations happens to be reverse there.

World space is for positioning your objects around the world. You should deal with specific aspects of your meshes in their own model space (ie, their scale, their orientation). Once you set up that, then you can position that object on your world.

Otherwise you'd be rotating an object with the world as reference coordinate system, which it isn't what you'd do if you wanted to, say, turn the mesh upside down.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

World cordinate never changes, but local coordinate is relative to the object you are modifying. For example if your up vector is represented by 0,1,0 in world it will always be 0,1,0 even though your orientation may have changed.

So i mantain,say,a 4x4 matrix to store the object's orientation and displacement , edit that matrix to rotate/move the object, and glRotate,glTranslate accordingly before drawing the object?

So i mantain,say,a 4x4 matrix to store the object's orientation and displacement , edit that matrix to rotate/move the object, and glRotate,glTranslate accordingly before drawing the object?

Look into my previous post's history, it may give you some ideas.


So i mantain,say,a 4x4 matrix to store the object's orientation and displacement , edit that matrix to rotate/move the object, and glRotate,glTranslate accordingly before drawing the object?

Look into my previous post's history, it may give you some ideas.

Thanks, that helped smile.png

So is the following correct?

So If i want to move an object to the direction it's facing, since i don't have a "forward" vector explicitly,

i assume that all objects begin facing a certain direciton.

For example -Z direction, and the current direction they're facing is [0 0 -1]^T times their rotation matrix?

Is this how it's done generally?

If so, isn't it processor-intensive to compute all orientations from this?



So i mantain,say,a 4x4 matrix to store the object's orientation and displacement , edit that matrix to rotate/move the object, and glRotate,glTranslate accordingly before drawing the object?


Look into my previous post's history, it may give you some ideas.



Thanks, that helped smile.png

So is the following correct?
So If i want to move an object to the direction it's facing, since i don't have a "forward" vector explicitly,
i assume that all objects begin facing a certain direciton.

For example -Z direction, and the current direction they're facing is [0 0 -1]^T times their rotation matrix?

Is this how it's done generally?
If so, isn't it processor-intensive to compute all orientations from this?



If ^ meant transformation by matrix, then yes. Let me iterate to avoid any error:
If you want to move, lets say, a camera -Z units back (in it's rotated state), you simply transform this movement vector (0, 0, -1) by camera's current rotation matrix, and just do pos += that_vector.
pos += vec3(0, 0, -1) ^ rotation; // ^ == transform
I have no idea "how it is done", I find this easy for me and just try to minimize the amount of things I do smile.png. Ok, I started by modding Unreal so I guess I got some concepts in my head from that.
As for performance, I think this kind of operation goes like butter in C++. It is bunch of additions and multiplications.

This topic is closed to new replies.

Advertisement