Add double pendulum simulator, make menu buttons smaller

This commit is contained in:
csd4ni3l
2025-09-27 21:32:53 +02:00
parent a64904bdcc
commit 195a429ff5
3 changed files with 153 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
Some simulator games i tried to make to get better at math and interesting concepts. Currently includes a Boids simulator, Water Simulator, Physics Sandbox, Chladni Plate simulation, Fourier simulation, Lissajous simulation, Voronoi Diagram simulation, Lorenz Attractor simulation, Spirograph simulator. Some simulator games i tried to make to get better at math and interesting concepts. Currently includes a Boids simulator, Water Simulator, Physics Sandbox, Chladni Plate simulation, Fourier simulation, Lissajous simulation, Voronoi Diagram simulation, Lorenz Attractor simulation, Spirograph simulator and a Double Pendulum Simulator.
The Water Simulator simulates 2d water splashes. The Water Simulator simulates 2d water splashes.
The Physics Sandbox includes crates, coins, and any custom SVG you want! Use w to place the current inventory item, and on the right bottom you can select new ones. The Physics Sandbox includes crates, coins, and any custom SVG you want! Use w to place the current inventory item, and on the right bottom you can select new ones.

View File

@@ -0,0 +1,135 @@
import arcade, arcade.gui, json, os, math
from utils.constants import button_style
from utils.preload import button_texture, button_hovered_texture
class Game(arcade.gui.UIView):
def __init__(self, pypresence_client):
super().__init__()
self.pypresence_client = pypresence_client
self.pypresence_client.update(state="Playing a simulator", details="Double Pendulum Simulator")
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
self.settings_box = self.anchor.add(arcade.gui.UIBoxLayout(align="center", size_hint=(0.2, 1)).with_background(color=arcade.color.GRAY), anchor_x="right", anchor_y="bottom")
self.settings_label = self.settings_box.add(arcade.gui.UILabel(text="Settings", font_size=24))
if os.path.exists("data.json"):
with open("data.json", "r") as file:
self.settings = json.load(file)
else:
self.settings = {}
if not "double_pendulum_simulator" in self.settings:
self.settings["double_pendulum_simulator"] = {
"length_a": 200,
"length_b": 200,
"mass_a": 40,
"mass_b": 40,
"gravity": 9.81,
"speed": 1.0,
"trail_size": 500
}
self.theta_a, self.theta_b = math.pi / 2, math.pi / 2
self.omega_a, self.omega_b = 0, 0
self.origin_x, self.origin_y = self.window.width * 0.4, self.window.height / 2
self.trace = []
def on_show_view(self):
super().on_show_view()
self.add_setting("Length A: {value}", 5, 500, 5, "length_a")
self.add_setting("Length B: {value}", 5, 500, 5, "length_b")
self.add_setting("Mass A: {value}", 1, 100, 1, "mass_a")
self.add_setting("Mass B: {value}", 1, 100, 1, "mass_b")
self.add_setting("Gravity: {value}", 0, 25, 0.01, "gravity")
self.add_setting("Speed: {value}", 1, 100, 1, "speed")
self.add_setting("Trail Size: {value}", 0, 5000, 50, "trail_size")
def add_setting(self, text, min_value, max_value, step, settings_key):
label = self.settings_box.add(arcade.gui.UILabel(text.format(value=self.settings["double_pendulum_simulator"][settings_key])))
slider = self.settings_box.add(arcade.gui.UISlider(value=self.settings["double_pendulum_simulator"][settings_key], min_value=min_value, max_value=max_value, step=step))
slider._render_steps = lambda surface: None
slider.on_change = lambda event, label=label: self.change_value(label, text, settings_key, event.new_value)
def change_value(self, label, text, settings_key, value):
label.text = text.format(value=value)
self.settings["double_pendulum_simulator"][settings_key] = value
def on_draw(self):
super().on_draw()
current_settings = self.settings["double_pendulum_simulator"]
L1, L2 = current_settings["length_a"], current_settings["length_b"]
m1, m2 = current_settings["mass_a"], current_settings["mass_b"]
x1 = self.origin_x + L1 * math.sin(self.theta_a)
y1 = self.origin_y - L1 * math.cos(self.theta_a)
x2 = x1 + L2 * math.sin(self.theta_b)
y2 = y1 - L2 * math.cos(self.theta_b)
arcade.draw_line(self.origin_x, self.origin_y, x1, y1, arcade.color.BLACK, 2)
arcade.draw_line(x1, y1, x2, y2, arcade.color.BLACK, 2)
arcade.draw_circle_filled(x1, y1, m1 / 2, arcade.color.RED)
arcade.draw_circle_filled(x2, y2, m2 / 2, arcade.color.BLUE)
arcade.draw_points(self.trace, arcade.color.GREEN, 2)
def on_update(self, dt):
for _ in range(int(self.settings["double_pendulum_simulator"]["speed"])):
current_settings = self.settings["double_pendulum_simulator"]
g = current_settings["gravity"]
m1, m2 = current_settings["mass_a"], current_settings["mass_b"]
L1, L2 = current_settings["length_a"], current_settings["length_b"]
t1, t2 = self.theta_a, self.theta_b
w1, w2 = self.omega_a, self.omega_b
num1 = -g * (2 * m1 + m2) * math.sin(t1)
num2 = -m2 * g * math.sin(t1 - 2 * t2)
num3 = -2 * math.sin(t1 - t2) * m2 * (w2 ** 2 * L2 + w1 ** 2 * L1 * math.cos(t1 - t2))
den = L1 * (2 * m1 + m2 - m2 * math.cos(2 * t1 - 2 * t2))
a1 = (num1 + num2 + num3) / den
num1 = 2 * math.sin(t1 - t2)
num2 = w1 ** 2 * L1 * (m1 + m2)
num3 = g * (m1 + m2) * math.cos(t1)
num4 = w2 ** 2 * L2 * m2 * math.cos(t1 - t2)
den = L2 * (2 * m1 + m2 - m2 * math.cos(2 * t1 - 2 * t2))
a2 = (num1 * (num2 + num3 + num4)) / den
self.omega_a += a1 * dt
self.omega_b += a2 * dt
self.theta_a += self.omega_a * dt
self.theta_b += self.omega_b * dt
x1 = self.origin_x + L1 * math.sin(self.theta_a)
y1 = self.origin_y - L1 * math.cos(self.theta_a)
x2 = x1 + L2 * math.sin(self.theta_b)
y2 = y1 - L2 * math.cos(self.theta_b)
if not current_settings["trail_size"] == 0:
self.trace.append((x2, y2))
if len(self.trace) > current_settings["trail_size"]:
self.trace = self.trace[-int(current_settings["trail_size"]):]
else:
self.trace = []
def on_key_press(self, symbol, modifiers):
if symbol == arcade.key.ESCAPE:
with open("data.json", "w") as file:
file.write(json.dumps(self.settings, indent=4))
from menus.main import Main
self.window.show_view(Main(self.pypresence_client))

