<disclaimer> I haven’t done any courses involving vectors properly, but here is my understanding. Anyone who see’s something that I’ve done wrong, then correct me.</disclaimer>
Do you understand what a vector is?
A vector is an angle, but represented as a 3D co-ordinate. The vector is visualised by drawing a line between this point and the origin.
By using vectors we can add, subtract and multiply them more easily.
So to align an axis to a vector, you first have to have a vector. So let’s make one up:
import mathutils
vec1 = mathutils.Vector((0,0,1))
vec2 = mathutils.Vector((0,1,0))
Vec1 is a vector pointing straight up, and vec2 is a vector pointing across on the positive Y axis.
Vectors don’t have to have a length of 1, and when representing things like velocity or forces, they don’t. If we have the vectors [0,0,1] and [0,0,2] they have the same angle, and thus the same result in the alignAxisToVect function, but they are different vectors.
A vector with a length of 1 is known as a normalized vector.
Vectors can be split into their X, Y and Z components. This is simple in blender:
x = vec1[0]
y = vec1[1]
z = vec1[2]
#Or we can use:
x = vec1.x
y = vec1.y
z = vec1.z
So, creating vectors and splitting them up is all very good, but sometimes we want to get vectors from things. BGE provides us with some good functions such as:
import bge
cont = bge.logic.getCurrentController()
obj = cont.owner
other = [o for o in bge.logic.getCurrentScene().objects if o.name == 'Cube'][0]
distance, globalVector, localVector = obj.getVectTo(other) #Get's the vectors between two objects
ZAxisVect = obj.getAxisVect([0,0,1])
We can manipulate them. We can multiply them with a number to make them bigger (makes no difference to the angle), we can add two vectors etc.
Now, what can we do with them?
Well, BGE also has a number of good functions for this:
- applyForce
- applyTorque
- alignAxisToVect
- apply impulse
import bge
cont = bge.logic.getCurrentController()
obj = cont.owner
vect = mathutils.Vector((0,0,1))
obj.applyForce(vect, 1) #Applies a local force on the Z axis
obj.alignAxisToVect(vect, [0,1,0], 0.5) #Aligns the objects Y axis to the global Z axis with some smoothing
In many cases we don’t need to convert them to mathutils vectors. The only reason you may want to do this is if you want to use a mathitils function, such as normalizing, projecting, addition. Otherwise, just keep it as a simple list.
I hope that gave you somewhere to start.