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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file manim-physics-0.1.0.tar.gz.

File metadata

  • Download URL: manim-physics-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.6 Windows/10

File hashes

Hashes for manim-physics-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b0d77976ff81674d8c488f677076070f4ac893996f3262d9e425f32606c115db
MD5 ff1ae3b7c1515c0b13f1e7e54d965a9c
BLAKE2b-256 d20d2cf6b0f72c8c47b78084df19736427842307f394a0274614ba66007ba063

See more details on using hashes here.

File details

Details for the file manim_physics-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: manim_physics-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.6 Windows/10

File hashes

Hashes for manim_physics-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0cc15acf3cc8e5e05721e0dfd6d8abddada6a2b6fcae08ca323ae58cbf068ead
MD5 8c03de2b2a6a2ca6fcf875bc32ed6dd5
BLAKE2b-256 4e2e626f0c0d621c03536853dee1016374d7c0772714ac01cc2a7c7dcf3fb798

See more details on using hashes here.

Supported by

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