Beispiel #1
0
    def load(self):
        bodyDef = box2d.b2BodyDef()

        if self.shape.type == WorldConfig.BOX:
            bodyDef.position = self.shape.center[0], self.shape.center[1]
            body = WorldConfig.world.CreateBody(bodyDef)
            
            shapeDef = box2d.b2PolygonDef()
            shapeDef.SetAsBox(self.shape.width / 2, self.shape.height / 2)            
        elif self.shape.type == WorldConfig.POLYGON:
            body = WorldConfig.world.CreateBody(bodyDef)
            
            shapeDef = box2d.b2PolygonDef()
            shapeDef.setVertices(self.shape.points)
        elif self.shape.type == WorldConfig.CIRCLE:
            bodyDef.position = self.shape.x, self.shape.y
            body = WorldConfig.world.CreateBody(bodyDef)
            
            shapeDef = box2d.b2CircleDef()
            shapeDef.radius = self.shape.radius
        else:
            raise 'unknown shape'
            return 
                         
#        shapeDef.density = 1
#        shapeDef.friction = 0.1
#        shapeDef.restitution = 1.0
        shapeDef.filter.categoryBits = Wall.catBits
#        shapeDef.filter.maskBits = Wall.maskBits
        body.CreateShape(shapeDef)
        body.SetMassFromShapes()
        body.userData = self
        self.body = body
Beispiel #2
0
    def shape(self):

        if self.shape_type == 'box':
            self.blockShapeDef = box2d.b2PolygonDef()
            self.blockShapeDef.friction =0.3
            self.blockShapeDef.SetAsBox(self.x, self.y)#, self.position ,0)
        if self.shape_type == 'circle':
            self.blockShapeDef = box2d.b2CircleDef()
            self.blockShapeDef.friction =0.3
            #self.blockShapeDef.localPosition.Set(*self.position)
            self.blockShapeDef.localPosition.Set(0,0)
            self.blockShapeDef.radius = self.x
        if self.shape_type == 'triangle':
            self.blockShapeDef = box2d.b2PolygonDef()
            self.blockShapeDef.friction =0.3
            self.blockShapeDef.setVertices( [ (-self.x, 0.0), (self.x, 0.0), (0.0, self.y*2) ])
Beispiel #3
0
    def load(self, size):
        bd=box2d.b2BodyDef()
        bd.position=self.shape.center[0], self.shape.center[1]

        sd=box2d.b2PolygonDef()
        sd.SetAsBox(size[0]/2.0, size[1]/2.0)
        sd.filter.categoryBits= BallGen.catBits
        self.body=WorldConfig.world.CreateBody(bd)
        self.body.CreateShape(sd)
        self.body.SetMassFromShapes()
        self.body.userData = self

        sd.density=2.0
        sd.SetAsBox(0.1, 1.8)
        bd.isBullet=True
        bd.position=self.body.position.x+1, self.body.position.y+4
        body_bar=WorldConfig.world.CreateBody(bd)
        body_bar.CreateShape(sd)
        body_bar.SetMassFromShapes()
                
        jd= box2d.b2RevoluteJointDef()
        jd.Initialize(self.body, body_bar, (self.body.position.x+1, self.body.position.y+size[1]/2.0))
        jd.maxMotorTorque= 30000.0
        jd.motorSpeed    = 2* box2d.b2_pi
        jd.enableMotor   = True        
        WorldConfig.world.CreateJoint(jd).getAsType()
        
        self.body_bar=body_bar
 def handle_enter(self, parameters):
     self.next_state = parameters['next_state']
     self.next_state_parameters = parameters.setdefault('parameters', {})
     self.next_state_parameters={'totalScore':parameters['totalScore'],'edit':parameters['edit']}
     
     self.delay = parameters.setdefault('delay', 2000)
     self.time_passed = 0
     self.background = None
     self.shader = None
     self.screenFont = pygame.font.Font("data/font/fsex2p00_public.ttf", 70)
     text = u"Тоглоом дууслаа"
     self.message = self.screenFont.render(text, True, (255,255,255))
             
     bodyDef = box2d.b2BodyDef()
     bodyDef.position = (8, -2)
     bodyDef.angle = -0.5
     bodyDef.userData = self
     self.message_body = self.world.CreateBody(bodyDef)
     shapeDef = box2d.b2PolygonDef()
     shapeDef.density = 0.5
     shapeDef.friction = 0.95
     shapeDef.restitution = 0.5
     shapeDef.SetAsBox(self.message.get_width()/2.0/self.pixels_per_unit, 
                       self.message.get_height()/2.0/self.pixels_per_unit)
     self.message_body.CreateShape(shapeDef)
     self.message_body.SetMassFromShapes()
     self.message_body.SetLinearVelocity = box2d.b2Vec2(100.0,0)
