Skip to content

Commit fbf7210

Browse files
committed
fix division by 0 in PHOT+C5 interaction, shoot the photon off at 90 degrees instead
1 parent c31267b commit fbf7210

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/simulation/Simulation.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,17 +2416,27 @@ int Simulation::try_move(int i, int x, int y, int nx, int ny)
24162416
float vx = ((parts[r>>8].tmp << 16) >> 16) / 255.0f;
24172417
float vy = (parts[r>>8].tmp >> 16) / 255.0f;
24182418
float vn = parts[i].vx * parts[i].vx + parts[i].vy * parts[i].vy;
2419-
parts[i].ctype = (parts[r>>8].ctype & parts[i].ctype) >> 6;
2419+
// if the resulting velocity would be 0, that would cause division by 0 inside the else
2420+
// shoot the photon off at a 90 degree angle instead (probably particle order dependent)
2421+
if (parts[i].vx + vx == 0 && parts[i].vy + vy == 0)
2422+
{
2423+
parts[i].vx = vy;
2424+
parts[i].vy = -vx;
2425+
}
2426+
else
2427+
{
2428+
parts[i].ctype = (parts[r>>8].ctype & parts[i].ctype) >> 6;
2429+
// add momentum of photons to each other
2430+
parts[i].vx += vx;
2431+
parts[i].vy += vy;
2432+
// normalize velocity to original value
2433+
vn /= parts[i].vx * parts[i].vx + parts[i].vy * parts[i].vy;
2434+
vn = sqrtf(vn);
2435+
parts[i].vx *= vn;
2436+
parts[i].vy *= vn;
2437+
}
24202438
parts[r>>8].life = 0;
24212439
parts[r>>8].ctype = 0;
2422-
// add momentum of photons to each other
2423-
parts[i].vx += vx;
2424-
parts[i].vy += vy;
2425-
// normalize velocity to original value
2426-
vn /= parts[i].vx * parts[i].vx + parts[i].vy * parts[i].vy;
2427-
vn = sqrtf(vn);
2428-
parts[i].vx *= vn;
2429-
parts[i].vy *= vn;
24302440
}
24312441
else if(!parts[r>>8].ctype && parts[i].ctype & 0xFFFFFFC0)
24322442
{

0 commit comments

Comments
 (0)