Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


local vs world coordinate system for object manipulation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 garrus   Members   -  Reputation: 113

Like
0Likes
Like

Posted 21 February 2013 - 01:45 PM

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?



Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2645

Like
0Likes
Like

Posted 21 February 2013 - 01:53 PM

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


#3 garrus   Members   -  Reputation: 113

Like
0Likes
Like

Posted 21 February 2013 - 03:18 PM

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?



#4 Nercury   Crossbones+   -  Reputation: 798

Like
0Likes
Like

Posted 21 February 2013 - 05:00 PM

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.


Edited by Nercury, 26 February 2013 - 03:57 PM.


#5 TheChubu   Crossbones+   -  Reputation: 4849

Like
1Likes
Like

Posted 21 February 2013 - 05:19 PM

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


#6 BornToCode   Members   -  Reputation: 951

Like
0Likes
Like

Posted 21 February 2013 - 11:50 PM

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.


Edited by BornToCode, 21 February 2013 - 11:50 PM.


#7 garrus   Members   -  Reputation: 113

Like
0Likes
Like

Posted 22 February 2013 - 02:14 AM

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?



#8 Nercury   Crossbones+   -  Reputation: 798

Like
0Likes
Like

Posted 22 February 2013 - 04:50 AM

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.



#9 garrus   Members   -  Reputation: 113

Like
0Likes
Like

Posted 26 February 2013 - 03:38 PM


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?


Edited by garrus, 26 February 2013 - 03:39 PM.


#10 Nercury   Crossbones+   -  Reputation: 798

Like
0Likes
Like

Posted 26 February 2013 - 04:17 PM



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.

Edited by Nercury, 26 February 2013 - 04:28 PM.


#11 Nercury   Crossbones+   -  Reputation: 798

Like
0Likes
Like

Posted 26 February 2013 - 04:34 PM

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?

Edit: I guess I should have answered YES. I am actually not sure if maintaining transformation matrix with both rotation and translation for EVERY object has any benefits versus simply calling glTranslate, glRotate.

#12 garrus   Members   -  Reputation: 113

Like
1Likes
Like

Posted 28 February 2013 - 05:58 AM

Thanks Nercury smile.png

^ meant simply the transpose (to get a column vector from [0 0 -1] )

 

To store each rotation, i'd imagine you mantain 3 angles for the object, which you modify according to user input, and from these you transform the unit vectors of the axes 

( [1 0 0] , [0 1 0] , [0 0 1])  to get the transformed orientation.

 


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?

Edit: I guess I should have answered YES. I am actually not sure if maintaining transformation matrix with both rotation and translation for EVERY object has any benefits versus simply calling glTranslate, glRotate.

 

I meant, keeping such a matrix for every object, and calling glMatrixMult before drawing the object.

If you didn't do this, wouldn't you need to call a glTranslate and 3 glRotates for every object either way?


Edited by garrus, 28 February 2013 - 06:54 AM.


#13 Nercury   Crossbones+   -  Reputation: 798

Like
0Likes
Like

Posted 28 February 2013 - 07:52 AM

I meant, keeping such a matrix for every object, and calling glMatrixMult before drawing the object.
If you didn't do this, wouldn't you need to call a glTranslate and 3 glRotates for every object either way?

You are right, I would smile.png

To store each rotation, i'd imagine you mantain 3 angles for the object, which you modify according to user input, and from these you transform the unit vectors of the axes
( [1 0 0] , [0 1 0] , [0 0 1]) to get the transformed orientation.

Yeah, I am keeping euler for such objects like my camera, because they better map to mouse input (heading - X, pitch - Y).
I think most of my other objects will not need euler angles at all, so I am planning on storing only the quaternion which represents current rotation.

Edited by Nercury, 28 February 2013 - 07:58 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS