Point / Matrix multiplication
BlitzMax Forums/BlitzMax Programming/Point / Matrix multiplication
| ||
I have a 3-dimensional point and a 4x4 matrix. How do I multiply the point by the matrix to push it "into" the matrix (transform from world to this matrix)? How do I transform the point from the matrice's space to the world? |
| ||
Why have you got a 4x4 matrix? |
| ||
It's one better. |
| ||
are you asking how to do vector+matrix multiplication or how to be able to construct inverse matrices to transform a point from global to local coordinates and vice versa? |
| ||
For starters, matrix multiplication is not defined for two matrices of 3-by-1 and 4-by-4 sizes. |
| ||
This is a technical trick so that matrix multiplication can move a vector to a new location. The idea is to encode a 3-d vector (x,y,z) into 4-d as (x,y,z,1). If you worked only in 3-d then matrix*vector would always leave the zero vector (0,0,0) unchanged. The exact details depend on what convention is used. The vector may be considered as a row and the multiplication is vector*matrix Or the vector may be a column and you do matrix*vector. I think Direct3D uses the first approach and OpenGL uses the second. |
| ||
I found that if I created a matrix from the position and multiplied it by the other, I could get the transformed position. To get "out" of a matrix, I used the inverse matrix. |
| ||
Here's how I did TFormPoint():Function TFormPoint(x#,y#,z#,src_ent:TEntity,dest_ent:TEntity) If dest_ent If Not dest_ent.mat dest_ent=Null EndIf Local mat:TMatrix=New TMatrix Local mat2:TMatrix If src_ent mat2=src_ent.mat.copy() mat=TMatrix.FromPoint(vec3(x,y,z)) mat2.multiply(mat) x=mat2.grid[3,0] y=mat2.grid[3,1] z=mat2.grid[3,2] EndIf If dest_ent mat2=dest_ent.mat.inverse() mat=TMatrix.FromPoint(vec3(x,y,z)) mat2.multiply(mat) x=mat2.grid[3,0] y=mat2.grid[3,1] z=mat2.grid[3,2] EndIf tformed_x#=x# tformed_y#=y# tformed_z#=z# EndFunction To do a vector, you just set the entity matrix position to 0. |
| ||
Looks like you got it sorted, looks almost exactly like what I had (except, I'm using Ogre's methods.) I recently got into this matrix stuff myself. It was a pain for a starter because looking around it was rather difficult to find what to multiply with what. Thanks for sharing your solution- two months ago this would have saved me quite a long time googling and I'm sure others will find it useful. :-) |