I'm modeling a force-based physics simulation where several particles are interacting with each other. The particles can move in such a way that a static viewport can easily lose track of them (imagine the whole group of particles moving off the screen to the right and the viewport displaying a blank background).
My current solution is to simply track the "center" of the particles by finding the min/max of the x's and y's and make the viewport's middle follow that. It works fine (no need to find the true center of mass). Here's a visual:

My problem is I'm attempting to add the ability to manually drag particles around with the mouse. I haven't found an elegant way to do this. The problem is the translation between viewport coordinates and simulation coordinates is constantly changing because the viewport follows the particles' center.
When I mouse down on a particle I want it to have a fixed location in the simulation (it ignores all forces on it). When I move the mouse with the button depressed I want the particle to move in the simulation. I have achieved this. However, it produces unpleasant side-effects because of the viewport and the other particles that are still in motion. The particle isn't moving in the simulation when the mouse is depressed and stationary but it is moving on screen because the center of all the particles changes and the viewport adjusts accordingly. The cursor is stationary so the particle "moves" out from under it. When the mouse is moved ever-so-slightly, the particle teleports back under the cursor because the cursor's new position represents radically different simulation coordinates.
Any suggestions for a nice, coherent user experience without the above side effects?
I've already thought of:
But they all have other bad side-effects or take away value from the simulation.
Also, I think this question is language and platform agnostic so don't attach literal and implementation-specific meanings to "viewport", "viewport coordinates", or "simulation coordinates". Treat them as generic graphics concepts.
Change the algorithm that adjusts the viewport to use the cursor's position as the origin of the transformation. That way the viewport and simulation coordinates at the cursor would remain fixed. You could just do this when the mouse is pressed, but it may also have pleasing results if it were done at all times.
It complicates the maths somewhat, but it doesn't seem like you're left with too many options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With