Beispiel #5
0
    def __init__(self,world,top,size):


        scenter=(top[0]+(size[0]/2),
                top[1]+(size[1]/2))
        wcenter=world.s2wPos(scenter)

        extends=world.s2wScale(size)

        #print 'top: '+str(top)
        #print 'scenter: '+str(scenter)
        #print 'wcenter: '+str(wcenter)
        #print world.w2sPos(wcenter)
        #print 'size: '+str(size)
        #print 'extends: '+str(extends)
        #print world.w2sScale(extends)


        # Physics
        walldef=Box2D.b2BodyDef()
        walldef.position=Box2D.b2Vec2(wcenter[0],wcenter[1])
        walldef.userData={}
        wallbody=world.world.CreateBody(walldef)
        wallbody.iswall=True
        wallshape=Box2D.b2PolygonDef()
        width, height = extends
        wallshape.SetAsBox(width/2, height/2)
        wallbody.CreateShape(wallshape)

        # Display
        self.image=pygame.Surface(size)
        self.image.fill((56,71,216))
        self.rect = self.image.get_rect()
        self.rect.topleft=top
Beispiel #6
0
    def addRevolute(self):
        sd=box2d.b2PolygonDef()
        sd.SetAsBox(1, 0.2)
        sd.density = 2.0
        sd.filter.groupIndex=-2

        bd1=box2d.b2BodyDef()
        ppos= self.body.GetWorldCenter()
        bd1.position = (ppos.x-0.2, ppos.y+0.8)
        body_arm1 = WorldConfig.world.CreateBody(bd1)
        body_arm1.CreateShape(sd)
        body_arm1.SetMassFromShapes()
        
        jointDef1 = box2d.b2RevoluteJointDef()
        jointDef1.Initialize(self.body, body_arm1, (body_arm1.position.x-1, body_arm1.position.y))
#        jointDef1.collideConnected = True      
        jointDef1.lowerAngle    = 0.1 * box2d.b2_pi  # -90 degrees
        jointDef1.upperAngle    = 0.2 * box2d.b2_pi  #  45 degrees
        jointDef1.enableLimit   = True
#        jointDef1.maxMotorTorque= 100.0
        jointDef1.motorSpeed    = 0.0
        jointDef1.enableMotor   = True
        WorldConfig.world.CreateJoint(jointDef1).getAsType() 
        self.bodies.append(body_arm1)


        bd1.position = (body_arm1.position.x+2, body_arm1.position.y)
        body_arm2 = WorldConfig.world.CreateBody(bd1)
        body_arm2.CreateShape(sd)
        body_arm2.SetMassFromShapes()
        
        jointDef1.Initialize(body_arm1, body_arm2, (body_arm2.position.x-1, body_arm2.position.y))
#        jointDef1.collideConnected = True      
        jointDef1.lowerAngle    = -0.25 * box2d.b2_pi  # -90 degrees
        jointDef1.upperAngle    = -0.1 * box2d.b2_pi  #  45 degrees
        jointDef1.enableLimit   = True
#        jointDef1.maxMotorTorque= 1000.0
        jointDef1.motorSpeed    = 0.0
        jointDef1.enableMotor   = True
        WorldConfig.world.CreateJoint(jointDef1).getAsType() 
        self.bodies.append(body_arm2)
        
        bd1.position = (body_arm2.position.x+2, body_arm2.position.y)
        body_arm3 = WorldConfig.world.CreateBody(bd1)
        body_arm3.CreateShape(sd)
        body_arm3.SetMassFromShapes()
        
        jointDef1.Initialize(body_arm2, body_arm3, (body_arm3.position.x-1, body_arm3.position.y))
#        jointDef1.collideConnected = True      
        jointDef1.lowerAngle    = -0.25 * box2d.b2_pi  # -90 degrees
        jointDef1.upperAngle    = -0.1 * box2d.b2_pi  #  45 degrees
        jointDef1.enableLimit   = True
#        jointDef1.maxMotorTorque= 1000.0
        jointDef1.motorSpeed    = 0.0
        jointDef1.enableMotor   = True
        WorldConfig.world.CreateJoint(jointDef1).getAsType() 
        self.bodies.append(body_arm3)
