Skip to main content
gamedev.net
🔒 Locked

Vector3 * Matrix4 multiplication?

Started by xynapse Dec 11, 2011 at 9:36 PM 4 replies 9.2k views
Original Post
xynapse
xynapse
Hey guys,

i am now in the middle of animation implementation and have a small query regarding vertexes updating with matrices.




Here is what i have :

- actual vertex position (Vector3) ( object space )

- bone transformation matrix ( consists of a Matrix4 translation/rotation ) - for actual frame ( object space )




Matrix4 Class ( multiplication definition )

friend CVector3 operator*(const CVector3 &lhs, const CMatrix4x4 &rhs);

inline CVector3 operator*(const CVector3 &lhs, const CMatrix4x4 &rhs)
{
return CVector3(
(lhs.x * rhs.mtx[0][0]) + (lhs.y * rhs.mtx[1][0]) + (lhs.z * rhs.mtx[2][0]),
(lhs.x * rhs.mtx[0][1]) + (lhs.y * rhs.mtx[1][1]) + (lhs.z * rhs.mtx[2][1]),
(lhs.x * rhs.mtx[0][2]) + (lhs.y * rhs.mtx[1][2]) + (lhs.z * rhs.mtx[2][2]));
}














What i need to do

is to translate/rotate the vertex that is linked with bone - that has own transformation matrix ( also in object space ).

new vertex position should return Vector3.




Is this what i am supposed to do or it is to late for me and i have lost my mind...







iCurrentFrame = actual frame

CVector3 vNewVertexPosition = vVertexOrigin * mat4BoneTransformationMatrix[ iCurrentFrame ];






And after that multiply that by MVP of course.







What i have found is that when i multiply vertex position by bone transformation matrix there is no effect at all.



// No effect at all ( see my operator* definition, is this ok ? )

CVector3 vNewVertexPosition = vVertexOrigin * mat4BoneTransformationMatrix[ iCurrentFrame ];






When i do



// I get translation only but no rotation ( which is obvious as i pick [3][0] to [3][1] from my matrix4 )

CVector3 vNewVertexPosition = vVertexOrigin + CVector3(mat4BoneTransformationMatrix[ iCurrentFrame ][3][0], mat4BoneTransformationMatrix[ iCurrentFrame ][3][1], mat4BoneTransformationMatrix[ iCurrentFrame ][3][2]) ;






Is there something i forgot here?

Many many many thanks!
perfection.is.the.key
RulerOfNothing
RulerOfNothing
When you say that there is no effect at all, what exactly do you mean by that? Do you mean that the output vector is the same as the input vector? Also, what happens if you replace the overloaded operator with a normal function? (I am curious, because while I can't see anything wrong with your operator, I am not very familiar with operator overloading)
Hodgman
Hodgman
You only seem to be taking the 3x3 part of your 4x4 matrix into account when doing the multiplication. If you want the translation part to be applied, you've got to do a full Vec4 * Mat4 multiplication (use your Vec3 as input, but pretend it has a [font="'Lucida Console"]w[/font] value of 1.0)
xynapse
xynapse

You only seem to be taking the 3x3 part of your 4x4 matrix into account when doing the multiplication. If you want the translation part to be applied, you've got to do a full Vec4 * Mat4 multiplication (use your Vec3 as input, but pretend it has a [font="Lucida Console"]w[/font] value of 1.0)

You're absolutely right, i missed that one -


the effect of Vec3 * matrix4 multiplication should result in a vector3 that is translated and rotated by that matrix - correct?



So it is the same as doing Vec4 * Matrix4 - where Vec4 .w component = 1 as follows:




inline CVector4 operator*(const CVector4 &lhs, const CMatrix4x4 &rhs)
{
return CVector4(
(lhs.x * rhs.mtx[0][0]) + (lhs.y * rhs.mtx[1][0]) + (lhs.z * rhs.mtx[2][0]) + (lhs.w * rhs.mtx[3][0]),
(lhs.x * rhs.mtx[0][1]) + (lhs.y * rhs.mtx[1][1]) + (lhs.z * rhs.mtx[2][1]) + (lhs.w * rhs.mtx[3][1]),
(lhs.x * rhs.mtx[0][2]) + (lhs.y * rhs.mtx[1][2]) + (lhs.z * rhs.mtx[2][2]) + (lhs.w * rhs.mtx[3][2]),
(lhs.x * rhs.mtx[0][3]) + (lhs.y * rhs.mtx[1][3]) + (lhs.z * rhs.mtx[2][3]) + (lhs.w * rhs.mtx[3][3]));
}








// Create a vector4 to multiply by bone transformation matrix

CVector4 vNewVertexPosition = CVector4(vVertexPosition.x,vVertexPosition.y,vVertexPosition.z,1) ;




// Vector4 * Matrix4 multiplication

vNewVertex4Position = vNewVertex4Position * mat4BoneTransformationMatrix[ iCurrentFrame ];




// And here is the translated/rotated vector3 from Vector4

CVector3 vVector3Position = CVector3(vNewVertex4Position.x, vNewVertex4Position.y, vNewVertex4Position.z);



perfection.is.the.key
Ripiz
Ripiz

the effect of Vec3 * matrix4 multiplication should result in a vector3 that is translated and rotated by that matrix - correct?


Vec3 * Mat4 is undefined, therefore you have to decide yourself if you want to use 0 or 1 or any other number in 4th component. If you transform Point then it should be 1, but if it's direction (ex. Normal vector) then only rotation is supposed to affect it, therefore you have to use 0.
xynapse
xynapse
All clear,




thanks for your replies.

perfection.is.the.key

Topic Locked

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

Sign in to reply to this topic.