Vectors - global, local

Hi all

I am having a look at making my turret aim better and stfgeoff kindly gave this snippet of code (or similar):

Base["current_Target"] = locked_target
                target_position = Base["current_Target"].worldPosition
                target_velocity = Base["current_Target"].linearVelocity
                bullet_velocity = 1000                
                distance_to_target = Base.getDistanceTo(Base["current_Target"])                  
                Base["TargetYPos"] = target_position + target_velocity * distance_to_target / bullet_velocity  

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?

Cheers

Paul

Check this out :smiley:

Attachments

TorqueTrackToBot (3).blend (539 KB)

You could subtract the targets position from your turrets position, this will give you the targets position relative to the turret.

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;


vector_to = target.worldPosition - source.worldPosition
inverse_orientation = source.worldOrientation.inverted()
local_vector = inverse_orientation * vector_to

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.

I’ve not tested this statement myself, so I’d be interested to see the difference between calling the two functions.

They are both very fast:

This is the code I used:

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()

Thanks for the info guys, very informative!