Giter Site home page Giter Site logo

manim-physics's Introduction

manim-physics

Introduction

This is a 2D physics simulation plugin that allows you to generate complicated scenes in various branches of Physics such as rigid mechanics, electromagnetism, wave etc. Due to some reason, I (Matheart) may not have time to maintain this repo, if you want to contribute please seek help from other contributers.

Official Documentation: https://manim-physics.readthedocs.io/en/latest/

Contributors:

Contents

Installation

manim-physics is a package on pypi, and can be directly installed using pip:

pip install manim-physics

Warnings: Please do not directly clone the github repo! The repo is still under development and it is not a stable version, download manim-physics through pypi.

Usage

Make sure include these two imports at the top of the .py file

from manim import *
from manim_physics import *

Rigid Mechanics

Most objects can be made into a rigid body (moves according to gravity and collision) or a static body (stays still within the scene).

To use this feature, the SpaceScene must be used, to access the specific functions of the space.

NOTE

  • This feature utilizes the pymunk package. Although unnecessary, it might make it easier if you knew a few things on how to use it.

    Official Documentation

    Youtube Tutorial

  • A low frame rate might cause some objects to pass static objects as they don't register collisions finely enough. Trying to increase the config frame rate might solve the problem.

Example

# use a SpaceScene to utilize all specific rigid-mechanics methods
class TwoObjectsFalling(SpaceScene):
    def construct(self):
        circle = Circle().shift(UP)
        circle.set_fill(RED, 1)
        circle.shift(DOWN + RIGHT)

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

        ground = Line([-4, -3.5, 0], [4, -3.5, 0])
        wall1 = Line([-4, -3.5, 0], [-4, 3.5, 0])
        wall2 = Line([4, -3.5, 0], [4, 3.5, 0])
        walls = VGroup(ground, wall1, wall2)
        self.add(walls)

        self.play(
            DrawBorderThenFill(circle),
            DrawBorderThenFill(rect),
        )
        self.make_rigid_body(rect, circle)  # Mobjects will move with gravity
        self.make_static_body(walls)  # Mobjects will stay in place
        self.wait(5)
        # during wait time, the circle and rect would move according to the simulate updater

TwoObjectsFalling

Electromagnetism

This section introduces new mobjects:

  • Charge
  • ElectricField
  • Current
  • CurrentMagneticField
  • BarMagnet
  • BarmagneticField
class ElectricFieldExampleScene(Scene):
    def construct(self):
        charge1 = Charge(-1, LEFT + DOWN)
        charge2 = Charge(2, RIGHT + DOWN)
        charge3 = Charge(-1, UP)
        field = ElectricField(charge1, charge2, charge3)
        self.add(charge1, charge2, charge3)
        self.add(field)

ElectricFieldExampleScene

class MagnetismExample(Scene):
    def construct(self):
        current1 = Current(LEFT * 2.5)
        current2 = Current(RIGHT * 2.5, direction=IN)
        field = MagneticField(current1, current2)
        self.add(field, current1, current2)

MagnetismExample

class BarMagnetExample(Scene):
    def construct(self):
        bar1 = BarMagnet().rotate(PI / 2).shift(LEFT * 3.5)
        bar2 = BarMagnet().rotate(PI / 2).shift(RIGHT * 3.5)
        self.add(MagneticField(bar1, bar2))
        self.add(bar1, bar2)

BarMagnetExample

Waves

This section introduces new wave mobjects into manim:

  • LinearWave (3D)
  • RadialWave (3D)
  • StandingWave (2D)
class LinearWaveExampleScene(ThreeDScene):
    def construct(self):
        self.set_camera_orientation(60 * DEGREES, -45 * DEGREES)
        wave = LinearWave()
        self.add(wave)
        wave.start_wave()
        self.wait()
        wave.stop_wave()

LinearWaveExampleScene

class RadialWaveExampleScene(ThreeDScene):
    def construct(self):
        self.set_camera_orientation(60 * DEGREES, -45 * DEGREES)
        wave = RadialWave(
            LEFT * 2 + DOWN * 5, # Two source of waves
            RIGHT * 2 + DOWN * 5,
            checkerboard_colors=[BLUE_D],
            stroke_width=0,
        )
        self.add(wave)
        wave.start_wave()
        self.wait()
        wave.stop_wave()

