first commit

This commit is contained in:
Looki2000 2023-05-20 10:12:48 +02:00
parent eb00d878da
commit a94e0592a7
2 changed files with 101 additions and 0 deletions

30
gas_sim.py Normal file
View File

@ -0,0 +1,30 @@
from math import hypot
class Particle:
def __init__(self, pos, vel):
self.pos = pos
self.vel = vel
def update(self, dt):
self.pos = (self.pos[0] + self.vel[0] * dt, self.pos[1] + self.vel[1] * dt)
class GasSim:
def __init__(self, particle_radius, particle_compressibility, substeps):
self.particle_radius = particle_radius
self.particle_compressibility = particle_compressibility
self.substeps = substeps
self.particles = []
def add_particle(self, pos, vel = (0, 0)):
self.particles.append(Particle(pos, vel))
def return_particles_pos(self):
return tuple(particle.pos for particle in self.particles)
def cursor_particle_collide_test(self, cursor_pos, cursor_radius):
for particle in self.particles:
if hypot(particle.pos[0] - cursor_pos[0], particle.pos[1] - cursor_pos[1]) <= cursor_radius + self.particle_radius:
return True

71
main.py Normal file
View File

@ -0,0 +1,71 @@
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)