Beispiel #1
0
    def __init__(self, world, showcase, cam, pos):
        self.mouseJoint = None
        self.mouseWorld = Box2D.b2Vec2(0.0, 0.0)
        self.mouseTracing = False
        self.mouseTracingPosition = Box2D.b2Vec2(0.0, 0.0)
        self.mouseTracingVelocity = Box2D.b2Vec2(0.0, 0.0)

        self.cam = cam
        self.world = world
        self.ground = world.CreateStaticBody()

        # Create Particle System
        psDef = Box2D.b2ParticleSystemDef()
        psDef.flags = Box2D.b2_waterParticle
        self.particleSystem = world.CreateParticleSystem(psDef)

        # Particle system initialization
        self.particleSystem.SetRadius(DEF.PARTICLE_RADIUS)  # 0.025
        self.particleSystem.SetDensity(DEF.PARTICLE_DENSITY)

        # ground
        shape = Box2D.b2PolygonShape()
        shape.vertices = [
            Box2D.b2Vec2(-40, -10),
            Box2D.b2Vec2(40, -10),
            Box2D.b2Vec2(40, 0),
            Box2D.b2Vec2(-40, 0)]
        self.ground.CreatePolygonFixture(shape=shape, density=0.0)

        # left wall
        shape = Box2D.b2PolygonShape()
        shape.vertices = [
            Box2D.b2Vec2(-40, -0.1),
            Box2D.b2Vec2(-20, -0.1),
            Box2D.b2Vec2(-20, 20),
            Box2D.b2Vec2(-40, 30)]
        self.ground.CreatePolygonFixture(shape=shape, density=0.0)

        # right wall
        shape = Box2D.b2PolygonShape()
        shape.vertices = [
            Box2D.b2Vec2(20, -0.1),
            Box2D.b2Vec2(40, -0.1),
            Box2D.b2Vec2(40, 30),
            Box2D.b2Vec2(20, 20)]
        self.ground.CreatePolygonFixture(shape=shape, density=0.0)

        # add to showcases
        points = []
        for fixture in self.ground.fixtures:
            for point in fixture.shape.vertices:
                points.append(point)
        index = [0, 1, 3, 1, 2, 3, 4, 5, 7, 5, 6, 7, 8, 9, 11, 9, 10, 11]
        self.figure = graphicsHelper.createMesh(points, index)
        showcase.add(self.figure)

        # particle volume
        shape = Box2D.b2PolygonShape()
        shape.SetAsBox(20, 10, Box2D.b2Vec2(0, 10), 0)
        pd = Box2D.b2ParticleGroupDef()
        pd.shape = shape
        pd.color = Box2D.b2ParticleColor(255, 0, 0, 255)
        group = self.particleSystem.CreateParticleGroup(pd)

        # dynamic box 1
        self.body1 = world.CreateDynamicBody()
        shape = Box2D.b2PolygonShape()
        shape.SetAsBox(2, 2, Box2D.b2Vec2(-10, 5), 0)
        self.body1.CreateFixture(Box2D.b2FixtureDef(shape=shape, density=0.1))
        self.particleSystem.DestroyParticlesInShape(
            shape, self.body1.transform)
        points = self.body1.fixtures[0].shape.vertices
        index = (0, 1, 3, 1, 2, 3)
        self.boxF1 = graphicsHelper.createMesh(points, index)
        self.boxF1.position = (
            self.body1.transform.position.x, self.body1.transform.position.y)
        showcase.add(self.boxF1)

        # dynamic box 2
        self.body2 = world.CreateDynamicBody()
        shape.SetAsBox(2, 2, Box2D.b2Vec2(10, 5), 0)
        self.body2.CreateFixture(Box2D.b2FixtureDef(shape=shape, density=0.1))
        self.particleSystem.DestroyParticlesInShape(
            shape, self.body2.transform)
        points = self.body2.fixtures[0].shape.vertices
        index = (0, 1, 3, 1, 2, 3)
        self.boxF2 = graphicsHelper.createMesh(points, index)
        self.boxF2.position = (
            self.body2.transform.position.x, self.body2.transform.position.y)
        showcase.add(self.boxF2)

        # important: make a step to initialize particles position
        world.Step(DEF.TIME_STEP, DEF.VELOCITY_ITERATION,
                   DEF.POSITION_ITERATION, DEF.PARTICLE_ITERATION)

        # get particle buffers
        posBuff = self.particleSystem.GetPositionBuffer()
        colorBuff = self.particleSystem.GetColorBuffer()
        count = self.particleSystem.GetParticleCount()
        radius = self.particleSystem.GetRadius()

        # important: pass posBuff.this and colorBuff.this as SwigPyObject (parsable at C++)
        self.particle = core.particle(
            posBuff.this, colorBuff.this, count, radius)
        showcase.add(self.particle)