How to use the pymunk.moment_for_box function in pymunk

To help you get started, we’ve selected a few pymunk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github noio / peas / peas / tasks / walking.py View on Github external
space.damping = 0.7
        self.touching_floor = False
        # Create objects
        # Floor

        floor = pymunk.Body()
        floor.position = pymunk.Vec2d(self.track_length/2.0 , 210)
        sfloor = pymunk.Poly.create_box(floor, (self.track_length, 40))
        sfloor.friction = 1.0
        sfloor.collision_type = 1
        space.add_static(sfloor)

        # Torso
        torsolength = 20 + (self.num_legs // 2 - 1) * self.leg_spacing
        mass = torsolength * self.torso_height * self.torso_density
        torso = pymunk.Body(mass, pymunk.moment_for_box(mass, torsolength, self.torso_height))
        torso.position = pymunk.Vec2d(200, 200 - self.leg_length * 2 - self.torso_height)
        storso = pymunk.Poly.create_box(torso, (torsolength, self.torso_height))
        storso.group = 1
        storso.collision_type = 1
        storso.friction = 2.0
        # space.add_static(storso)
        space.add(torso, storso)

        # Legs
        legs = []
        for i in range(self.num_legs // 2):
            x = 10 - torsolength / 2.0 + i * self.leg_spacing
            y = self.torso_height / 2.0 - 10
            legs.append( Leg(torso, (x,y), self) )
            legs.append( Leg(torso, (x,y), self) )
github pvcraven / arcade / arcade / examples / pymunk_test3.py View on Github external
filename,
                 center_x=0,
                 center_y=0,
                 scale=1,
                 mass=DEFAULT_MASS,
                 moment=None,
                 friction=DEFAULT_FRICTION,
                 body_type=pymunk.Body.DYNAMIC):

        super().__init__(filename, scale=scale, center_x=center_x, center_y=center_y)

        width = self.texture.width * scale
        height = self.texture.height * scale

        if moment is None:
            moment = pymunk.moment_for_box(mass, (width, height))

        self.body = pymunk.Body(mass, moment, body_type=body_type)
        self.body.position = pymunk.Vec2d(center_x, center_y)

        self.shape = pymunk.Poly.create_box(self.body, (width, height))
        self.shape.friction = friction
github viblo / pymunk / tests / unittests.py View on Github external
def testGeneral(self):
        p.version
        p.inf
        p.chipmunk_version
        
        m = p.moment_for_box(1, 2, 3)
        self.assertAlmostEqual(m, 1.08333333333)
        
        m = p.moment_for_segment(1, Vec2d(-1,0), Vec2d(1,0))
        self.assertAlmostEqual(m, 0.33333333333)
github pvcraven / arcade / arcade / examples / pymunk_2.py View on Github external
def make_box(self, x, y):
        size = 45
        mass = 12.0
        moment = pymunk.moment_for_box(mass, (size, size))
        body = pymunk.Body(mass, moment)
        body.position = pymunk.Vec2d(x, y)
        shape = pymunk.Poly.create_box(body, (size, size))
        shape.friction = 0.3
        self.space.add(body, shape)

        sprite = BoxSprite(shape, "images/boxCrate_double.png", width=size, height=size)
        self.sprite_list.append(sprite)
github replit / play / play / play.py View on Github external
def _make_pymunk(self):
        mass = self.mass if self.can_move else 0

        # non-moving line shapes are platforms and it's easier to take care of them less-generically
        if not self.can_move and isinstance(self.sprite, line):
            self._pymunk_body = _physics_space.static_body.copy()
            self._pymunk_shape = _pymunk.Segment(self._pymunk_body, (self.sprite.x, self.sprite.y), (self.sprite.x1, self.sprite.y1), self.sprite.thickness)
        else:
            if self.stable:
                moment = _pymunk.inf
            elif isinstance(self.sprite, Circle):
                moment = _pymunk.moment_for_circle(mass, 0, self.sprite.radius, (0, 0))
            elif isinstance(self.sprite, line):
                moment = _pymunk.moment_for_box(mass, (self.sprite.length, self.sprite.thickness))
            else:
                moment = _pymunk.moment_for_box(mass, (self.sprite.width, self.sprite.height))

            if self.can_move and not self.stable:
                body_type = _pymunk.Body.DYNAMIC
            elif self.can_move and self.stable:
                if self.obeys_gravity or _physics_space.gravity == 0:
                    body_type = _pymunk.Body.DYNAMIC
                else:
                    body_type = _pymunk.Body.KINEMATIC
            else:
                body_type = _pymunk.Body.STATIC
            self._pymunk_body = _pymunk.Body(mass, moment, body_type=body_type)

            if isinstance(self.sprite, line):
                self._pymunk_body.position = self.sprite.x + (self.sprite.x1 - self.sprite.x)/2, self.sprite.y + (self.sprite.y1 - self.sprite.y)/2
github viblo / pymunk / examples / kivy_pymunk_demo / main.py View on Github external
wheel1_s.friction = 1.5
        wheel1_s.color = wheel_color
        space.add(wheel1_b, wheel1_s)

        mass = 100
        radius = 25
        moment = pymunk.moment_for_circle(mass, 20, radius)
        wheel2_b = pymunk.Body(mass, moment)
        wheel2_s = pymunk.Circle(wheel2_b, radius)
        wheel2_s.friction = 1.5
        wheel2_s.color = wheel_color
        space.add(wheel2_b, wheel2_s)

        mass = 100
        size = (50,30)
        moment = pymunk.moment_for_box(mass, size)
        chassi_b = pymunk.Body(mass, moment)
        chassi_s = pymunk.Poly.create_box(chassi_b, size)
        space.add(chassi_b, chassi_s)

        vs = [(0,0),(0,-45),(25,-45)]
        shovel_s = pymunk.Poly(chassi_b, vs, transform = pymunk.Transform(tx=85))
        shovel_s.friction = 0.5
        shovel_s.color = shovel_color
        space.add(shovel_s)

        wheel1_b.position = pos - (55,0)
        wheel2_b.position = pos + (55,0)
        chassi_b.position = pos + (0,25)

        space.add(
            pymunk.PinJoint(wheel1_b, chassi_b, (0,0), (-25,-15)),
github noio / peas / peas / tasks / linefollowing / linefollowing.py View on Github external
def __init__(self, space, field_friction, field_observation, initial_pos,
                       size=8, 
                       motor_torque=6,
                       friction_scale=0.2,
                       angular_damping=0.9,
                       force_global=False):
        self.field_friction = field_friction
        self.field_observation = field_observation
        self.size = size
        self.friction_scale = friction_scale
        self.motor_torque = motor_torque
        self.angular_damping = angular_damping
        self.force_global = force_global
        
        mass = size ** 2 * 0.2
        self.body = body = pymunk.Body(mass, pymunk.moment_for_box(mass, size, size))
        body.position = pymunk.Vec2d(initial_pos[0], initial_pos[1])
        body.angle = initial_pos[2]
        self.shape = shape = pymunk.Poly.create_box(body, (size, size))
        shape.group = 1
        shape.collision_type = 1
        space.add(body, shape)
        
        self.sensors = [(r, theta) for r in np.linspace(1,4,3) * size * 0.75
                                   for theta in np.linspace(-0.5 * np.pi, 0.5 * np.pi, 5)]

        self.l = self.r = 0
github pvcraven / arcade / arcade / examples / pymunk_box_stacks.py View on Github external
# Create the floor
        floor_height = 80
        body = pymunk.Body(body_type=pymunk.Body.STATIC)
        shape = pymunk.Segment(body, [0, floor_height], [SCREEN_WIDTH, floor_height], 0.0)
        shape.friction = 10
        self.space.add(shape)
        self.static_lines.append(shape)

        # Create the stacks of boxes
        for row in range(10):
            for column in range(10):
                size = 32
                mass = 1.0
                x = 500 + column * 32
                y = (floor_height + size / 2) + row * size
                moment = pymunk.moment_for_box(mass, (size, size))
                body = pymunk.Body(mass, moment)
                body.position = pymunk.Vec2d(x, y)
                shape = pymunk.Poly.create_box(body, (size, size))
                shape.elasticity = 0.2
                shape.friction = 0.9
                self.space.add(body, shape)
                # body.sleep()

                sprite = BoxSprite(shape, ":resources:images/tiles/boxCrate_double.png", width=size, height=size)
                self.sprite_list.append(sprite)
github viblo / pymunk / examples / kivy_pymunk_demo / main.py View on Github external
def box(self, space):        
        mass = 10
        moment = pymunk.moment_for_box(mass, (40,20))
        b = pymunk.Body(mass, moment)
        s = pymunk.Poly.create_box(b, (40,20))
        s.friction = 1
        b.position = 600, self.box_y
        self.box_y += 30
        space.add(b,s)
        
        with self.canvas:
            Color(0.2,0.2,0.2)
            s.ky = Quad(points=self.points_from_poly(s))