Beispiel #7
0
 def CreateShape(self, midpoint, pos=None):
     if self.dead:
         return
     shape = box2d.b2PolygonDef()
     if pos == None:
         shape.SetAsBox(*midpoint)
     else:
         shape.SetAsBox(midpoint[0], midpoint[1], pos.to_vec(), 0)
     return shape
Beispiel #8
0
 def getShapeDef(self, size):
     if self.shapeDef == None:
         self.shapeDef = box2d.b2PolygonDef()
         self.shapeDef.SetAsBox(size[0]/2.0, size[1]/2.0)
         self.shapeDef.density = 5
         self.shapeDef.friction = 0.0
         self.shapeDef.restitution = 0.5
         self.shapeDef.filter.categoryBits = Rock.catBits
                 
     return self.shapeDef  
Beispiel #9
0
    def load(self):
        bd=box2d.b2BodyDef()
        bd.position=self.shape.center[0], self.shape.center[1]

        sd=box2d.b2PolygonDef()
        sd.SetAsBox(self.shape.width / 2, self.shape.height / 2)
        sd.filter.categoryBits= RockGen.catBits
        self.body=WorldConfig.world.CreateBody(bd)
        self.body.CreateShape(sd)
        self.body.userData = self
Beispiel #10
0
 def __init__(self, world, x, y, vertices):
     bodyDef = b2.b2BodyDef()
     bodyDef.position = (x, y)
     self.body = world.CreateBody(bodyDef)
     shapeDef = b2.b2PolygonDef()
     shapeDef.setVertices_tuple(vertices)
     shapeDef.friction = 0.4
     shapeDef.restitution = 1
     self.body.CreateShape(shapeDef)
     self.body.userData = self
Beispiel #11
0
 def __init__(self, world, x, y, w, h):
     bodyDef = b2.b2BodyDef()
     bodyDef.position = (x + w / 2.0, y + h / 2.0)
     self.body = world.CreateBody(bodyDef)
     shapeDef = b2.b2PolygonDef()
     shapeDef.SetAsBox(w / 2.0, h / 2.0)
     shapeDef.friction = 0.4
     shapeDef.restitution = 1
     self.body.CreateShape(shapeDef)
     self.body.userData = self
Beispiel #12
0
 def create_ground(self, x, y, w, h):
     props = BodyProperties()
     sd = box2d.b2PolygonDef()
     sd.SetAsBox(w, h)
     bd = box2d.b2BodyDef()
     bd.position = (x, y)
     body = self.world.CreateBody(bd)
     body.CreateShape(sd)
     body.SetUserData(props)
     return body
Beispiel #13
0
	def __init__(self, world, x, y, w, h):
		bodyDef = b2.b2BodyDef()
		bodyDef.position = (x + w/2.0, y + h/2.0)
		self.body = world.CreateBody(bodyDef)
		shapeDef = b2.b2PolygonDef()
		shapeDef.SetAsBox(w/2.0, h/2.0)
		shapeDef.friction = 0.4
		shapeDef.restitution = 1
		self.body.CreateShape(shapeDef)
		self.body.userData = self
Beispiel #14
0
	def __init__(self, world, x, y, vertices):
		bodyDef = b2.b2BodyDef()
		bodyDef.position = (x, y)
		self.body = world.CreateBody(bodyDef)
		shapeDef = b2.b2PolygonDef()
		shapeDef.setVertices_tuple(vertices)
		shapeDef.friction = 0.4
		shapeDef.restitution = 1
		self.body.CreateShape(shapeDef)
		self.body.userData = self
Beispiel #15
0
    def load(self, size):
        bd=box2d.b2BodyDef()
        bd.position=self.shape.center[0], self.shape.center[1]

        sd=box2d.b2PolygonDef()
        sd.SetAsBox(size[0]/2.0, size[1]/2.0)
        sd.filter.categoryBits= Launcher.catBits
        self.body=WorldConfig.world.CreateBody(bd)
        self.body.CreateShape(sd)
        self.body.SetMassFromShapes()
        self.body.userData = self
Beispiel #16
0
    def create_character(self, x, y):
        props = BodyProperties(isCharacter=True)
        sd = box2d.b2PolygonDef()
        sd.SetAsBox(10.0, 10.0)
        sd.density = 1.0

        bd = box2d.b2BodyDef()
        bd.position = (x, y)
        body = self.world.CreateBody(bd)
        body.CreateShape(sd)
        body.SetUserData(props)
        return body
Beispiel #17
0
    def create(self, level):
        super(Box, self).create(level)

        shapeDef = b2d.b2PolygonDef()
        shapeDef.SetAsBox(*self.size)

        shapeDef.density = self.density #XXX
        shapeDef.restitution = self.restitution

        self.body.CreateShape(shapeDef)

        if not self.static:
            self.body.SetMassFromShapes()
