Ejemplo n.º 1
0
def b2PolyToScreen(shape):
    points = []
    for i in xrange(shape.GetVertexCount()):
        pt = box2d.b2Mul(shape.GetBody().GetXForm(), shape.getVertex(i))
        pt = toScreen(pt)           
        points.append(pt) 
    return points   
Ejemplo n.º 2
0
    def draw(self):
        """ If a drawing method is specified, this function passes the objects
            to the module in pixels.
            
            Return: True if the objects were successfully drawn
              False if the renderer was not set or another error occurred
        """
        self.callbacks.start(CALLBACK_DRAWING_START)
        
        # No need to run through the loop if there's no way to draw        
        if not self.renderer: 
            return False

        if self.camera.track_body:
            # Get Body Center
            p1 = self.camera.track_body.GetWorldCenter()   
            
            # Center the Camera There, False = Don't stop the tracking
            self.camera.center(self.to_screen((p1.x*self.ppm, p1.y*self.ppm)), stopTrack=False) 
            
        # Walk through all known elements
        body = self.world.GetBodyList()
        self.renderer.start_drawing()
        
        while body:            
            xform = body.GetXForm()
            shape = body.GetShapeList()
            angle = body.GetAngle()
            
            if shape:
                userdata = body.GetUserData()
                clr = userdata['color']
                                                
            while shape:                
                type = shape.GetType()
                                
                if type == box2d.e_circleShape:
                    circle = shape.asCircle()
                    position = box2d.b2Mul(xform, circle.GetLocalPosition())
                    
                    pos = self.to_screen((position.x*self.ppm, position.y*self.ppm))                    
                    self.renderer.draw_circle(clr, pos, self.meter_to_screen(circle.GetRadius()), angle)

                elif type == box2d.e_polygonShape:
                    poly = shape.asPolygon()
                    points = []
                    for i in xrange(poly.GetVertexCount()):
                        pt = box2d.b2Mul(xform, poly.getVertex(i))
                        x, y = self.to_screen((pt.x*self.ppm, pt.y*self.ppm))
                        points.append([x, y])

                    self.renderer.draw_polygon(clr, points)
                   
                else:
                    print "  unknown shape type:%d" % shape.GetType()
    
                shape = shape.GetNext()  
            body = body.GetNext()

        joint = self.world.GetJointList()
        while joint:
            p2 = joint.GetAnchor1()
            p2 = self.to_screen((p2.x*self.ppm, p2.y*self.ppm))
            
            p1 = joint.GetAnchor2()
            p1 = self.to_screen((p1.x*self.ppm, p1.y*self.ppm))
            
            if p1 == p2: # Fixation
                self.renderer.draw_circle((255,0,0), p1, 4, 0)
            else: # Object to object joint
                self.renderer.draw_lines((0,0,0), False, [p1, p2], 3)
            joint = joint.GetNext()

        self.callbacks.start(CALLBACK_DRAWING_END)
        self.renderer.after_drawing()
        
        return True
Ejemplo n.º 3
0
    def draw(self):
        """ If a drawing method is specified, this function passes the objects
            to the module in pixels.
            
            Return: True if the objects were successfully drawn
              False if the renderer was not set or another error occurred
        """
        self.callbacks.start(CALLBACK_DRAWING_START)

        # No need to run through the loop if there's no way to draw
        if not self.renderer:
            return False

        if self.camera.track_body:
            # Get Body Center
            p1 = self.camera.track_body.GetWorldCenter()

            # Center the Camera There, False = Don't stop the tracking
            self.camera.center(self.to_screen(
                (p1.x * self.ppm, p1.y * self.ppm)),
                               stopTrack=False)

        # Walk through all known elements
        body = self.world.GetBodyList()
        self.renderer.start_drawing()

        while body:
            xform = body.GetXForm()
            shape = body.GetShapeList()
            angle = body.GetAngle()

            if shape:
                userdata = body.GetUserData()
                clr = userdata['color']

            while shape:
                type = shape.GetType()

                if type == box2d.e_circleShape:
                    circle = shape.asCircle()
                    position = box2d.b2Mul(xform, circle.GetLocalPosition())

                    pos = self.to_screen(
                        (position.x * self.ppm, position.y * self.ppm))
                    self.renderer.draw_circle(
                        clr, pos, self.meter_to_screen(circle.GetRadius()),
                        angle)

                elif type == box2d.e_polygonShape:
                    poly = shape.asPolygon()
                    points = []
                    for i in xrange(poly.GetVertexCount()):
                        pt = box2d.b2Mul(xform, poly.getVertex(i))
                        x, y = self.to_screen(
                            (pt.x * self.ppm, pt.y * self.ppm))
                        points.append([x, y])

                    self.renderer.draw_polygon(clr, points)

                else:
                    print "  unknown shape type:%d" % shape.GetType()

                shape = shape.GetNext()
            body = body.GetNext()

        joint = self.world.GetJointList()
        while joint:
            p2 = joint.GetAnchor1()
            p2 = self.to_screen((p2.x * self.ppm, p2.y * self.ppm))

            p1 = joint.GetAnchor2()
            p1 = self.to_screen((p1.x * self.ppm, p1.y * self.ppm))

            if p1 == p2:  # Fixation
                self.renderer.draw_circle((255, 0, 0), p1, 4, 0)
            else:  # Object to object joint
                self.renderer.draw_lines((0, 0, 0), False, [p1, p2], 3)
            joint = joint.GetNext()

        self.callbacks.start(CALLBACK_DRAWING_END)
        self.renderer.after_drawing()

        return True
