Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Matrix to vector?

Started by bradhe Jun 23, 2012 at 2:24 AM 1 replies 1.5k views
Original Post
bradhe
bradhe
Heya! I've got a tree of objects, each with their own transformation matrices relative to their parent object. Give an object anywhere in the tree, I'd like to find it's absolute position from the origin. This is simple enough: I walk down the tree, applying each transform along the way, 'till I hit the object in question. The resultant matrix gives the absolute transformation matrix.


What's the best way to turn this in to a vector that is the position relative to the origin? I have bounding boxes around each element, and I'd like to use this to find collisions.

I may be making this more complicated than I need to...
bradhe
bradhe
Wow, yeah that was crazy easy. I cross-referenced Wikipedia on Perspective projection matrices, your simple suggestion + Wikipedia made it obvious. Here's what I ended up with:

[source lang="java"] public static Matrix4f fromVector(Vector3f vector) {
Matrix4f matrix = new Matrix4f();
matrix.m30 = vector.x;
matrix.m31 = vector.y;
matrix.m32 = vector.z;
matrix.m33 = 1;
return matrix;
}

public static Vector3f toVector(Matrix4f matrix, Vector3f vector) {
vector.x = matrix.m30;
vector.y = matrix.m31;
vector.z = matrix.m32;
return vector;
}

public static void mul(Matrix4f left, Vector3f right, Vector3f result) {
Matrix4f matrix = fromVector(right);
Matrix4f.mul(left, matrix, matrix);
toVector(matrix, result);
}[/source]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.