Beispiel #18
0
    def __init__(self, wld, tank):
        self.wld = wld
        w = wld.w
        self.tank = tank

        self._fuse = None
        self._exploding = False

        r = tank.turret
        pos = r.position
        vel = r.linearVelocity
        ang = r.angle

        blocalvel = box2d.b2Vec2(conf.bulletspeed, 0)
        bwvel = r.GetWorldVector(blocalvel)
        bvel = bwvel + vel
        

        bodyDef = box2d.b2BodyDef()
        blocalpos = box2d.b2Vec2(.1, 0)
        bwpos = r.GetWorldVector(blocalpos)
        bpos = bwpos + pos
        bodyDef.position = bpos
        bodyDef.angle = ang
        bodyDef.isBullet = True
        bodyDef.linearDamping = 0
        bodyDef.userData = {}

        body = w.CreateBody(bodyDef)
        
        
        body.linearVelocity = bvel

        shapeDef = box2d.b2PolygonDef()
        shapeDef.SetAsBox(.1, .1)
        shapeDef.density = conf.bullet_density
        shapeDef.restitution = 0
        shapeDef.friction = 0
        shapeDef.filter.groupIndex = -tank.n
        b = body.CreateShape(shapeDef)
        b.userData = {}
        body.SetMassFromShapes()

        body.userData['actor'] = self
        body.userData['kind'] = 'bullet'
        body.userData['shooter'] = tank

        self.body = body

        v = wld.v.addbullet(pos)
        self.v = v
Beispiel #19
0
    def __init__(self, wld, robot):
        self.wld = wld
        w = wld.w
        self.robot = robot  # Fired by this robot

        self._fuse = None
        self._exploding = False

        r = robot.turret
        pos = r.position
        vel = r.linearVelocity
        ang = r.angle

        blocalvel = box2d.b2Vec2(conf.bulletspeed, 0)
        bwvel = r.GetWorldVector(blocalvel)
        bvel = bwvel + vel
        #print bvel, bvel.Length()

        bodyDef = box2d.b2BodyDef()
        blocalpos = box2d.b2Vec2(.1, 0)
        bwpos = r.GetWorldVector(blocalpos)
        bpos = bwpos + pos
        bodyDef.position = bpos
        bodyDef.angle = ang
        bodyDef.isBullet = True
        bodyDef.linearDamping = 0
        bodyDef.userData = {}

        body = w.CreateBody(bodyDef)
        #print body
        #print 'IB', body.isBullet
        body.linearVelocity = bvel

        shapeDef = box2d.b2PolygonDef()
        shapeDef.SetAsBox(.1, .1)
        shapeDef.density = conf.bullet_density
        shapeDef.restitution = 0
        shapeDef.friction = 0
        shapeDef.filter.groupIndex = -robot.n
        b = body.CreateShape(shapeDef)
        b.userData = {}
        body.SetMassFromShapes()

        body.userData['actor'] = self
        body.userData['kind'] = 'bullet'
        body.userData['shooter'] = robot

        self.body = body

        v = wld.v.addbullet(pos)
        self.v = v
 def __init__(self):
     worldAABB=box2d.b2AABB()
     worldAABB.lowerBound = (-8, -8)
     worldAABB.upperBound = (30, 30)
     
     self.world = box2d.b2World(worldAABB, self.gravity, self.doSleep)        
     
     floor_bodyDef = box2d.b2BodyDef()
     floor_bodyDef.position = (14, 8)
     floor_bodyDef.userData = self
     floor_body = self.world.CreateBody(floor_bodyDef)
     floor_shapeDef = box2d.b2PolygonDef()
     floor_shapeDef.SetAsBox(16, 1)
     floor_body.CreateShape(floor_shapeDef)
Beispiel #21
0
 def __init__(self,physics,vertices):
     self.physics = physics
     self.bodydef = box2d.b2BodyDef()
     self.triangle = drawing.Triangle(globals.ground_buffer)
     #I don't think it matters much, but set the position to the average of the 3 points
     midpoint = ((vertices[0] + vertices[1] + vertices[2])*self.physics.scale_factor)/3.0
     self.bodydef.position = tuple(midpoint)
     self.shape = box2d.b2PolygonDef()
     self.shape.setVertices([ list(vertex*self.physics.scale_factor - midpoint) for vertex in vertices ])
     self.body = physics.world.CreateBody(self.bodydef)
     self.shape.density = 1
     self.shape.friction = 0.5
     self.body.CreateShape(self.shape)
     self.PhysUpdate()
