Saturday, August 30

SWARM_01

A first study into particle swarm behavior, with the help of RealFlow and Python scripting.
This code adjusts each particles's velocity and direction to the average value among its neighbors (within a specified distance).
However, in this script, the movement of the particles becomes far too uniform after some frames
already. More to come...




#version: august 29th, 2008
#description: EVENTS SRIPT / basic swarm behaviour test:
#get speed and direction of neighboring particles,
#calculate average and adjust own

def onSimulationBegin():
emitter = scene.addEmitter("Circle")
emitter.setName("Emitter1")
emitter.setParameter("Type","Dumb")
emitter.setParameter("Point size",1.0)
emitter.setParameter("Max particles",0)
import random
for i in range(0,50):
emitter.addParticle(Vector.new(random.uniform(-1,1)*3,random.uniform(-1,1)*3,
random.uniform(-1,1)*3),Vector.new(random.uniform(-1,1)*5,
random.uniform(-1,1)*5,random.uniform(-1,1)*5))


def onSimulationStep():
scene.message("|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||")
emitter = scene.getEmitter("Emitter1")
particle = emitter.getFirstParticle()
while particle:
#scene.message("INITIAL velocity = " + str(particle.getVelocity().x) + " | " + str(particle.getVelocity().y) + " | " +
str(particle.getVelocity().z))
neighlist = particle.getNeighbors(2)
scene.message("neighbors: " + str(len(neighlist)))
velsum = Vector.new(0,0,0)
for i in range (0,len(neighlist)):
neighbor = neighlist[i]
velneigh = neighbor.getVelocity()
#scene.message("velneigh = " + str(velneigh.x) + " | " + str(velneigh.y) + " | " + str(velneigh.z))
velsum = velsum + velneigh
#scene.message("velsum = " + str(velsum.x) + " | " + str(velsum.y) + " | " + str(velsum.z))
velaverage = velsum / Vector.new(len(neighlist),len(neighlist),len(neighlist))
particle.setVelocity(velaverage)
#scene.message("UPDATED velocity = " + str(particle.getVelocity().x) + " | " + str(particle.getVelocity().y) + " | " +
str(particle.getVelocity().z))
scene.message("-----------------------------------------------------------------------------")
particle = particle.getNextParticle()