simple-gas-sim/main.py

111 lines
2.6 KiB
Python
Raw Normal View History

2023-05-20 10:12:48 +02:00
import pygame
import pygame.gfxdraw
import gas_sim as gs
2023-05-20 20:05:38 +02:00
import numpy as np
2023-05-20 10:12:48 +02:00
##### CONFIG #####
window_size = (1280, 720)
2023-05-20 20:05:38 +02:00
target_fps = 60
2023-05-20 10:12:48 +02:00
2023-05-20 20:05:38 +02:00
particle_radius_ws = 0.025
particle_compressibility = 0.1
particle_friction = 0.1
2023-05-20 10:12:48 +02:00
2023-05-20 20:05:38 +02:00
cursor_radius_ws = 0.15
2023-05-20 10:12:48 +02:00
substeps = 10
##################
# ws - world space
# ss - screen space
window_aspect = window_size[0] / window_size[1]
cursor_radius_ss = cursor_radius_ws * window_size[1]
2023-05-20 20:05:38 +02:00
particle_radius_ss = particle_radius_ws * window_size[1]
2023-05-20 10:12:48 +02:00
# pygame init
pygame.init()
window = pygame.display.set_mode(window_size)
clock = pygame.time.Clock()
# gas sim init
2023-05-20 20:05:38 +02:00
gas_sim = gs.GasSim(particle_radius_ws, particle_compressibility, particle_friction, substeps)
# add walls
gas_sim.add_wall((0, 0), (window_aspect, 0))
gas_sim.add_wall((0, 1), (window_aspect, 1))
gas_sim.add_wall((0, 0), (0, 1))
gas_sim.add_wall((1, 0), (1, 1))
not_loaded_particles = True
dt = target_fps / 1000
2023-05-20 10:12:48 +02:00
# main loop
while True:
# events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# on mouse down
if pygame.mouse.get_pressed()[0]:
mouse_pos_ss = pygame.mouse.get_pos()
mouse_pos_ws = (mouse_pos_ss[0] / window_size[1], mouse_pos_ss[1] / window_size[1])
# if not colliding with any particles
2023-05-20 20:05:38 +02:00
if not gas_sim.cursor_particle_collide_test(mouse_pos_ws, particle_radius_ws):
2023-05-20 10:12:48 +02:00
gas_sim.add_particle(mouse_pos_ws)
2023-05-20 20:05:38 +02:00
# on s key press
if pygame.key.get_pressed()[pygame.K_s]:
# save particles to file
particles = tuple(pos for pos, _ in gas_sim.particles)
np.save("particles.npy", particles)
print("Saved particles to file")
# on l key press
if not_loaded_particles and pygame.key.get_pressed()[pygame.K_l]:
# load particles from file
particles = np.load("particles.npy")
for particle in particles:
gas_sim.add_particle(particle)
print("Loaded particles from file")
not_loaded_particles = False
# update particles
#gas_sim.update(dt)
2023-05-20 10:12:48 +02:00
window.fill((0, 0, 0))
# draw cursor
cursor_pos = pygame.mouse.get_pos()
pygame.gfxdraw.aacircle(window, cursor_pos[0], cursor_pos[1], int(cursor_radius_ss), (255, 255, 255))
# draw particles
2023-05-20 20:05:38 +02:00
for particle_pos, _ in gas_sim.particles:
2023-05-20 10:12:48 +02:00
particle_pos_ss = (int(particle_pos[0] * window_size[1]), int(particle_pos[1] * window_size[1]))
2023-05-20 20:05:38 +02:00
pygame.gfxdraw.aacircle(window, particle_pos_ss[0], particle_pos_ss[1], int(particle_radius_ss), (255, 255, 255))
2023-05-20 10:12:48 +02:00
# update
pygame.display.update()
2023-05-20 20:05:38 +02:00
dt = clock.tick(60) / 1000