# Coordinate Space Transformations

1. Jan 31, 2012

### blackwind

Hi,

I hope this is the right forum to post.

My question is, which is the math implied for transforming an objectA local space, to anothers objects local space, and then transforming it to world space?

For example,
Lets say we want to know if objectA is infront of objectB,

So the logic is to convert the objectB position into objectsA local space, so we get a new point(x,y,z). Now, we only need to know if the new "z" values is greater than 0 (assuming in front is represented by the positive z values).

Thats easy. My problem is that i dont really get the math.

For example, lets say we have our objectA at point (5,0,0) in world space.
and our objectB at (10,5,2) in world space.

Before continuing, we rotate our objectA by 90 degrees around Y axis counterclockwise. (So the point would be (0,0,-5) in object space)

Being said that, whats the math involved to get the objectB point position transformed into objectA object space?

By using some game software, The result is (-2,5,5).
Which comes from rotating ObjectA(5,0,05) and gives us (0,0,-5). The we use a "kind" of version of the rotated objectB (-2,5,10). And then we do the addition.

It almost makes sense to me, except that the objectB original point (10,5,2) rotated 90 degrees gives us (2,5,-10). Why do they use (-2,5,10) instead???

A similar problem is for the opposite operation.
Lets say we want to transform the ObjectB point position (10,5,2) relative to ObjectA objects space to world position.
i got that the result is (7,5,-10). Which i 'almost' understand.
Since ObjectB (10,5,2) rotated by 90degrees is (2,5,-10). And then a simple addition of the original (5,0,0) + (2,5,-10) will give us the result (7,5,-10).

What i dont understand is.. why?
Why do we use the unrotated ObjectA point, and the rotated ObjectB point? Why not both rotated points? Or why not both unrotated points?

What is the formula? Whats the real math behind?

Hope i make my self clear.

Thank you!

2. Feb 1, 2012

### chiro

Hey blackwind and welcome to the forums.

The process of doing this kind of thing in a structured and systematic way is through matrices.

Basically what we do is to consider three types of matrices: scaling matrices, rotation matrices and translation matrices which do their respective thing.

When you deal with different coordinate systems within a single coordinate structure (like cartesian x,y,z space) then we have to know how the systems are related through a transformation matrix.

Now in game software we have the camera which means that before we can make use of the camera, everything has to be transferred to world space (from object space) and then to the camera space.

The way this is done is to first convert every object to world space which is done by a translation of some sort.

After this what we do is rotate all of the objects (and points in the world/scene) based on the orientation of the camera. Once this is done we will have objects in our camera that are all on the positive z-axis and from there we can render the scene.

The actual specifics of this are little deep to discuss in one post, but the idea is to basically multiply matrices in the right order to get your final points which you pass to things like your graphics card and physics system to use (like drawing to the screen).

With matrices you have to understand that you apply things 'backwards'. Here is what I mean by this.

Lets say you have a point p. Lets say you have a transform T1 (which is a matrix). You apply this matrix to your point to transform it. To do this you calculate T1 x p.

Now lets say you want to apply another transform on top of that called T2. You then multiply T2 x (T1 x p) which gives you something else.

This is how you think of transformations in the order that they should be done by considering that operations to the right get done before the left and if you do the order wrong then you will end up getting weird results.

If you know about matrices, what they mean, how to do some matrix math (like multiplying by other matrices and by numbers), you will be able to do this pretty quickly.

Once you get to the camera matrix its pretty much the same thing, except that you will have to tell the graphics card what this is and it will do it for you. But you still have to do the work yourself because if you want to make your game run fast (by getting rid of stuff that will never be seen) then this has to be done in the software and not by the graphics card. Also some thing when you do physics.

I used this site a very very long time ago to help learn this stuff, but keep in mind there are a lot more resources to learn this kind of thing. If you go to www.gamedev.net you will probably find a few really good articles on this kind of thing that also show you example code with a lot of coding comments as well as other poster comments.

http://www.ecere.com/3dbhole/

http://www.gamedev.net

If you have any other questions I'll do my best to answer them.

3. Feb 1, 2012

### blackwind

I understand matrix operations.

But this specific process isnt clear to me. Also I dont need code, only the math.

In the example i posted, for converting the objectB point position transformed into objectA object space, its seems that objectB is first rotated -90 degrees and then only is applied some vector addition to the original objectA position withouth rotation. But the original rotation is 90 degrees (not -90).

So it seems like the math behind is something like:
assuming RotationMatrixAroundY is:
(1, 0, 0, 0)
(0, cosAngle, -sinAngle ,0 )
(0, sinAngle, cosAngle, 0 )
(0,0,0,1)

resultingPoint = (RotationMatrixAroundY(angle) * ObjectA ) + (RotationMatrixAroundY(-angle) * ObjectBPoint)

