Skip to content

Commit cd71a6d

Browse files
committed
Float rounding strikes again - set destination coords using integers when moving particles with PSTN
instead of adding a delta value to the current position, which might not give the correct result. Particles (except solids) were on rare occasions ending up at a point 1 pixel away from where they should be after being pushed by PSTN. This led to stacking, and in the case of save 1732622 after changing BIZS to a liquid, to disintegration of the save. (TPT++ version of commit 2ad996d in jacksonmj fork)
1 parent d71a0d9 commit cd71a6d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/simulation/elements/PSTN.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,12 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
282282
//Move particles
283283
for(int j = 0; j < currentPos; j++) {
284284
int jP = tempParts[j];
285-
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = 0;
286-
sim->parts[jP].x += (float)((-directionX)*amount);
287-
sim->parts[jP].y += (float)((-directionY)*amount);
288-
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = sim->parts[jP].type|(jP<<8);
285+
int srcX = (int)(sim->parts[jP].x + 0.5f), srcY = (int)(sim->parts[jP].y + 0.5f);
286+
int destX = srcX-directionX*amount, destY = srcY-directionY*amount;
287+
sim->pmap[srcY][srcX] = 0;
288+
sim->parts[jP].x = destX;
289+
sim->parts[jP].y = destY;
290+
sim->pmap[destY][destX] = sim->parts[jP].type|(jP<<8);
289291
}
290292
return amount;
291293
}
@@ -304,10 +306,12 @@ int Element_PSTN::MoveStack(Simulation * sim, int stackX, int stackY, int direct
304306
}
305307
if(!possibleMovement)
306308
continue;
307-
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = 0;
308-
sim->parts[jP].x += (float)(directionX*possibleMovement);
309-
sim->parts[jP].y += (float)(directionY*possibleMovement);
310-
sim->pmap[(int)(sim->parts[jP].y + 0.5f)][(int)(sim->parts[jP].x + 0.5f)] = sim->parts[jP].type|(jP<<8);
309+
int srcX = (int)(sim->parts[jP].x + 0.5f), srcY = (int)(sim->parts[jP].y + 0.5f);
310+
int destX = srcX+directionX*possibleMovement, destY = srcY+directionY*possibleMovement;
311+
sim->pmap[srcY][srcX] = 0;
312+
sim->parts[jP].x = destX;
313+
sim->parts[jP].y = destY;
314+
sim->pmap[destY][destX] = sim->parts[jP].type|(jP<<8);
311315
}
312316
return possibleMovement;
313317
}

0 commit comments

Comments
 (0)