What's the math behind CMatrix4::transformVect(vector3df)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
fabietto
Posts: 93
Joined: Wed Sep 24, 2008 4:38 pm
Location: Plymouth, UK
Contact:

What's the math behind CMatrix4::transformVect(vector3df)

Post by fabietto »

Dear all,

first of all let me apologise if I'm not posting on the proper forum section (obviously, in case feel free to move my post).

As a poor computer scientist lacking a serious background in math/geometry, I'm wondering what's the math behind the transformVect function. Whenever I've experienced the need of moving an object across a 3D space I've always relied on code similar to the one posted below:

Code: Select all

vel = vector3df(0.0f, 0.0f, distance);
m.setRotationDegrees(currentRotation);	
m.transformVect(vel);
coordinates += vel;
But what sort of operations the m.transformVect(vel) instruction applies on the vel matrix? I looked across the Irrlicht sources, but couldn't find where that function is implemented. Furthermore, as far as you know there is a simple way to express in a mathematical way these operations (I'm writing a paper right now and I'd like to express in mathematically correct terms what happen when I use a function such the one posted above)?

Thanks all for your attention! :)
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

The implementation is in matrix4.h
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
fabietto
Posts: 93
Joined: Wed Sep 24, 2008 4:38 pm
Location: Plymouth, UK
Contact:

Post by fabietto »

CuteAlien wrote:The implementation is in matrix4.h
Me idiot. Looking at the documentation I obviously noticed that the function was defined in matrix4.h, but I thought only the prototype was there. So I spent minutes looking for a matrix4.cpp or similar (without any luck as you can guess... :D).

Code: Select all

template <class T>
	inline void CMatrix4<T>::transformVect( vector3df& vect) const
	{
		f32 vector[3];

		vector[0] = vect.X*M[0] + vect.Y*M[4] + vect.Z*M[8] + M[12];
		vector[1] = vect.X*M[1] + vect.Y*M[5] + vect.Z*M[9] + M[13];
		vector[2] = vect.X*M[2] + vect.Y*M[6] + vect.Z*M[10] + M[14];

		vect.X = vector[0];
		vect.Y = vector[1];
		vect.Z = vector[2];
	}
I guess M is the current matrix (which in my case is the result of m.setRotationDegrees(currentRotation)), right?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

fabietto wrote: I guess M is the current matrix (which in my case is the result of m.setRotationDegrees(currentRotation)), right?
Hehe - check that matrix4.h file some more, M is even explained in there ;-) ("//! Matrix data, stored in row-major order")
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
fabietto
Posts: 93
Joined: Wed Sep 24, 2008 4:38 pm
Location: Plymouth, UK
Contact:

Post by fabietto »

CuteAlien wrote:Hehe - check that matrix4.h file some more, M is even explained in there ;-) ("//! Matrix data, stored in row-major order")
Thanks, dude, I think I finally obtained what I was looking for... :)

Just to summarise my thoughts, let consider again the code I posted above (assuming currentRotation and coordinates two vector3df objects respectively containing the rotation of a 3D body around the three axis and its coordinates; m a CMatrix4; distance a float variable):

Code: Select all

vel = vector3df(0.0f, 0.0f, distance); 
m.setRotationDegrees(currentRotation);    
m.transformVect(vel); 
coordinates += vel;
m.setRotationDegrees(currentRotation) calls in turn setRotationRadians and assigns values to m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], m[10] (I believe m[3], m[7], m[11+] are = 0, am I right?).

m.transformVect(vel) transforms vel in the following way (in my scenario m[12], m[13] and m[14] are equal to 0, as well as vel.X and vel.Y, thus I've removed all of these factors from the formulas below):

vel.X = vel.Z * m[8]
vel.Y = vel.Z * m[9]
vel.Z = vel.Z * m[10]

The new coordinates are then calculated simply as coordinates + vel.

In other terms (if my above considerations are correct) I might conclude that:

coordinates(t+1) = coordinates(t) + distance * (vel.Z * m[8], vel.Z * m[9], vel.Z * m[10]).

Hope I haven't written too much bullshit... :)
fabietto
Posts: 93
Joined: Wed Sep 24, 2008 4:38 pm
Location: Plymouth, UK
Contact:

Post by fabietto »

And, in more technical terms, the result should be something like the following (couldn't copy/paste from LaTeX, sorry - I also just noticed there is a "both" too much within the text, while a "a" is missing):

Image

I can't pretend anything, of course, but if anyone might want to give a look to it that would be ENORMOUSLY appreciated! :)
Post Reply