Beispiel #22
0
 def create_ground(self, x, y, w, h):
     rx = x / self.zoom
     ry = y / self.zoom
     rw = (w / 2) / self.zoom
     rh = (h / 2) / self.zoom
     props = BodyProperties()
     sd = box2d.b2PolygonDef()
     sd.SetAsBox(rw, rh)
     bd = box2d.b2BodyDef()
     bd.position = (rx, ry)
     body = self.world.CreateBody(bd)
     body.CreateShape(sd)
     body.SetUserData(props)
     return body
Beispiel #23
0
    def load(self):
        sd1=box2d.b2PolygonDef()
#        sd1.SetAsBox(1,0.5)
            
        sd1.setVertices(self.shape.points)
        sd1.density = 2.0
        
        bd=box2d.b2BodyDef()
        bd.fixedRotation=True
#        bd.position = self.shape.center[0], self.shape.center[1]
        self.body = WorldConfig.world.CreateBody(bd)
        self.body.CreateShape(sd1)
        self.body.SetMassFromShapes()
        self.body.userData = self
Beispiel #24
0
    def __init__(self, w, pos, size):
        walldef = box2d.b2BodyDef()
        walldef.position = pos
        walldef.userData = {}
        wallbod = w.CreateBody(walldef)
        wallbod.userData['actor'] = None
        wallbod.userData['kind'] = 'wall'
        wallbod.iswall = True
        wallshp = box2d.b2PolygonDef()
        width, height = size
        wallshp.SetAsBox(width, height)
        wallbod.CreateShape(wallshp)

        v = view.Wall(pos, size)
        self.v = v
Beispiel #25
0
 def create_phisycs_object(self, world, dynamic):
     bodyDef = box2d.b2BodyDef()
     bodyDef.position = (self.position[0] + self.size[0] / 2, self.position[1] + self.size[1] / 2)
     bodyDef.userdata = self
     body = world.CreateBody(bodyDef)
     shapeDef = box2d.b2PolygonDef()
     shapeDef.SetAsBox(self.size[0] / 2, self.size[1] / 2)
     if dynamic:
         shapeDef.density = 1
     shapeDef.linearDamping = 0.0
     shapeDef.angularDamping = 0.0
     shapeDef.friction = 0.1
     shapeDef.restitution = 1
     body.CreateShape(shapeDef)
     body.SetMassFromShapes()
     return body
Beispiel #26
0
 def __init__(self, w, pos, size):
     walldef = box2d.b2BodyDef()
     walldef.position = pos
     walldef.userData = {}
     wallbod = w.CreateBody(walldef)
     wallbod.userData['actor'] = None
     wallbod.userData['kind'] = 'wall'
     wallbod.iswall = True
     wallshp = box2d.b2PolygonDef()
     width, height = size
     wallshp.SetAsBox(width, height)
     wallbod.CreateShape(wallshp)
     self.body = wallbod
     self.width = size[0]
     self.height = size[1]
     v = view.Wall(pos, size)
     self.v = v
Beispiel #27
0
    def create_character(self, x, y, w, h):
        props = BodyProperties(isCharacter=True)
        sd = box2d.b2PolygonDef()
        rx = x / self.zoom
        ry = y / self.zoom
        rw = (w / 2) / self.zoom
        rh = (h / 2) / self.zoom
        sd.SetAsBox(rw, rh)
        sd.density = 1.0

        bd = box2d.b2BodyDef()
        bd.position = (rx, ry)
        body = self.world.CreateBody(bd)
        body.CreateShape(sd)
        body.SetUserData(props)
        body.SetFixedRotation(True)
        return body
