# simple logic bricks to python

(sara) #1

is there any example code that replaces simple logic bricks like:
trackto
near x do this
etc

because if you have many logic bricks blender doesnt manage them too fast.

(Doc Holiday) #2

if i understand You right, You want to simulate Logic-Bricks via a Python Script? I think that not possible, yet. But You can control them by Scripts.

(Yamyam) #3

This is from my WIP, This is free to use,but if it is donâ€™t work in yourfile, you need to fix this yourself.

``````from math import sqrt

def dot(a,b):
return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]

def cross(a,b):
return [a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]]

def norm(a):
len = sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2])
return [a[0]/len,a[1]/len,a[2]/len]

def lookat2d(y):
y[0]=-y[0]
y = norm(y)
x = norm(cross(y,[0,0,1]))
z = cross(x,y)
return [[x[0],x[1],x[2]],[y[0],y[1],y[2]],[z[0],z[1],z[2]]]

# cont = GameLogic.getCurrentController()
# obj = cont.getOwner()
# loc = obj.getPosition()

def trackto2d(obj,dstloc):
loc = obj.getPosition()
vec = [dstloc[0]-loc[0],dstloc[1]-loc[1],dstloc[2]-loc[2]]
mat = lookat2d(vec)
obj.setOrientation(mat)

def near(srcloc,dstloc,len):
dx = dstloc[0]-srcloc[0]
dy = dstloc[1]-srcloc[1]
dz = dstloc[2]-srcloc[2]
return (dx*dx + dy*dy + dz*dz &lt; len*len)

``````

(wiseman303) #4

You can also (obviously) totaly replace property sensors and actuators.