[SOLVED]How to make an object look towards another object with Python ? (not TrackTo)

Hello,

I want to make a tank patrol between two empties (E1 and E2)
When it is close to E1, it should turn towards E2 and vice versa.

It can be done with TrackTo and States I guess, but I prefer to
keep the AI within Python, for various reasons (mainly because if
I want to patrol between 3, 4, 5 points, logic states will become really messy)

I suppose I need to subtract the two positions, then convert the resulting
vector to an orientation ? Something like that, but Im not sure.
Could somebody help ?

Thanks in advance

TrackTo should be much faster than Python script and you can still keep 99% of the logic in the scripts.

Thanks for your reply,

yes but with Python you can create one AI and then give the empties as arguments.
Plus you can change the patrol points at runtime.

With logic bricks, for each patrol combination I need to write the whole logic from scratch.
For a single level, I would end up with tens of different scripts.

I assume you can not change the TrackTo target from Python, right ?

Target = own.scene.objects['TargetName']
v2 = own.getVectTo(Target)

own.alignAxisToVect(v2[1],0,.1)
own.alignAxisToVecT([0,0,1],2,1)

setting the track to target=


Track = cont.actuator['TrackTo']
Track.object = Target

You guys are good! Now I can even do multiple waypoints, not only two. This will make my game much better.

Thanks!

PS: Are all actuators accessible from Python ?

Yeah checkout the python api
There is soooooo much you can do using bge, and way more once you learn mathutils

i personally would use alignAxisToVect(), or some mathutils function.

See my post upstairs my bro_chacho

Be careful with alignAxisToVect () there’s a bug which causes flipping of the z axis sometimes when tracking on the y axis. Your tank can end up upside down! :slight_smile:

that is why I align the Z axis each time :smiley:

also, I have notices using Local is more effective :smiley:

local = target.worldPosition-own.worldPosition
local = target.wordOrientation.inverted()*local

if local.x>0:
   #object is in front of you
   if local.y>.1: 
       #(object is on left)
   elif local.y<-.1:
       #(object is on right)
   else:
       #object is in front