lundi 13 août 2018

Design patterns. For a particle system

Problem:

I am trying to simulate a boids particle system with python for an addon I am creating for Blender 3D, something quite complex and will be done from scratch.

Its the third time I try to design a maintainable code structure for it.

I tried to come up with pythonic design that allow for fast prototyping of different classes of particles interacting with each other, such that I can just create a new particle implementation and have it to interact with the other particles in the system.

see the example:

class ParticleSystem:
    def __init__(self):
        self.particles = []

    def add_particle(self, particle):
        self.particles.append(particle)

    def step_frame(self, speed):
        for p in self.particles:
            p.step(speed)


 class Particle:
    def __init__(self, system)
        self.location = Vector(0,0,0)
        self.system = system

    def step(speed):
        for particle in self.system.particles:
            #random interatcion formula
            self.location = particle.location + self.location


system = ParticleSystem()

p = Particle(system)
system.add_particle(p)

As you can see this my best aproach but it gets quite messy when I have multiple classes of particles and I have to pass the system to the particle and then the particle to the system.

question

Is there a way so I can detect when the creation of a class implementation like: Class Baseball(Particle): and have the system class to know all the existing types so I can just call system.create_baseball(location)?

Aucun commentaire:

Enregistrer un commentaire