But i thought it would be something like:
resultingPoint = (RotationMatrixAroundY(angle) * ObjectA ) + (RotationMatrixAroundY(angle) * ObjectBPoint)

(the difference is that in the first its used one negative angle). Why?

And the inverse operation (the second example, transform the ObjectB point position relative to ObjectA objects space to world position)
Seems like the math is:

resultingPoint = ObjectAPoint + (RotationMatrixAroundY(angle) * ObjectBPoint )

So, if you could explain why, i would really appreciate it.

P.S: Sorry for not using proper math notation. Hope i made my statements clear.

thank you.

4. Feb 1, 2012

### blackwind

Ok thinking a bit more of the problem, it seems that the real operation is that they use the transpose of the rotationYmatrix
So the equation seems to be like this:

resultingPoint = (RotationMatrixAroundY(angle) * ObjectA ) + (RotationMatrixAroundYTransposed(angle) * ObjectBPoint)

It seems that for converting to object space we need first to convert to the inertial space using the transpose. But i'm not reallly sure...

5. Feb 1, 2012

### blackwind

Hi,

Afters many hours of analyzing and proving, i finally i got the formula and logic behind.
I consulted many books but the only one i found it talks explicity about this is "3d math primer" , but even they do not explain the whole process.

First, for transforming from object space relative to anothers world space.

The direction is simple, we just multiply the current direction times the rotationMatrix. Since direction is not affected by position or scale, we only need that multiplication.
Direction = ObjectVector*RotMatrix

Points do get affected by scaling. So we need to keep in mind that. In my case, i dont scale never, so i didnt "solve" the problem for scaled points.
So after doing the vector*RotMatrix, we add the translation needed to be relative to the reference objects world space.
We dont need to rotate the orignal referencePoint, since if we rotate it and then add, we would be moving in some sort of object space. But we want world space.
So the "formula" (without scaling) would be like this:

NewWorldPoint = ReferencePoint + (ObjectPoint * RotMatrix)

My guess is that if we use scaling, the formula would be something like this:
NewWorldPoint = ReferencePoint + ((ObjectPoint * RotMatrix)*ScalingMatrix)

But i'm not really sure, since i didnt make a test.

Now, for the inverse operation, transforming from world space to be relative to anothers objects object space, its only the opposite procedure.
For example, if we moved to the right and up for transforming to world space, now we have to move to the left and down for transforming to object space.

So for direction, its almost the same process, except that we use the transpose of the rotationMatrix.

localDirection = objectDir * (RotMatrix^T) (where "^T" denotes the transpose of the matrix ) :P

Finally, for the point (without scaling)
NewLocalPoint = (ReferencePoint*(RotMatrix^T) - (ObjectPoint * (RotMatrix^T))

Here we use both rotated because we want object space. So we need the points in object space. The transpose is needed because it would be the "opposite" operation (or equivalent to rotate counterclockwise by the same amount).

I tested for all the possibilities (withouth scaling) and it seems to be working perfect. Also now it makes a lot of sense to me all these operations. Problem solved :)

P.S: Again, sorry for my very informal math notation.

6. Feb 3, 2012

### Synthetix

Hey chiro, I am trying to do the same as blackwind and although I sort of understand how to apply transformations through matrices, I am unsure of how to get two objects in world space to transform in unison using one object's local space.

The best way to explain it is like this:

I have two objects, objectA and objectB. I want to only rotate objectA, but have objectB follow this rotation. In other words, objectB should be "parented" to objectA. The problem is that even though I am only rotating objectA (multiplying objectA's matrix by its own rotation matrix), in order for objectB to be properly positioned in the world space it needs to both rotate and translate. An analogy would be a hinge. The pivot point of the hinge never translates through space, it only rotates. But the end of the hinge makes a long, sweeping motion though space (both translation and rotation) based on the pivot point of the hinge.

I am guessing in order for this to work, I need to have both objectA and objectB reside in the same space. I don't yet know how to do that just through matrix multiplication, or maybe I just don't know what the variables should be.

I'm sure this is a pretty rudimentary task, but I'm just starting out! :tongue2:

7. Feb 3, 2012

### Synthetix

Ok, I at least figured out the rotation. I tried blackwind's formula:

Direction = ObjectVector*RotMatrix

But it didn't work for me as-is. I had to put the rotation matrix on the left side to get it to work:

objectB_matrix = RotMatrix*objectB_matrix

"objectB_matrix" is the object's world matrix. This worked, so at least objectB is pointing in the right direction. However, it hasn't been translated yet so it's still in its old position.

Getting closer!

8. Feb 3, 2012

### chiro

You're right in that the matrix goes on the left hand side of the equation. I think blackwind has implemented it in a strange way or has interpreted it in another way, so yes you are correct in your thinking.

9. Feb 3, 2012

### blackwind

You are right. The matrix goes first. But the api i use needs the vector first.