RadialWaveExampleScene

class StandingWaveExample(Scene):
    def construct(self):
        wave1 = StandingWave(1)
        wave2 = StandingWave(2)
        wave3 = StandingWave(3)
        wave4 = StandingWave(4)
        waves = VGroup(wave1, wave2, wave3, wave4)
        waves.arrange(DOWN).move_to(ORIGIN)
        self.add(waves)
        for wave in waves:
            wave.start_wave()
        self.wait()

StandingWaveExample

Pendulums

MultiPendulum and Pendulum both stem from the Rigid Mechanics feature.

class PendulumExample(SpaceScene):
    def construct(self):
        pends = VGroup(*[Pendulum(i) for i in np.linspace(1,5,7)])
        self.add(pends)
        for p in pends:
            self.make_rigid_body(p.bobs)
            p.start_swinging()
        self.wait(10)

MultiPendulumExample

class MultiPendulumExample(SpaceScene):
    def construct(self):
        p = MultiPendulum(
            RIGHT, LEFT # positions of the bobs.
        )
        self.add(p)
        self.make_rigid_body(p.bobs) # make the bobs fall free.
        p.start_swinging() # attach them to their pivots.
        self.add(TracedPath(p.bobs[-1].get_center, stroke_color=BLUE))
        self.wait(10)

MultiPendulumExample

Lensing

This section showcases ray and lens refraction. Currently only shows refraction and not total internal reflection.

class RayExampleScene(Scene):
    def construct(self):
        lens_style = {"fill_opacity": 0.5, "color": BLUE}
        a = Lens(-5, 1, **lens_style).shift(LEFT)
        a2 = Lens(5, 1, **lens_style).shift(RIGHT)
        b = [
            Ray(LEFT * 5 + UP * i, RIGHT, 8, [a, a2], color=RED)
            for i in np.linspace(-2, 2, 10)
        ]
        self.add(a, a2, *b)

RayExample

Contribution Guidelines

The manim-physics plugin contains objects that are classified into several main branches, now including rigid mechanics simulation, electromagnetism and wave.

If you want to add more objects to the plugin, The classes of the objects should be placed in the python file of corresponding branch, for example, wave.py, and place it under the folder src\manim_physics. The tests of objects should be named as test_thefilename.py such as test_wave.py, with some documentation, so the maintainer of this repo could ensure that it runs as expected.

Other beautiful animations based on manim-physics

Falling formulas

Changelog

v0.2.5

Bugfixes

  • VGroups can be whole rigid bodies. Support for SVGMobjects

v0.2.4 2021.12.25

New Features

  • Hosted official documentation on readthedocs. The readme might be restructured due to redundancy.
  • New lensing module: Mobjects including Lens and Ray
  • SpaceScene can now specify the gravity vector.
  • Fixed ConvertToOpenGL import error for manim v0.15.0.

Improvements

  • Combined BarMagneticField with CurrentMagneticField into MagneticField.
  • Improved the updaters for pendulum module. Frame rate won't showc any lagging in the pendulum rods.

Bugfixes

  • Updated deprecated parameters in the wave module.

v0.2.3 2021.07.14

Bugfixes

  • Fix the small arrow bug in ElectricField

v0.2.2 2021.07.06

New objects

  • Rigid Mechanics: Pendulum

Bugfixes

  • Fix the __all__ bug, now rigid_mechanics.py can run normally.

Improvements

  • Rewrite README.md to improve its readability

v0.2.1 2021.07.03

New objects

  • Electromagnetism: Charge, ElectricField, Current, CurrentMagneticField, BarMagnet, and BarMagnetField
  • Wave: LinearWave, RadialWave, StandingWave

Bugfixes

  • Fix typo

Improvements

  • Simplify rigid-mechanics

v0.2.0 2021.07.01

Breaking Changes

  • Objects in the manim-physics plugin are classified into several main branches including rigid mechanics simulation, electromagnetism and wave.

manim-physics's People

Contributors

icedcoffeeee avatar matheart avatar myzel394 avatar thaza-kun avatar turkeybilly avatar viicos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.