added cursor force and more other changes
This commit is contained in:
65
main.py
65
main.py
@@ -2,6 +2,7 @@ import pygame
|
||||
import pygame.gfxdraw
|
||||
import gas_sim as gs
|
||||
import numpy as np
|
||||
import random
|
||||
|
||||
##### CONFIG #####
|
||||
window_size = (1280, 720)
|
||||
@@ -32,21 +33,13 @@ clock = pygame.time.Clock()
|
||||
|
||||
|
||||
# gas sim init
|
||||
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))
|
||||
gas_sim = gs.GasSim(particle_radius_ws, particle_compressibility, particle_friction, substeps, window_aspect)
|
||||
|
||||
|
||||
not_loaded_particles = True
|
||||
dt = target_fps / 1000
|
||||
|
||||
|
||||
|
||||
# main loop
|
||||
while True:
|
||||
# events
|
||||
@@ -55,8 +48,31 @@ while True:
|
||||
pygame.quit()
|
||||
quit()
|
||||
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
# on s key press
|
||||
if event.key == pygame.K_s:
|
||||
# save particles to file
|
||||
particles = tuple(pos for pos, _ in gas_sim.particles)
|
||||
np.save("particles.npy", particles)
|
||||
|
||||
# on mouse down
|
||||
print("Saved particles to file")
|
||||
|
||||
# on l key press
|
||||
elif event.key == pygame.K_l:
|
||||
if not_loaded_particles:
|
||||
# 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
|
||||
else:
|
||||
print("Particles already loaded !!!")
|
||||
|
||||
|
||||
# on left mouse down
|
||||
if pygame.mouse.get_pressed()[0]:
|
||||
|
||||
mouse_pos_ss = pygame.mouse.get_pos()
|
||||
@@ -64,33 +80,20 @@ while True:
|
||||
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, particle_radius_ws):
|
||||
if not gas_sim.circle_particle_collide_test(mouse_pos_ws, particle_radius_ws):
|
||||
gas_sim.add_particle(mouse_pos_ws)
|
||||
|
||||
# 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
|
||||
# on right mouse down
|
||||
elif pygame.mouse.get_pressed()[2]:
|
||||
# apply wind force
|
||||
mouse_pos_ss = pygame.mouse.get_pos()
|
||||
mouse_pos_ws = (mouse_pos_ss[0] / window_size[1], mouse_pos_ss[1] / window_size[1])
|
||||
|
||||
gas_sim.circle_force(mouse_pos_ws, cursor_radius_ws, (0.2, random.uniform(-0.5, 0.5)), dt)
|
||||
|
||||
|
||||
# update particles
|
||||
#gas_sim.update(dt)
|
||||
|
||||
gas_sim.update(dt)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user