Ejemplo n.º 4
0
    def draw(self):
        """ If a drawing method is specified, this function passes the objects
            to the module in pixels.

            Return: True if the objects were successfully drawn
              False if the renderer was not set or another error occurred
        """
        self.callbacks.start(CALLBACK_DRAWING_START)

        # No need to run through the loop if there's no way to draw
        if not self.renderer:
            return False

        if self.camera.track_body:
            # Get Body Center
            p1 = self.camera.track_body.GetWorldCenter()

            # Center the Camera There, False = Don't stop the tracking
            self.camera.center(
                self.to_screen(
                    (p1.x * self.ppm,
                     p1.y * self.ppm)),
                stopTrack=False)

        # Walk through all known elements
        self.renderer.start_drawing()

        for body in self.world.bodies:
            xform = body.transform
            shape = body.fixtures
            angle = body.angle

            if shape:
                userdata = body.userData
                clr = userdata['color']

            for fixture in body.fixtures:
                type = fixture.type

                if type == box2d.b2Shape.e_circle:
                    position = box2d.b2Mul(xform, fixture.shape.pos)

                    pos = self.to_screen(
                        (position.x * self.ppm, position.y * self.ppm))
                    self.renderer.draw_circle(
                        clr, pos, self.meter_to_screen(
                            fixture.shape.radius), angle)

                elif type == box2d.b2Shape.e_polygon:
                    points = []
                    for v in fixture.shape.vertices:
                        pt = box2d.b2Mul(xform, v)
                        x, y = self.to_screen(
                            (pt.x * self.ppm, pt.y * self.ppm))
                        points.append([x, y])

                    self.renderer.draw_polygon(clr, points)

                else:
                    print "  unknown shape type:%d" % fixture.type

        for joint in self.world.joints:
            p2 = joint.anchorA
            p2 = self.to_screen((p2.x * self.ppm, p2.y * self.ppm))

            p1 = joint.anchorB
            p1 = self.to_screen((p1.x * self.ppm, p1.y * self.ppm))

            if p1 == p2:
                self.renderer.draw_circle((255, 255, 255), p1, 2, 0)
            else:
                self.renderer.draw_lines((0, 0, 0), False, [p1, p2], 3)

        self.callbacks.start(CALLBACK_DRAWING_END)
        self.renderer.after_drawing()

        return True
Ejemplo n.º 5
0
    def draw(self):
        """ If a drawing method is specified, this function passes the objects
            to the module in pixels.

            Return: True if the objects were successfully drawn
              False if the renderer was not set or another error occurred
        """
        self.callbacks.start(CALLBACK_DRAWING_START)

        # No need to run through the loop if there's no way to draw
        if not self.renderer:
            return False

        if self.camera.track_body:
            # Get Body Center
            p1 = self.camera.track_body.GetWorldCenter()

            # Center the Camera There, False = Don't stop the tracking
            self.camera.center(self.to_screen(
                (p1.x * self.ppm, p1.y * self.ppm)),
                               stopTrack=False)

        # Walk through all known elements
        self.renderer.start_drawing()

        for body in self.world.bodies:
            xform = body.transform
            shape = body.fixtures
            angle = body.angle

            if shape:
                userdata = body.userData
                clr = userdata['color']

            for fixture in body.fixtures:
                type = fixture.type

                if type == box2d.b2Shape.e_circle:
                    position = box2d.b2Mul(xform, fixture.shape.pos)

                    pos = self.to_screen(
                        (position.x * self.ppm, position.y * self.ppm))
                    self.renderer.draw_circle(
                        clr, pos, self.meter_to_screen(fixture.shape.radius),
                        angle)

                elif type == box2d.b2Shape.e_polygon:
                    points = []
                    for v in fixture.shape.vertices:
                        pt = box2d.b2Mul(xform, v)
                        x, y = self.to_screen(
                            (pt.x * self.ppm, pt.y * self.ppm))
                        points.append([x, y])

                    self.renderer.draw_polygon(clr, points)

                else:
                    print "  unknown shape type:%d" % fixture.type

        for joint in self.world.joints:
            p2 = joint.anchorA
            p2 = self.to_screen((p2.x * self.ppm, p2.y * self.ppm))

            p1 = joint.anchorB
            p1 = self.to_screen((p1.x * self.ppm, p1.y * self.ppm))

            if p1 == p2:
                self.renderer.draw_circle((255, 255, 255), p1, 2, 0)
            else:
                self.renderer.draw_lines((0, 0, 0), False, [p1, p2], 3)

        self.callbacks.start(CALLBACK_DRAWING_END)
        self.renderer.after_drawing()

        return True