Python Messing with my head

I’ve come unstuck doing a little test. I decided to brush up my Python skills by emulating the track to Actuator. How hard can it be methinks, especially as I’m only doing the X, Y tracking. How wrong could I be. Been messing with this for ages, note it works as i wish in the positive X direction but behaviours messed going the other way.


Pathfinding.blend (129 KB)

Here’s how I do it:

oblist = GameLogic.getCurrentScene().getObjectList()
bot = oblist['OBenemy']
player = oblist['OBplayer']

axis = '-y'
if axis[-1:]=='x': dir = 0
elif axis[-1:]=='y': dir = 1
elif axis[-1:]=='z': dir = 2

#find difference in location
dloc = bot.getVectTo(player.getPosition())[1] #not 0, and not 2. I don't know what 0 and 2 are, but [1] is the location difference.

#convert all of the numbers in dloc to - numbers
if axis[:1] == '-':
     temp = []
     for data in dloc:
    #end for
    dloc = temp
#end if

#0 = no turning, 1 = instant turn
rotspeed = 0.1

#now apply the variables to the magic command

#to keep the z axis straight up, align it to world z axis

That can be changed and simplified in most situations, but you can probably adapt that to what you want.

Thanks for the help I managed to do it with my script though, just took a break and then I could think clearly again. Heres the finished script.

import math
cont = g.getCurrentController()
own = cont.getOwner()

Collision = cont.getSensor(“Collision”)
Motion = cont.getActuator(“Motion”)

Goal = g.getCurrentScene().getObjectList()[“OBObjective”]
Objective_loc = Goal.getPosition()
Self_loc = own.getPosition()
X_distance = (Objective_loc[0]-Self_loc[0])
Y_distance = (Objective_loc[1]-Self_loc[1])
Distance = math.sqrt((Y_distance2)+(X_distance2))
Bearing = math.acos(Y_distance/Distance)

if X_distance < 0:
Y_force = 15math.cos(Bearing)
X_force = -15
Y_force = 15math.cos(Bearing)
X_force = 15

Motion.setForce(X_force, Y_force, 0, 1)
g.addActiveActuator(Motion, 1)

Hi Wookie1,

if you want to make a runtime out of your game, and the user shouldn’t have to install Python, than I woudn’t use “import math”.
Currently I’m freeing all my code from “math” including an a* pathfinding.
“Almosts” solution looks better, because there is no usage of “math”.