Beispiel #28
0
 def build_map_container(self):
     bd=box2d.b2BodyDef() 
     sd=box2d.b2PolygonDef()
     bd.position = (self.world_width//2, self.world_height//2)
     self.container = self.world.CreateBody(bd)
     
     sd.SetAsBox(self.world_width, 10.0*settings.scale, box2d.b2Vec2(0, -self.world_height/2-5*settings.scale), 0 )
     self.container.CreateShape(sd)
     
     #top?
     #sd.SetAsBox(settings.world_width, 10.0*settings.scale, box2d.b2Vec2(0, settings.world_height/2+5*settings.scale), 0 )
     #self.container.CreateShape(sd)
     
     sd.SetAsBox(10.0*settings.scale, self.world_width, box2d.b2Vec2(self.world_width/2+5*settings.scale, 0), 0 )
     self.container.CreateShape(sd)
     
     sd.SetAsBox(10.0*settings.scale, self.world_width, box2d.b2Vec2(-self.world_width/2-5*settings.scale, 0), 0 )
     self.container.CreateShape(sd)
Beispiel #29
0
    def initilize(self, locx, locy, width, height):
        # Define the ground body.
        groundBodyDef = Box2D.b2BodyDef()
        groundBodyDef.position.Set(locx, locy)
        
        # Call the body factory which allocates memory for the ground body
        # from a pool and creates the ground box shape (also from a pool).
        # The body is also added to the world.
        groundBody = Manager.world.CreateBody(groundBodyDef)
 
        # Define the ground box shape.
        groundShapeDef = Box2D.b2PolygonDef()
 
        # The extents are the half-widths of the box.
        groundShapeDef.SetAsBox(width/2.0, height/2.0)
 
        # Add the ground shape to the ground body.
        groundBody.CreateShape(groundShapeDef)
Beispiel #30
0
    def _buildPhysics(self,width,height,canRotate,isStatic,friction=FRICTION):
        bodyDef = Box2D.b2BodyDef()
        bodyDef.position = (self.physicalPosition[0],self.physicalPosition[1])
        bodyDef.fixedRotation = not canRotate
        bodyDef.linearDamping = 0.15
        self.body = self.physicsWorld.CreateBody(bodyDef)
        #self.body.SetUserData(self)

        shapeDef = Box2D.b2PolygonDef()
        shapeDef.SetAsBox((width / 2.0) /  PIXELS_PER_METER, (height / 2.0) / PIXELS_PER_METER)
        if isStatic:
            shapeDef.density = 0
        else:
            shapeDef.density = DENSITY
        shapeDef.linearDamping = AIR_RESISTANCE
        shapeDef.friction = friction
        
        self.body.CreateShape(shapeDef)
        self.body.SetMassFromShapes()
Beispiel #31
0
 def __init__(self, world, vertices, density=1.0, friction=0.3, restitution=0.1,  texture=None):
     self.world = world
     self._vertices = vertices
     self._display_list = None
     self._texture = None
     if texture:
         self._texture = pyglet.resource.texture(texture)
     
     
     sd = box2d.b2PolygonDef()
     sd.vertexCount = len(vertices)
     i = 0
     for v in vertices:
         sd.setVertex(i, v[0], v[1])
         i += 1
     sd.density = density
     sd.friction = friction
     sd.restitution = restitution
     self.shape_def = sd
Beispiel #32
0
    def initilize(self, locx, locy, width, height):
	# Define the dynamic body. We set its position and call the body factory.
	bodyDef = Box2D.b2BodyDef()
	bodyDef.position.Set(locx, locy)
	self.body = Manager.world.CreateBody(bodyDef)
	 
	# Define another box shape for our dynamic body.
	shapeDef = Box2D.b2PolygonDef()
	shapeDef.SetAsBox(width/2.0, height/2.0)
	 
	# Set the box density to be non-zero, so it will be dynamic.
	shapeDef.density = 1
	 
	# Override the default friction.
	shapeDef.friction = 0.3
	 
	# Add the shape to the body.
	self.body.CreateShape(shapeDef)
	 
	# Now tell the dynamic body to compute it's mass properties base on its shape.
	self.body.SetMassFromShapes()
Beispiel #33
0
    def __init__(self, world):
        eq_s = "x*x/40.0 if x >0 else -x"
        self.eq = compile(eq_s, '<string>', 'eval')


        self.verts = [(x, self.calc(x)) for x in range(-100, 100, 1)]
        # new_ones = [(vert[0]-0.1, vert[1]-0.1) for vert in reversed(self.verts)]
        # self.verts += new_ones

        # bd = physics.b2BodyDef()
        # bd.position = (0.0, 0.0)
        # self.body = world.CreateBody(bd)
        
        # edgeDef = physics.b2EdgeChainDef()
        # edgeDef.setVertices_tuple(self.verts)
        # edgeDef.isALoop = False
        # edgeDef.density = 0
        # self.shape = self.body.CreateShape(edgeDef)

        # self.body.SetMassFromShapes()

        # self.shape.friction = 0
        # self.shape.restitution = 1.0

        for i, vert in enumerate(self.verts):
            if i==0 or i==len(self.verts)-1:
                continue

            bd = physics.b2BodyDef()
            bd.position = (vert[0], vert[1])
            body = world.CreateBody(bd)
            
            edgeDef = physics.b2PolygonDef()
            v = [(self.verts[i-1][0]-vert[0], self.verts[i-1][1]-vert[1]), (0,0), (self.verts[i+1][0]-vert[0], self.verts[i+1][1]-vert[1])]
            edgeDef.setVertices(v)
            shape = body.CreateShape(edgeDef)

            body.mass = 0
Beispiel #34
0
    def initialize_bodies(self):
        # first, calculate the points/vertices
        self.verts = [(x*0.5, self.calc(x*0.5)) for x in range(-200, 200, 1)]

        # for each triplet of vertices, we create a body linking them
        self.bodies = []
        for i, vert in enumerate(self.verts):
            if i==0 or i==len(self.verts)-1:
                continue

            bd = physics.b2BodyDef()
            bd.position = (vert[0], vert[1])
            body = self.world.CreateBody(bd)
            
            edgeDef = physics.b2PolygonDef()
            v = [(self.verts[i-1][0]-vert[0], self.verts[i-1][1]-vert[1]) ,(0,0), (self.verts[i+1][0]-vert[0], self.verts[i+1][1]-vert[1])]
            edgeDef.setVertices(v)
            shape = body.CreateShape(edgeDef)

            # for a fixed body, mass has to be zero
            shape.friction = 1.0
            body.mass = 0

            self.bodies.append(body)
Beispiel #35
0
    def json_load(self, path, additional_vars = {}):
        import cjson

        self.world.GetGroundBody().userData = {"saveid" : 0}

        f = open(path, 'r')
        worldmodel = cjson.decode(f.read())
        f.close()
        #clean world
        for joint in self.world.GetJointList():
            self.world.DestroyJoint(joint)
        for body in self.world.GetBodyList():
            if body != self.world.GetGroundBody():
                self.world.DestroyBody(body)

        #load bodys
        for body in worldmodel['bodylist']:
            bodyDef = box2d.b2BodyDef()
            bodyDef.position = body['position']
            bodyDef.userData = body['userData']
            bodyDef.angle = body['angle']
            newBody = self.world.CreateBody(bodyDef)
            #_logger.debug(newBody)
            newBody.angularVelocity = body['angularVelocity']
            newBody.linearVelocity = body['linearVelocity']
            if body.has_key('shapes'):
                for shape in body['shapes']:
                    if shape['type'] == 'polygon':
                        polyDef = box2d.b2PolygonDef()
                        polyDef.setVertices(shape['vertices'])
                        polyDef.density = shape['density']
                        polyDef.restitution = shape['restitution']
                        polyDef.friction = shape['friction']
                        newBody.CreateShape(polyDef)
                    if shape['type'] == 'circle':
                        circleDef = box2d.b2CircleDef()
                        circleDef.radius = shape['radius']
                        circleDef.density = shape['density']
                        circleDef.restitution = shape['restitution']
                        circleDef.friction = shape['friction']
                        circleDef.localPosition  = shape['localPosition']
                        newBody.CreateShape(circleDef)
                newBody.SetMassFromShapes()

        for joint in worldmodel['jointlist']:
            if joint['type'] == 'distance':
                jointDef = box2d.b2DistanceJointDef()
                body1 = self.getBodyWithSaveId(joint['body1'])
                anch1 = joint['anchor1']
                body2 = self.getBodyWithSaveId(joint['body2'])
                anch2 = joint['anchor2']
                jointDef.collideConnected = joint['collideConnected']
                jointDef.Initialize(body1,body2,anch1,anch2)
                jointDef.SetUserData(joint['userData'])
                self.world.CreateJoint(jointDef)
            if joint['type'] == 'revolute':
                jointDef = box2d.b2RevoluteJointDef()
                body1 = self.getBodyWithSaveId(joint['body1'])
                body2 = self.getBodyWithSaveId(joint['body2'])
                anchor = joint['anchor']
                jointDef.Initialize(body1,body2,anchor)
                jointDef.SetUserData(joint['userData'])
                jointDef.enableMotor = joint['enableMotor']
                jointDef.motorSpeed = joint['motorSpeed']
                jointDef.maxMotorTorque = joint['maxMotorTorque']
                self.world.CreateJoint(jointDef)

        for (k,v) in worldmodel['additional_vars'].items():
            additional_vars[k] = v

        for body in self.world.GetBodyList():
            del body.userData['saveid'] #remove temporary data
Beispiel #36
0
    def __init__(self, wld, kind, name, pos, ang):
        w = wld.w

        Robot.nrobots += 1
        self.n = Robot.nrobots

        self.alive = True
        self.health = conf.maxhealth
        self.kind = kind
        self.name = name

        self._pingtype = 'w'
        self._pingangle = 0
        self._pingdist = 0

        self._pinged = -5  # Tick most recently pinged by another robot's radar

        self._cannonheat = 0
        self._cannonreload = 0

        self._kills = 0  # number of robots killed while this one is still alive
        self._damage_caused = 0

        bodyDef = box2d.b2BodyDef()
        bodyDef.position = pos
        bodyDef.angle = ang

        bodyDef.linearDamping = conf.robot_linearDamping
        bodyDef.angularDamping = conf.robot_angularDamping
        bodyDef.userData = {}

        body = w.CreateBody(bodyDef)

        shapeDef = box2d.b2PolygonDef()
        shapeDef.SetAsBox(1, 1)
        shapeDef.density = conf.robot_density
        shapeDef.friction = conf.robot_friction
        shapeDef.restitution = conf.robot_restitution
        shapeDef.filter.groupIndex = -self.n
        body.CreateShape(shapeDef)
        body.SetMassFromShapes()

        body.userData['actor'] = self
        body.userData['kind'] = 'robot'

        self.body = body

        turretDef = box2d.b2BodyDef()
        turretDef.position = pos
        turretDef.angle = ang

        turretDef.linearDamping = 0
        turretDef.angularDamping = 0
        turret = w.CreateBody(bodyDef)

        shapeDef = box2d.b2PolygonDef()
        shapeDef.SetAsBox(.1, .1)
        shapeDef.density = 1
        shapeDef.friction = 0
        shapeDef.restitution = 0
        shapeDef.filter.groupIndex = -self.n
        turret.CreateShape(shapeDef)
        turret.SetMassFromShapes()
        self.turret = turret

        jointDef = box2d.b2RevoluteJointDef()
        jointDef.Initialize(body, turret, pos)
        jointDef.maxMotorTorque = conf.turret_maxMotorTorque
        jointDef.motorSpeed = 0.0
        jointDef.enableMotor = True
        self.turretjoint = w.CreateJoint(jointDef).getAsType()
        self._turretangletarget = 0

        v = wld.v.addrobot(pos, ang)
        self.v = v

        i = wld.v.addrobotinfo(self.n, name)
        self.i = i
Beispiel #37
0
    def __init__(self, wld, kind, name, pos, ang):
        w = wld.w

        Tank.ntanks += 1
        self.n = Tank.ntanks

        self.alive = True
        self.health = conf.maxhealth
        self.kind = kind
        self.name = name

        self._pingtype = 'w'
        self._pingangle = 0
        self._pingdist = 0

        self._pinged = -5

        self._cannonheat = 0
        self._cannonreload = 0

        self._kills = 0 
        self._damage_caused = 0

        bodyDef = box2d.b2BodyDef()
        bodyDef.position = pos
        bodyDef.angle = ang

        bodyDef.linearDamping = conf.tank_linearDamping
        bodyDef.angularDamping = conf.tank_angularDamping
        bodyDef.userData = {}

        body = w.CreateBody(bodyDef)

        shapeDef = box2d.b2PolygonDef()
        shapeDef.SetAsBox(1, 1)
        shapeDef.density = conf.tank_density
        shapeDef.friction = conf.tank_friction
        shapeDef.restitution = conf.tank_restitution
        shapeDef.filter.groupIndex = -self.n
        body.CreateShape(shapeDef)
        body.SetMassFromShapes()

        body.userData['actor'] = self
        body.userData['kind'] = 'tank'

        self.body = body

        turretDef = box2d.b2BodyDef()
        turretDef.position = pos
        turretDef.angle = ang

        turretDef.linearDamping = 0
        turretDef.angularDamping = 0
        turret = w.CreateBody(bodyDef)

        shapeDef = box2d.b2PolygonDef()
        shapeDef.SetAsBox(.1, .1)
        shapeDef.density = 1
        shapeDef.friction = 0
        shapeDef.restitution = 0
        shapeDef.filter.groupIndex = -self.n
        turret.CreateShape(shapeDef)
        turret.SetMassFromShapes()
        self.turret = turret

        jointDef = box2d.b2RevoluteJointDef()
        jointDef.Initialize(body, turret, pos)
        jointDef.maxMotorTorque = conf.turret_maxMotorTorque
        jointDef.motorSpeed = 0.0
        jointDef.enableMotor = True
        self.turretjoint = w.CreateJoint(jointDef)
        self._turretangletarget = 0

        v = wld.v.addtank(pos, ang)
        self.v = v

        i = wld.v.addtankinfo(self.n, name)
        self.i = i
Beispiel #38
0
#!/usr/bin/python2.6