as i’m very new at this (python and all) the following code is probably a disaster, but it works… It’s based on the pseudocode for boids at http://www.vergenet.net/~conrad/boids/pseudocode.html
1.create empty (name = initGame)
in realtime tab add always sensor with fire pulse switched off
link always sensor to Python script (initGame):
import GameLogic as g
#creates the objectlist
g.ObjectList = []
2.create empty (name = Add) with property int ‘N’ = 0 and int ‘Add’ = 1
in realtime tab add property sensor with fire pulse switched on
Prop: Add - Equal - 1
Link this to Pythonscript (newPos)
This script randomly places an amount of boids
import GameLogic as g
c = g.getCurrentController()
o = c.getOwner()
AddBoid = c.getActuator('AddBoid')
print 'newstart'
N = o.N
#the maximum amount of boids is set to 10
if N<11:
#set new random position
x = g.getRandomFloat() * 50
y = g.getRandomFloat() * 50
z = g.getRandomFloat() * 50
newPos = [x,y,z]
#move empty to new position
o.setPosition(newPos)
#add new boid
g.addActiveActuator(AddBoid,1)
#add boid to objectlist
lastBoid = AddBoid.getLastCreatedObject()
if lastBoid != None:
g.ObjectList.append(lastBoid)
o.N = o.N +1
else:
o.Add = 0
print g.ObjectList
connect the script to an Edit Object actuator named AddBoid
Add Object - Boid
3.In a new layer add a boid named ‘Boid’ with property int ‘dx’ = 0,
int ‘dy’ = 0 and int ‘dz’ = 0.
in realtime tab add always sensor with fire pulse switched on
link always sensor to Python script (Rules):
import GameLogic as g
c = g.getCurrentController()
o = c.getOwner()
x = o.getPosition()[0]
y = o.getPosition()[1]
z = o.getPosition()[2]
dx = o.dx
dy = o.dy
dz = o.dz
print 'vel =',dx,dy,dz
#xxxxxxxxx RULE 1 xxxxxxxx
#count of objects in list (just to know)
max = len(g.ObjectList)
print 'max =',max
sum = [0,0,0]
for boid in g.ObjectList:
#if boid is not equal to this boid
if boid.getPosition() != o.getPosition():
sum[0] = sum[0] + boid.getPosition()[0]
sum[1] = sum[1] + boid.getPosition()[1]
sum[2] = sum[2] + boid.getPosition()[2]
else:
print 'boid is thisboid'
# pc = percieved center of mass
pc = [sum[0]/(max-1),sum[1]/(max-1),sum[2]/(max-1)]
print pc
v1 = [(pc[0]-x)/100,(pc[1]-y)/100,(pc[2]-z)/100]
print v1
#xxxxxxxxx RULE 2 xxxxxxxx
dist = 10
v2 = [0,0,0]
for boid in g.ObjectList:
#if boid is not equal to this boid
if boid.getPosition() != o.getPosition():
if abs(x - boid.getPosition()[0]) < dist:
if abs(y - boid.getPosition()[1]) <dist:
if abs(z - boid.getPosition()[2]) <dist:
v2[0] = (x - boid.getPosition()[0])/50
v2[1] = (y - boid.getPosition()[1])/50
v2[2] = (z - boid.getPosition()[2])/50
#xxxxxxxxx RULE 3 xxxxxxxx
#sum of all velocities
sumvel = [0,0,0]
#factor of influence
f = 1.5
for boid in g.ObjectList:
#if boid is not equal to this boid
if boid.getPosition() != o.getPosition():
sumvel[0] = sumvel[0] + boid.dx
sumvel[1] = sumvel[1] + boid.dy
sumvel[2] = sumvel[2] + boid.dz
else:
print 'boid is thisboid'
# vc = percieved velocity
vc = [sumvel[0]/(max-1),sumvel[1]/(max-1),sumvel[2]/(max-1)]
print vc
v3 = [(vc[0]-dx)/f,(vc[1]-dy)/f,(vc[2]-dz)/f]
print v3
#xxxxxxxxx POSITION xxxxxxxx
print 'initial velocity = ',dx,dy,dz
vel = [dx+v1[0]+v2[0]+v3[0],dy+v1[1]+v2[1]+v3[1],dz+v1[2]+v2[2]+v3[2]]
newPos = [x+vel[0],y+vel[1],z+vel[2]]
o.setPosition(newPos)
in realtime tab add always sensor with fire pulse switched off
link always sensor to Python script (Velocity):
import GameLogic as g
c = g.getCurrentController()
o = c.getOwner()
vel = [-1,-1,-1,0,0,0,1,1,1,0]
i = int(g.getRandomFloat()*10)
o.dx = vel[i]
j = int(g.getRandomFloat()*10)
o.dy = vel[j]
k = int(g.getRandomFloat()*10)
o.dz = vel[k]
AND THAT’s it… It’s probably a nightmare for those of you who know how to program Python but as i don’t (not really
) i very much welcome any changes etc…