Skip to content

Commit

Permalink
Invert y axis and add hanging bridge with gravity case
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelMess committed Jul 20, 2024
1 parent a275cf0 commit ed96ae2
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
83 changes: 82 additions & 1 deletion code/simulator/Cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,47 @@ def force(t: np.float64) -> np.ndarray:
return particles, constraints, force


def case_pendulum() -> Tuple[List[Particle], List[Constraint], Callable[[np.float64], np.ndarray]]:
"""
Pendulum under gravity
"""
particles: List[Particle] = Indexer.indexer([
Particle(np.array([50, -50], dtype=np.float64))
])

constraints: List[Constraint] = Indexer.indexer([
CircleConstraint(particles[0], np.array([0, 0], dtype=np.float64), np.float64(100))
])

def force(t: np.float64) -> np.ndarray:
g = 9.8
return np.array([[0, -g]], dtype=np.float64)

return particles, constraints, force


def case_double_pendulum() -> Tuple[List[Particle], List[Constraint], Callable[[np.float64], np.ndarray]]:
"""
Double pendulum under gravity
"""
particles: List[Particle] = Indexer.indexer([
Particle(np.array([50, -50], dtype=np.float64)),
Particle(np.array([50, -100], dtype=np.float64))
])

constraints: List[Constraint] = Indexer.indexer([
CircleConstraint(particles[0], np.array([0, 0], dtype=np.float64), np.float64(100)),
DistanceConstraint(particles[0], particles[1], np.float64(50))
])

def force(t: np.float64) -> np.ndarray:
g = 9.8
return np.array([[0, -g], [0, -g]], dtype=np.float64)

return particles, constraints, force



def case2() -> Tuple[List[Particle], List[Constraint], Callable[[np.float64], np.ndarray]]:
"""
Distance constraint single particle
Expand Down Expand Up @@ -156,6 +197,42 @@ def force(t: np.float64) -> np.ndarray:
return particles, constraints, force


def case_hanging_bridge() -> Tuple[List[Particle], List[Constraint], Callable[[np.float64], np.ndarray]]:
"""
Distance constraints multi particles
"""
particles: List[Particle] = Indexer.indexer([
Particle(np.array([-90, 0], dtype=np.float64), static=True),
Particle(np.array([-70, 0], dtype=np.float64)),
Particle(np.array([-50, 0], dtype=np.float64)),
Particle(np.array([-30, 0], dtype=np.float64)),
Particle(np.array([-10, 0], dtype=np.float64)),
Particle(np.array([10, 0], dtype=np.float64)),
Particle(np.array([30, 0], dtype=np.float64)),
Particle(np.array([50, 0], dtype=np.float64)),
Particle(np.array([70, 0], dtype=np.float64)),
Particle(np.array([90, 0], dtype=np.float64), static=True),
])

constraints: List[Constraint] = Indexer.indexer([
DistanceConstraint(particles[0], particles[1], np.float64(25)),
DistanceConstraint(particles[1], particles[2], np.float64(25)),
DistanceConstraint(particles[2], particles[3], np.float64(25)),
DistanceConstraint(particles[3], particles[4], np.float64(25)),
DistanceConstraint(particles[4], particles[5], np.float64(25)),
DistanceConstraint(particles[5], particles[6], np.float64(25)),
DistanceConstraint(particles[6], particles[7], np.float64(25)),
DistanceConstraint(particles[7], particles[8], np.float64(25)),
DistanceConstraint(particles[8], particles[9], np.float64(25)),
])

def force(t: np.float64) -> np.ndarray:
g = 9.8
return np.array([[0, -g] for i in range(len(particles))], dtype=np.float64)

return particles, constraints, force


def case6() -> Tuple[List[Particle], List[Constraint], Callable[[np.float64], np.ndarray]]:
"""
Circle constrant single particle
Expand Down Expand Up @@ -312,5 +389,9 @@ def force(t: np.float64) -> np.ndarray:
] =\
{"1": case1,
"dot_single_particle": case_dot_single_particle,
"2": case2, "3": case3, "4": case4, "5": case5, "6": case6, "7": case7, "8": case8, "9": case9,
"pendulum": case_pendulum,
"double_pendulum": case_double_pendulum,
"2": case2, "3": case3, "4": case4, "5": case5,
"hanging_bridge": case_hanging_bridge,
"6": case6, "7": case7, "8": case8, "9": case9,
"10": case10}
2 changes: 1 addition & 1 deletion code/simulator/drawers/CircleConstraintDrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def __init__(self, circleConstraint: CircleConstraint) -> None:
self.circleConstraint = circleConstraint

def draw(self, surface: pygame.Surface, origin: np.ndarray) -> None:
c = self.circleConstraint.center + origin
c = origin + self.circleConstraint.center * np.array([1, -1], dtype=np.float64)
r: float = self.circleConstraint.radius.item()
rect = pygame.Rect((c[0] - r).item(), (c[1] - r).item(), r * 2, r * 2)
pygame.draw.ellipse(surface, (0, 0, 0), rect, width=1)
Expand Down
3 changes: 2 additions & 1 deletion code/simulator/drawers/DistanceConstraintDrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def __init__(self, distanceConstraint: DistanceConstraint):
self.distanceConstraint = distanceConstraint

def draw(self, surface: pygame.Surface, origin: np.ndarray) -> None:
a, b = self.distanceConstraint.particles[0].x + origin, self.distanceConstraint.particles[1].x + origin
a = origin + self.distanceConstraint.particles[0].x * np.array([1, -1], dtype=np.float64)
b = origin + self.distanceConstraint.particles[1].x * np.array([1, -1], dtype=np.float64)
pygame.draw.line(surface, (0, 0, 0), a, b)

def getText(self) -> str:
Expand Down
6 changes: 3 additions & 3 deletions code/simulator/drawers/ParticleDrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def __init__(self, particle: Particle) -> None:

def draw(self, surface: pygame.Surface, origin: np.ndarray) -> None:
c = 0.1
p = origin + self.particle.x
aApplied = p + self.particle.aApplied * c
aConstraint = p + self.particle.aConstraint * c
p = origin + self.particle.x * np.array([1, -1], dtype=np.float64)
aApplied = p + self.particle.aApplied * c * np.array([1, -1], dtype=np.float64)
aConstraint = p + self.particle.aConstraint * c * np.array([1, -1], dtype=np.float64)
pygame.draw.line(surface, (0, 255, 0), p, aApplied)
pygame.draw.line(surface, (255, 0, 0), p, aConstraint)
pygame.draw.circle(surface, (255, 0, 0) if self.particle.static else (0, 0, 255), p, self.radius)
Expand Down

0 comments on commit ed96ae2

Please sign in to comment.