simple-gas-sim/gas_sim.py

30 lines
967 B
Python
Raw Normal View History

2023-05-20 10:12:48 +02:00
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