From 05e37a0bd58179584fbd0aaaf8f2866b90609d9c Mon Sep 17 00:00:00 2001 From: Stan44 Date: Thu, 26 Dec 2024 14:22:19 -0600 Subject: [PATCH] made small updates to the spatial grid rearranged some of the code for better readability --- __pycache__/rendering.cpython-312.pyc | Bin 11846 -> 11840 bytes __pycache__/sim.cpython-312.pyc | Bin 30532 -> 30584 bytes rendering.py | 2 +- setup.py | 2 +- sim.py | 132 +++++++++++++------------- simulation_core.pyx | 1 + 6 files changed, 71 insertions(+), 66 deletions(-) diff --git a/__pycache__/rendering.cpython-312.pyc b/__pycache__/rendering.cpython-312.pyc index e845e579e133382894ab2417a15363bd6509942a..fc4b5ac7f50f3bb46ec82b2e0590bef6101d4939 100644 GIT binary patch delta 139 zcmX>Wb0CKAG%qg~0}$Nbl$$Q0vXO5gJLA>O8`zl{McEkCG&VRL5V{Z$bWtj}gQbW6 zhNkZ3a~#r4j2fF?aY->T8f|9fZDD1!-P|P<&(4^*`IV$ECu8qs3*`i6#?6~&s(oc* q+_AY|^8*v(!Oaais*J31tjeF9Ca==%<#%UhH2lN>B8yCbwgUhTaVoq3 delta 145 zcmX>Qb1a7MG%qg~0}u$<=BEEu+Q_$%o$>bO4eZQ}(i{xpk~e@zR2)ctR{>N z|G>kbrMLM6hcpwT?&c?4QcR5IoB#8)urfMrZV`%SXH47tMADa&anfcZI0~6!1%~d+8jI7G6%Ab5DFVXGg_hDu<{KNnvi%fuS003jVD>(oF diff --git a/__pycache__/sim.cpython-312.pyc b/__pycache__/sim.cpython-312.pyc index 56ca267b355ebd95c7475f25b79ba4cd0e08f70b..d93ef72db0efb1bdb707a80d841189c25809fff2 100644 GIT binary patch delta 2120 zcmZvddrXs86u^7GuU{z@3i2qmg0xs$KWSPPl!-4WAmXdwV4}zbv=!@kd0S9H5tC)n zy@(z+Uoo0Fv$?6$T;nnqWy?l3vl(aRvW+a;T=pVHv-FS6eNM=bn4c zz2}^J``LZ+;65>bnV4wO@sW3>wfWzUi{=^05L)!HRH*}Z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_Wt@d z+~k{%fsNDRnNW<-Ls;~)ChV;FOj<&(K1S3h*!c}U~NSyUdrBz1?)$0wW36=q)@&e zQKzZ#3mw1txCYWGfYR`dgxv_QHVF$br_z}w&#jXG03jW2EVX6H8c$UA{xcq`oUh!} ziR+b41|!WW;>D^Xs%IB1%_2-gc)6X-Y{F{noAK8tBdO{2MYp0+Zw$@HB zd9eO8zn$>#<6Nu_Go2`{>sMV=f>G+Q&Jp|`ua@+`Z79utxY|%;r4Q`UBXV%?qWDB= z<78t#!-?Z0bf{`P4(_xMl`;mm(;jDMpekKx{W^XGqcuH0N{gN#G!RIq_cTP=AwOfK zaV}+RhV2>6>b1mD_&&p@^?La&`I6>Z)^CYr1!#)e?Etz8K)IBQ!a zMwf2r`mE^ z`T$tu{VJYLQ>*1&Vls(LEOW|E9imw}G(SxsQOkX}Jjx}Q*H@ilHB_-!iOKL3i{-$fzQVb=hDEV@ zmSmV0o1!=5#LRj_Sn3?3FM($g|?rD1k#3!7qiEH(#5`d&^AC9)YY K9g&ydX8!^8 0: # % chance for smoke + properties = self.particle_properties['smoke'] + new_smoke = Particle.from_type((x, y-1), 'smoke', properties) + if self.particles[x][y-1] is None: + self.particles[x][y-1] = new_smoke + self.active_particles.add((x, y-1)) + + else: + # Handle collision with water + if particle.particle_type == 'water': + self.particles[x][y-1] = None + self.active_particles.discard((x, y-1)) + self.particles[x][y] = None + self.active_particles.discard((x, y)) + self.particles[x][y] = Particle.from_type((x, y), 'water', self.particle_properties['water']) + self.active_particles.add((x, y)) + + def handle_temperature(self, dt): # this is where we handle the temperature. """Handle temperature changes and state transitions""" for x, y in list(self.active_particles): @@ -298,36 +356,7 @@ class Simulation: if particle.temperature > 1000: self.particles[x][y] = None self.active_particles.remove((x, y)) - self.spatial_grid.pop((x, y), None) - - - def get_cell_key(self, x, y): # this is where we get the cell key. - # Convert coordinates to grid cell - cell_x = x // self.cell_size - cell_y = y // self.cell_size - return (cell_x, cell_y) - - - def add_to_spatial_grid(self, particle, x, y): # this is where we add to the spatial grid. - cell_key = self.get_cell_key(x, y) - if cell_key not in self.spatial_grid: - self.spatial_grid[cell_key] = set() - self.spatial_grid[cell_key].add((x, y)) - - - def remove_from_spatial_grid(self, x, y): # this is where we remove from the spatial grid. - cell_key = self.get_cell_key(x, y) - if cell_key in self.spatial_grid: - self.spatial_grid[cell_key].discard((x, y)) - - - def create_particle_circle(self, center_x, center_y): # this is where we create the particle circle. - brush_size = int(self.brush_size) - for dx in range(-brush_size, brush_size + 1): - for dy in range(-brush_size, brush_size + 1): - if dx*dx + dy*dy <= brush_size*brush_size: # Circle check - self.create_particle(center_x + dx * self.particle_size, - center_y + dy * self.particle_size) + self.spatial_grid.pop((x, y), None) def create_particle(self, x, y): # this is where we create the particle. @@ -351,13 +380,15 @@ class Simulation: self.particles[grid_x][grid_y] = new_particle self.active_particles.add((grid_x, grid_y)) - - def update_spatial_grid(self): # this is where we update the spatial grid. - """Update spatial grid for optimized collision detection""" - self.spatial_grid.clear() - for x, y in self.active_particles: - self.add_to_spatial_grid(self.particles[x][y], x, y) - + + def create_particle_circle(self, center_x, center_y): # this is where we create the particle circle. + brush_size = int(self.brush_size) + for dx in range(-brush_size, brush_size + 1): + for dy in range(-brush_size, brush_size + 1): + if dx*dx + dy*dy <= brush_size*brush_size: # Circle check + self.create_particle(center_x + dx * self.particle_size, + center_y + dy * self.particle_size) + def apply_gravity(self, dt): # this is where we apply gravity. """Handle only gravity and basic particle movement""" @@ -419,33 +450,6 @@ class Simulation: particle.position = (new_x, new_y) - def handle_special_particles(self, particle, x, y): # this is where we handle special particles. - """Handle special particle behaviors""" - if particle.particle_type in ['fire', 'flame', 'smoke']: - if random.random() < 0.6: # % chance - self.particles[x][y] = None - self.active_particles.discard((x, y)) - - if particle.particle_type in ['fire', 'flame', 'lava']: - # Create smoke above with proper physics - if random.random() < 0.65 and y > 0: # % chance for smoke - properties = self.particle_properties['smoke'] - new_smoke = Particle.from_type((x, y-1), 'smoke', properties) - if self.particles[x][y-1] is None: - self.particles[x][y-1] = new_smoke - self.active_particles.add((x, y-1)) - - else: - # Handle collision with water - if particle.particle_type == 'water': - self.particles[x][y-1] = None - self.active_particles.discard((x, y-1)) - self.particles[x][y] = None - self.active_particles.discard((x, y)) - self.particles[x][y] = Particle.from_type((x, y), 'water', self.particle_properties['water']) - self.active_particles.add((x, y)) - - def apply_physics(self, dt): # this is where we apply physics. """Handle all physics effects""" new_active_particles = set() diff --git a/simulation_core.pyx b/simulation_core.pyx index 7d3d18e..74d689b 100644 --- a/simulation_core.pyx +++ b/simulation_core.pyx @@ -1,4 +1,5 @@ # simulation_core.pyx +# Cython code for simulating the physics of the system cimport cython from libc.math cimport sqrt