Hi,
I’m desperately trying to connect growing cells with springs using the toxiclibs library. More specifically, I’d like to to put a spring between a “mother” cell and its “child” after division.
PROBLEMS:
-
All cells are appended to an array list named
collection
and I don’t know how to identify which cell is a “mother” cell and which other cell is its “child”. I need to know that information in order to connect them -
I have a
Cell
class that inherits fromVerletParticle2D
but for some reason I’m unable to create springs from these verlet particles.
The final output should look like this:
add_library('verletphysics')
add_library('toxiclibscore')
from toxi.physics2d import VerletPhysics2D, VerletParticle2D
connections = []
def setup():
global collection, physics
size(480, 360, P2D)
smooth(8)
physics = VerletPhysics2D()
collection = [Cell(width>>1, height>>1 , 20)]
def draw():
global physics, collection
background(255)
physics.update()
for i, c in enumerate(collection):
if c.connected == False:
physics.addParticle(c)
c.connected = True
c.growth()
c.interact()
c.display()
for i2, c2 in enumerate(collection):
if i != i2:
s = VerletSpring2D(collection[i], collection[i2], 200, 1)
physics.addSpring(s)
class Cell(VerletParticle2D):
global physics
growthInterval = 5
divisionInterval = 50
def __init__(self, x, y, rad):
super(Cell, self).__init__(x, y)
self.position = Vec2D(x, y)
self.radius = rad
self.time = 0
self.connected = False
def growth(self):
self.time += 1
if len(collection) < 20:
if self.time > 0 and self.time%Cell.divisionInterval == 0:
if random(1) > .5:
dir = PVector.random2D()
self.radius *= .5
child = Cell(self.position.x() + dir.x, self.position.y() + dir.y, self.radius)
collection.append(child)
connections.append([self, child, False])
if self.time%Cell.growthInterval == 0:
self.radius += .5
def interact(self):
for a in collection:
if a is not self:
d = self.position.distanceTo(a.position)
if d < (a.radius*.5 + self.radius*.5 + 1):
gap = (a.radius*.5 + self.radius*.5 + 1) - d
diff = Vec2D.sub(self.position, a.position).normalizeTo(gap)
a.position = a.position.sub(diff)
def display(self):
stroke(0)
strokeWeight(3)
point(self.position.x(), self.position.y())
noFill()
strokeWeight(1)
ellipse(self.position.x(), self.position.y(), self.radius, self.radius)