Skip to main content

Support physics simulation

Project description

manim-physics

A physics simulation plugin based on Pymunk, with this plugin you could generate complicated physics scenes without struggling to use many updaters.

Contributed by pdcxs and Matheart

Installation

Follow this guide: https://docs.manim.community/en/stable/installation/plugins.html?highlight=plugin

A simple Example

class TwoObjectsFalling(Scene):
    def construct(self):
        space = Space(dt = 1 / self.camera.frame_rate) 
        # space is the basic unit of simulation (just like scene)
        # you can add rigid bodies, shapes and joints to it 
        # and then step them all forward together through time
        self.add(space)

        circle = Circle().shift(UP)
        circle.set_fill(RED, 1)
        circle.shift(DOWN + RIGHT)

        circle.body = pymunk.Body() # add a rigid body to the circle
        circle.body.position = \
            circle.get_center()[0], \
            circle.get_center()[1]
        circle.shape = pymunk.Circle(
            body = circle.body,
            radius = circle.width / 2
        ) # set the shape of the circle in pymunk
        circle.shape.elasticity = 0.8
        circle.shape.density = 1
        circle.angle = 0

        rect = Square().shift(UP)
        rect.rotate(PI/4)
        rect.set_fill(YELLOW_A, 1)
        rect.shift(UP*2)
        rect.scale(0.5)

        rect.body = pymunk.Body()
        rect.body.position = \
            rect.get_center()[0], \
            rect.get_center()[1]
        rect.body.angle = PI / 4
        rect.shape = pymunk.Poly.create_box(rect.body, (1, 1))
        rect.shape.elasticity = 0.4
        rect.shape.density = 2
        rect.shape.friction = 0.8
        rect.angle = PI / 4

        ground = Rectangle(width = 8, height = 0.1, color = GREEN).set_fill(GREEN, 1)
        ground.shift(3.5*DOWN)
        ground.body = space.space.static_body 
        # static body means the object keeps static even after collision
        ground.shape = pymunk.Segment(ground.body, (-4, -3.5), (4, -3.5), 0.1)
        ground.shape.elasticity = 0.99
        ground.shape.friction = 0.8
        self.add(ground)

        wall1 = Rectangle(width = 0.1, height = 7, color = GREEN).set_fill(GREEN, 1)
        wall1.shift(3.95*LEFT)
        wall1.body = space.space.static_body
        wall1.shape = pymunk.Segment(wall1.body, (-4, -5), (-4, 5), 0.1)
        wall1.shape.elasticity = 0.99
        self.add(wall1)

        wall2 = Rectangle(width = 0.1, height = 7, color = GREEN).set_fill(GREEN, 1)
        wall2.shift(3.95*RIGHT) 
        wall2.body = space.space.static_body
        wall2.shape = pymunk.Segment(wall2.body, (4, -5), (4, 5), 0.1)
        wall2.shape.elasticity = 0.99
        self.add(wall2)

        self.play(
            DrawBorderThenFill(circle),
            DrawBorderThenFill(rect))
        self.wait()

        space.add_body(circle)
        space.add_body(rect)
        space.add_body(ground)
        space.add_body(wall1)
        space.add_body(wall2)

        space.add_updater(step)
        circle.add_updater(simulate)
        rect.add_updater(simulate)
        self.wait(10)
        # during wait time, the circle and rect would move according to the simulate updater

Other beautiful animations based on manim-physics

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

manim-physics-0.1.0.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

manim_physics-0.1.0-py3-none-any.whl (3.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page