manim-physics

repository·main·Indexed 18 days ago

https://github.com/matheart/manim-physics

A 2D physics simulation plugin for Manim (version 0.4.0) that enables the creation of animations involving rigid mechanics, electromagnetism, wave physics, and optics. It includes specialized modules for electrostatics, magnetostatics, lenses, ray tracing, pendulums, and wave types such as RadialWave, LinearWave, and StandingWave.

Tokens
1.7K
Snippets
6
Records
11
Agent score
63%

What's inside manim-physics

  1. Explore Rigid Mechanics in manim-physics

    main

    The manim_physics.rigid_mechanics module provides tools for simulating rigid body mechanics within Manim. The primary components available for use are:

    • rigid_mechanics: The core module for managing rigid body simulations.
    • pendulum: A specialized component for simulating pendulum motion.

    Detailed API references for these components can be found in the project's reference documentation.

  2. Explore the Optics module in manim-physics

    main

    The manim_physics optics module provides tools for simulating light behavior, specifically through lenses and ray tracing. The module is organized into two primary sub-modules:

    • manim_physics.optics.lenses: Contains classes and functions for simulating various types of optical lenses.
    • manim_physics.optics.rays: Contains tools for simulating light rays and their interactions with optical elements.
  3. Install manim-physics via pip

    main

    Install the manim-physics package directly from PyPI using pip.

    Warning: Do not clone the GitHub repository directly for use, as the repository is under active development and may not be stable. Always use the PyPI package for a stable version.

    pip install manim-physics
  4. Import manim-physics components

    main

    The manim_physics package acts as a central entrypoint that re-exports various physics-related modules for use within Manim animations. Instead of importing from submodules individually, you can import the entire physics suite directly from manim_physics.

    Available physics domains included in the package:

    • Electromagnetism: Electrostatics and Magnetostatics.
    • Optics: Lenses and Rays.
    • Rigid Mechanics: Pendulums and general Rigid Mechanics.
    • Waves: Wave physics components.
    from manim_physics import *
    
    # Now you can use classes from:
    # .electromagnetism.electrostatics
    # .electromagnetism.magnetostatics
    # .optics.lenses
    # .optics.rays
    # .rigid_mechanics.pendulum
    # .rigid_mechanics.rigid_mechanics
    # .wave
  5. Create radial waves with RadialWave

    main

    The RadialWave class creates a 3D Surface where waves propagate outward from one or more source points.

    Parameters:

    • *sources: One or more np.ndarray objects representing the $(x, y, z)$ coordinates of the wave sources. Multiple sources will have their wave amplitudes summed.
    • wavelength (float): The wavelength of the wave.
    • period (float): The time taken for one full oscillation.
    • amplitude (float): The maximum height of the wave.
    • x_range (Iterable[float]): The range of the wave in the x direction (default [-5, 5]).
    • y_range (Iterable[float]): The range of the wave in the y direction (default [-5, 5]).
    • **kwargs: Additional parameters passed to the Manim Surface class.

    Animation Control:

    • start_wave(): Begins the wave propagation animation using an updater.
    • stop_wave(): Stops the wave propagation animation.
    wave = RadialWave(
        LEFT * 2 + DOWN * 5,  # First source
        RIGHT * 2 + DOWN * 5, # Second source
        wavelength=1,
        amplitude=0.1,
        checkerboard_colors=[BLUE_D],
        stroke_width=0,
    )
    self.add(wave)
    wave.start_wave()
    self.wait()
    wave.stop_wave()
  6. Create 2D standing waves with StandingWave

    main

    The StandingWave class creates a 2D wave (represented as a ParametricFunction) that oscillates in place, simulating a standing wave pattern.

    Parameters:

    • n (int): The harmonic number.
    • length (float): The total length of the wave.
    • period (float): The time taken for one full oscillation.
    • amplitude (float): The maximum height of the wave.
    • **kwargs: Additional parameters passed to the Manim ParametricFunction class.

    Animation Control:

    • start_wave(): Begins the standing wave oscillation.
    • stop_wave(): Stops the standing wave oscillation.
    # Example: Creating multiple harmonics
    wave1 = StandingWave(n=1, length=4, amplitude=1)
    wave2 = StandingWave(n=2, length=4, amplitude=1)
    
    waves = VGroup(wave1, wave2)
    da.add(waves)
    for wave in waves:
        wave.start_wave()
    self.wait()
  7. Create linear waves with LinearWave

    main

    The LinearWave class creates a 3D Surface where waves propagate in a single direction (along the x-axis). It inherits from RadialWave but uses a different mathematical model for the z-axis calculation.

    Parameters:

    • wavelength (float): The wavelength of the wave.
    • period (float): The time taken for one full oscillation.
    • amplitude (float): The maximum height of the wave.
    • x_range (Iterable[float]): The range of the wave in the x direction (default [-5, 5]).
    • y_range (Iterable[float]): The range of the wave in the y direction (default [-5, 5]).
    • **kwargs: Additional parameters passed to the Manim Surface class.

    Animation Control:

    • start_wave(): Begins the wave propagation animation.
    • stop_wave(): Stops the wave propagation animation.
    wave = LinearWave(
        wavelength=1,
        period=1,
        amplitude=0.1,
        x_range=[-5, 5],
        y_range=[-5, 5]
    )
    self.add(wave)
    wave.start_wave()
    self.wait()
    wave.stop_wave()