Skip to content

Commit 8ddae11

Browse files
committed
Started the physics engine
Started a physical system class together with Body and Force
1 parent 735a15a commit 8ddae11

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

manimlib/physics/__init__.py

Whitespace-only changes.

manimlib/physics/body.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import TYPE_CHECKING
2+
3+
if TYPE_CHECKING:
4+
import numpy as np
5+
from manimlib.mobject.mobject import Mobject
6+
7+
8+
class Body:
9+
"""
10+
Represents a body in a physical system
11+
"""
12+
13+
def __init__(
14+
self,
15+
mass: float,
16+
pos: np.ndarray,
17+
mobj: Mobject
18+
) -> None:
19+
"""
20+
Initialize a new Body object
21+
"""
22+
23+
self.mass: float = mass
24+
if self.mass < 0:
25+
raise Exception(
26+
f"Current value for mass ({self.mass}) is negative!"
27+
)
28+
29+
self.pos: np.ndarray = pos
30+
if len(self.pos.shape) != 1 or self.pos.shape[0] == 3:
31+
raise Exception("Position is invalid!")
32+
33+
self.mobj: Mobject = mobj
34+
self.mobj.move_to(self.pos)
35+
36+
# index of the body in the physical system, to be set
37+
# by a PhysicalSystem
38+
self.index: int = -1

manimlib/physics/force.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
from typing import TYPE_CHECKING
4+
5+
if TYPE_CHECKING:
6+
import numpy as np
7+
from manimlib.physics.body import Body
8+
9+
10+
11+
class Force(metaclass=ABCMeta):
12+
"""
13+
Represents a force among bodies in a physical
14+
system
15+
"""
16+
17+
def __init__(self, bodies: tuple[Body]) -> None:
18+
"""
19+
Initialize a new Force object
20+
21+
Keyword arguments
22+
-----------------
23+
bodies: the bodies to which the force applies
24+
"""
25+
self.bodies: tuple[Body] = bodies
26+
if not self.bodies:
27+
raise Exception("No bodies have been provided!")
28+
29+
@abstractmethod
30+
def apply(forces: np.ndarray):
31+
"""
32+
Apply the force and add the contributions to the 'total'
33+
forces in the system
34+
35+
Keyword arguments
36+
-----------------
37+
forces: with shape (nbodies, DIMENSIONS), each row stores the
38+
total force exerted in the body with that index
39+
"""
40+
pass
41+
42+
43+
class NewtonGravitationalForce(Force):
44+
"""
45+
Newton's gravitational force between two masses
46+
F = - G * m1 * m2 / r^2
47+
"""
48+
49+
def __init__(self, bodies: tuple[Body]) -> None:
50+
super().__init__(bodies)

manimlib/physics/physical_system.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from manimlib.mobject.mobject import Mobject
2+
3+
from typing import TYPE_CHECKING
4+
5+
if TYPE_CHECKING:
6+
from manimlib.physics.body import Body
7+
from manimlib.physics.force import Force
8+
9+
class PhysicalSystem(Mobject):
10+
"""
11+
Represents a physical system
12+
"""
13+
14+
def __init__(self, bodies: list[Body]=[], forces: list[Force]=[], **kwargs):
15+
super().__init__(**kwargs)
16+
self.bodies: list[Body] = bodies
17+
for i, body in enumerate(self.bodies): # set indices for the bodies
18+
body.index = i
19+
self.forces: list[Force] = forces

0 commit comments

Comments
 (0)