@@ -46,10 +46,10 @@ fn update(
4646
4747 let range = config . repulsionRange + config . attractionRange ;
4848
49- let min_bin_x = u32 (floor (( position . x - range ) * config . binWidth ) );
50- let min_bin_y = u32 (floor (( position . y - range ) * config . binHeight ) );
51- let max_bin_x = u32 (floor (( position . x + range ) * config . binWidth ) );
52- let max_bin_y = u32 (floor (( position . y + range ) * config . binHeight ) );
49+ let min_bin_x = u32 (( position . x - range ) * config . binWidth );
50+ let min_bin_y = u32 (( position . y - range ) * config . binHeight );
51+ let max_bin_x = u32 (( position . x + range ) * config . binWidth );
52+ let max_bin_y = u32 (( position . y + range ) * config . binHeight );
5353
5454 let wd = config . widthDivisions ;
5555 let hd = config . heightDivisions ;
@@ -89,25 +89,18 @@ fn update(
8989 }
9090
9191 let dist = length (offset );
92- if (dist > range ) {
93- continue ;
94- }
9592
96- if (dist < 0 .5 ) {
97- // Collision (elastic bounce) (also avoids division by zero)
98- let other_velocity = particleVelocities [other_index ];
99- velocity -= dot (velocity - other_velocity , offset / dist ) * (offset / dist );
100- } else if (dist < config . repulsionRange ) {
93+ if (dist < config . repulsionRange ) {
10194 // Repulsion
10295 let strength = (config . repulsionRange - dist ) / config . repulsionRange * config . repulsionStrength * dt ;
10396 velocity -= normalize (offset ) * strength ;
104- } else {
97+ } else if ( dist < range ) {
10598 // Attraction
10699 let other_color = particleColors [other_index ];
107100 let interaction_index = color * COLOR_COUNT + other_color ;
108101 let interaction_strength = interactions [interaction_index ];
109102 let normalized_distance = (config . attractionRange - (dist - config . repulsionRange )) / config . attractionRange ;
110- let symmetric_distance = abs (normalized_distance * 2 .0 - 1 .0 );
103+ let symmetric_distance = 1 .0 - abs (normalized_distance * 2 .0 - 1 .0 );
111104 let strength = symmetric_distance * interaction_strength * config . attractionStrength * dt ;
112105 velocity += normalize (offset ) * strength ;
113106 }
@@ -116,7 +109,7 @@ fn update(
116109 }
117110
118111 // dampen velocity
119- velocity -= normalize ( velocity ) * 12 * dt ;
112+ velocity -= velocity * 8 * dt ;
120113
121114 // update position
122115 position += velocity * dt ;
0 commit comments