Game Development Community

World/local coordinate transformation

by Liu Yi · in Torque Game Engine · 06/04/2006 (11:05 pm) · 3 replies

Suppose I know the local coordinate of a point in a Item object. How can I get the world coordinate of this point in the engine code?

Thanks.

#1
06/09/2006 (8:51 pm)
In the engine it's done with mObjToWorld.

Somewhere in the Item code if you wanted to get it's world coordinates you would have code that looks something that looks like

Point3F worldItemPos;
mObjToWorld.getColumn(3,&worldItemPos);

Or if it's a local coordinate in general in terms of the Item's coordinates you can just mul().

mObjToWorld.mulV(&somePosLocalToItem);

- Eric
#2
06/09/2006 (11:43 pm)
Yes, but note that mulV is for vectors, while mulP is for points. Also, mulP and mulV take their arguments as references, so nix the &. Just:
mObjToWorld.mulP(pointToTranslate);

It may also be worth mentioning here that there are two transform matrices associated with each object: mObjToWorld and mWorldToObj. (Well, four actually, if you count the render transforms mRenderObjToWorld and mRenderWorldToObj. Render transforms are client-only and are updated every frame, whereas the regular non-render transforms are present on both client and server and are updated once per tick). These transform matrices can be used to translate world coordinates to object coordinates, and vice versa. Their names reflect their function. mObjToWorld translates object space to world space, while mWorldToObj is the reverse: world space to object space. Nicely named, aren't they? (Of course, all this is documented in sceneObject.h).

These matrices can be accessed from outside the object's class with the following functions:
objectPointer->getTransform() returns object's mObjToWorld
objectPointer->getWorldTransform() returns object's mWorldToObj
objectPointer->getRenderTransform() returns object's mRenderObjToWorld
objectPointer->getRenderWorldTransform() returns object's mRenderWorldToObj
or in script:
object.getTransform() returns object's mObjToWorld (as a matrix in script form: "posX posY posZ axisX axisY axisZ rotationAngle")

Strangely, Torque Script seems to be missing a getWorldTransform function. (Somebody correct me if I'm wrong). I'm not surprised that the render transforms aren't available from script, but I am surprised by the lack of a getWorldTransform. Granted, I'm just noticing this now, so it's not as if I've yet had a need to getWorldTransform from script; Maybe no one else has either. Still, it seems to me that it should be included. I think I may add a console function to my Torque (in sceneObject.cc, near the top) like so:

ConsoleMethod( SceneObject, get[b]World[/b]Transform, const char*, 2, 2, "Get [b]world[/b] transform of object.")
{
   char *returnBuffer = Con::getReturnBuffer(256);
   const MatrixF& mat = object->get[b]World[/b]Transform();
   Point3F pos;
   mat.getColumn(3,&pos);
   AngAxisF aa(mat);
   dSprintf(returnBuffer,256,"%g %g %g %g %g %g %g",
            pos.x,pos.y,pos.z,aa.axis.x,aa.axis.y,aa.axis.z,aa.angle);
   return returnBuffer;
}

I copied the ConsoleMethod for getTransform and added a few "world"s. That should work, though I've not tested it.

Furthermore, while on the subject of script: The script equivalents of mulV and mulP are MatrixMulVector and MatrixMulPoint, respectively. They each take two arguments, a matrix (in script form) and a vector (or point, as the case may be).

So, to recap: If you have a point in local object coordinates that you wish to translate to world coordinates, you can do so like this: (from C++)
mObjToWorld.mulP(pointToTranslate);
or this: (from script)
%mat = object.getTransform();
%translatedPoint = MatrixMulPoint(%mat, %pointToTranslate);

---

Yikes. I didn't intend to type that much. Sometimes I get carried away. :-o
Ok, I'm going back to work now...
#3
06/10/2006 (8:05 am)
@ Scott:

Quote:
Yes, but note that mulV is for vectors, while mulP is for points. Also, mulP and mulV take their arguments as references, so nix the &.

Whoops. You're most definately right - I was kinda sleepy when I read the function declarations.

Good post!

- Eric