simple-gas-sim/main.py
2023-05-20 10:12:48 +02:00

71 lines
1.6 KiB
Python

import pygame
import pygame.gfxdraw
import gas_sim as gs
##### CONFIG #####
window_size = (1280, 720)
particle_radius_ws = 0.1
particle_compressibility = 0.5
cursor_radius_ws = 0.1
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]
# pygame init
pygame.init()
window = pygame.display.set_mode(window_size)
clock = pygame.time.Clock()
# gas sim init
gas_sim = gs.GasSim(particle_radius_ws, particle_compressibility, substeps)
# 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
if not gas_sim.cursor_particle_collide_test(mouse_pos_ws, cursor_radius_ws):
gas_sim.add_particle(mouse_pos_ws)
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
for particle_pos in gas_sim.return_particles_pos():
particle_pos_ss = (int(particle_pos[0] * window_size[1]), int(particle_pos[1] * window_size[1]))
pygame.gfxdraw.aacircle(window, particle_pos_ss[0], particle_pos_ss[1], int(cursor_radius_ss), (255, 255, 255))
# update
pygame.display.update()
clock.tick(60)