View File

@@ -52,34 +52,37 @@ class Main(arcade.gui.UIView):
self.title_label = self.box.add(arcade.gui.UILabel(text="Simulator Games", font_name="Roboto", font_size=48)) self.title_label = self.box.add(arcade.gui.UILabel(text="Simulator Games", font_name="Roboto", font_size=48))
self.boid_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Boid Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.boid_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Boid Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.boid_simulator_button.on_click = lambda event: self.boid_simulator() self.boid_simulator_button.on_click = lambda event: self.boid_simulator()
self.water_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Water Splash Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.water_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Water Splash Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.water_simulator_button.on_click = lambda event: self.water_simulator() self.water_simulator_button.on_click = lambda event: self.water_simulator()
self.physics_playground_button = self.box.add(arcade.gui.UITextureButton(text="Physics Playground", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.physics_playground_button = self.box.add(arcade.gui.UITextureButton(text="Physics Playground", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.physics_playground_button.on_click = lambda event: self.physics_playground() self.physics_playground_button.on_click = lambda event: self.physics_playground()
self.fourier_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Fourier Drawing Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.fourier_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Fourier Drawing Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.fourier_simulator_button.on_click = lambda event: self.fourier_simulator() self.fourier_simulator_button.on_click = lambda event: self.fourier_simulator()
self.chladni_plate_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Chladni Plate Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.chladni_plate_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Chladni Plate Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.chladni_plate_simulator_button.on_click = lambda event: self.chladni_plate_simulator() self.chladni_plate_simulator_button.on_click = lambda event: self.chladni_plate_simulator()
self.lissajous_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Lissajous Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.lissajous_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Lissajous Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.lissajous_simulator_button.on_click = lambda event: self.lissajous_simulator() self.lissajous_simulator_button.on_click = lambda event: self.lissajous_simulator()
self.voronoi_diagram_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Voronoi Diagram Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.voronoi_diagram_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Voronoi Diagram Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.voronoi_diagram_simulator_button.on_click = lambda event: self.voronoi_diagram_simulator() self.voronoi_diagram_simulator_button.on_click = lambda event: self.voronoi_diagram_simulator()
self.lorenz_attractor_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Lorenz Attractor Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.lorenz_attractor_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Lorenz Attractor Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.lorenz_attractor_simulator_button.on_click = lambda event: self.lorenz_attractor_simulator() self.lorenz_attractor_simulator_button.on_click = lambda event: self.lorenz_attractor_simulator()
self.spirograph_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Spirograph Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.spirograph_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Spirograph Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.spirograph_simulator_button.on_click = lambda event: self.spirograph_simulator() self.spirograph_simulator_button.on_click = lambda event: self.spirograph_simulator()
self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 14, style=big_button_style)) self.double_pendulum_simulator_button = self.box.add(arcade.gui.UITextureButton(text="Double Pendulum Simulator", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.double_pendulum_simulator_button.on_click = lambda event: self.double_pendulum_simulator()
self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=self.window.height / 16, style=big_button_style))
self.settings_button.on_click = lambda event: self.settings() self.settings_button.on_click = lambda event: self.settings()
def physics_playground(self): def physics_playground(self):
@@ -118,6 +121,10 @@ class Main(arcade.gui.UIView):
from game.spirograph_simulator.game import Game from game.spirograph_simulator.game import Game
self.window.show_view(Game(self.pypresence_client)) self.window.show_view(Game(self.pypresence_client))
def double_pendulum_simulator(self):
from game.double_pendulum_simulator.game import Game
self.window.show_view(Game(self.pypresence_client))
def settings(self): def settings(self):
from menus.settings import Settings from menus.settings import Settings
self.window.show_view(Settings(self.pypresence_client)) self.window.show_view(Settings(self.pypresence_client))