This is great for now, and works well with alignAxisToVect() but my current turret uses the local component of getVectTo by using something like:
(dist,glob,loc) = getVectTo
It works by taking the normalized vector and if it is between -1,0 or 1 etc I can work out where the target is, and the turret will rotate until this value is 0 (i.e. straight ahead). It allows me to have a traversable turret with an independent gun barrel.
My problem is trying to get the local vector of the target (so I can use my turret script) from the targeting script snippet that uses the world axis to define its vector.
Can anyone give me some pointers on how I can solve this?
A quick lesson on Matrices (if you’re not aware already, and this applies to all rotations)
Local space of an object is simply treating the object as being axis aligned. A local vector to another object is the same as rotating the object to the axis and in effect rotating the other object about the first by the same rotation. In order to do this, one can apply the inverted orientation matrix of the first object.
Your case involves a local vector between two objects. To find the “local vector”, take the global vector between the two, and then rotate it by the inverse of the object’s orientation. This would look something as follows;
Just a note: I’m not sure if it’s efficient to use the inverted() method on rotation matrices. Matrix inversion is a pretty complicated process. For orthogonal matrices (which includes rotation matrices), it should be faster to take the transpose instead. This is assuming that the Matrix class doesn’t optimize for special cases.
from time import clock
numIt = 10000
startTranspose = clock()
for i in range(numIt):
matTranspose = obj.worldOrientation.transposed()
endTranspose= clock()
print(numIt, "transp'ns take ", 1e3*(endTranspose - startTranspose), "ms")
startInverse = clock()
for i in range(numIt):
matInverse = obj.worldOrientation.inverted()
endInverse = clock()
print(numIt, "inversions take ", 1e3*(endInverse- startInverse), "ms")
print()