Mathutils : What do you use Vector and Matrix for?

Hi,

I’ve seen Vector and Matrix from Mathutils used in some scripts.
The documentation is here : link

I couldn’t get what this Mathutils Module (Euler, Matrix, Quaternion, Vector) is useful for.

Could someone explain what those modules are made for, using ingame examples,
and show what it’s meant to replace in Python?
I’ve tried to understand it by myself, but I sometimes need working examples to get the thing.

I have the feeling that it would help to improve my scripting abilities,
and it might help some other members.

Thank you.

You may want to check the “Matrix Transformation Tutorial

I use vectors and matrices for almost everything. If you have to objects for example, and want to know the direction between the two: you can substract all position components seperatly for posX, posY and posZ. So the direction along the X axis would be posX(object1) - posX(object2). Having to repeat this for every component is annnoying.
If you make a vector from the position, like vec1 = Vector( object1.position ) you can do this for all 3 components at once; vec1 - vec2. So basically the math becomes simpler.
A matrix can be used to rotate a vector, first create a rotation matrix, M = RotationMatrix(90, 3, “x”). Then the vector rotated 90 degrees around the x-axis is vec_rot = M * vec.
You can do a lot more with vectors and matrices but that requires a bit better understanding, there are probably some beginner tutorials around the web, like the one ypoissant gave.

Latest documentation for GE is here btw, some functions now in different places:

http://www.blender.org/documentation/249PythonDoc/GE/Mathutils-module.html

Some recent examples of where I used the Vector object:
Planetary Gravity.
Explosion.
Bullet Hole.
Local to Global.

This is the best example (in my opinion):

Tornado.

And there are others too, but I can’t remember them all ( I don’t exactly keep track of all my examples…I should probably start, though ).

In either case, as Wraaah already mentioned, it’s all about making the math easier. There is really nothing special about the Vector, or the Matrix objects as supplied in the Mathutils module; I mean, I could just use lists, and then write functions that would allow me to treat them like vectors, but having an object with all the methods already there is just more convenient.

So, don’t think about the Mathutils module, and the objects it provides, as “magic” -> the math behind the constructs is the important thing, and that’s what you should learn.

The Mathutils module is just a very convenient tool.

As for the orientation matrix, I actually wrote a tutorial some time ago (step by step, with .blend examples) that tries to explain some basic concepts about it:

http://socialstorage.googlepages.com/orientationmatrix-basics

Although, the tutorial posted by ypoissant is probably more detailed in the mathematical sense, so you should definitely look over that too.

Math is really the key: You can know all of python and the BGE api by heart, but if you don’t know the math, you’re just tapping into 1% of what’s possible.

Thank you all for your fast answers, and for taking the time to write explanations.
I appreciate it.