Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 14 March 2012 - 09:55 PM
"Only idiots quote themselves" - MisterFuzzy
Posted 14 March 2012 - 10:11 PM
Posted 15 March 2012 - 11:40 AM
Posted 15 March 2012 - 09:52 PM
void Amoeba::updateForces(float mouseX, float mouseY, bool mouseLbut) // This function makes reference to the following members of the Amoeba class: // const int N = 100; // Number of vertices // double x[N]; // X cooordinates of vertex positions // double y[N]; // Y coordinates of vertex positions // double vx[N]; // X components of vertex velocities // double vy[N]; // Y components of vertex velocities // double Fx[N]; // X components of forces on vertices // double Fy[N]; // Y components of forces on vertices { int k, kprev, knext; double A = polyArea(x, y, N); // Compute O(N) forces kprev = N-1; k = 0; knext = 1; for(;;) { double laplacX = x[kprev] - 2*x[k] + x[knext]; double laplacY = y[kprev] - 2*y[k] + y[knext]; double laplacVx = vx[kprev] - 2*vx[k] + vx[knext]; double laplacVy = vy[kprev] - 2*vy[k] + vy[knext]; double diffX = x[knext]-x[kprev]; double diffY = y[knext]-y[kprev]; // Pressure force double Fpx = -cp*diffY*pow7(A - A0); double Fpy = cp*diffX*pow7(A - A0); // Surface tension (spring) force double Fkx = ck*laplacX; double Fky = ck*laplacY; // Drag force double Fdx = cd*laplacVx; double Fdy = cd*laplacVy; // Mouse force input double Fmousex = 0.0; double Fmousey = 0.0; double mouseDistSq = square(x[k] - mouseX) + square(y[k] - mouseY); if(mouseLbut && mouseDistSq < mouseRsq) { double mouseDist = sqrt(mouseDistSq); double Fmag = mouseFmag*cube(1.0 - mouseDistSq/mouseRsq)/mouseDist; Fmousex = Fmag*(x[k] - mouseX); Fmousey = Fmag*(y[k] - mouseY); } // Force sum Fx[k] = Fpx + Fkx + Fdx + Fmousex; Fy[k] = Fpy + Fky + Fdy + Fmousey; // Update indexes if(k == N-1) break; kprev = k; k = knext; knext = (knext+1)%N; } } void Amoeba::updateSimulation(float mouseX, float mouseY, bool mouseLbut) // This function makes use of the following class members: // const double dt = 0.01; // const double Fmax = 100; // ...and the others mentioned previously. { // Velocity Verlet integration for(int k = 0; k < N; ++k) { x[k] += vx[k]*dt + 0.5*Fx[k]*(dt*dt); y[k] += vy[k]*dt + 0.5*Fy[k]*(dt*dt); vx[k] += (0.5*dt)*Fx[k]; vy[k] += (0.5*dt)*Fy[k]; } updateForces(mouseX, mouseY, mouseLbut); // The previous function for(int k=0; k<N; ++k) { Fx[k] = clamp(Fx[k], -Fmax, Fmax); Fy[k] = clamp(Fy[k], -Fmax, Fmax); } for(int k = 0; k < N; ++k) { vx[k] += (0.5*dt)*Fx[k]; vy[k] += (0.5*dt)*Fy[k]; } }
Posted 16 March 2012 - 07:07 PM
"Only idiots quote themselves" - MisterFuzzy
Posted 16 March 2012 - 07:12 PM
You may want to check out the game Gish, by Edmund McMillen. Apparently it's now open source: http://crypticsea.bl...pen-source.html
Posted 19 March 2012 - 09:13 AM
Posted 19 March 2012 - 11:20 PM
To make a "slime ball" like in GISH is simple, just do like this:
(1) put a number of particles in a circle
(2) connect neighboring particles with Hooke's law damped springs, forming a rubberband
(3) calculate the area of the polygon formed by the particles using this equation
(4) set that area as the slime's rest area
(5) for each loop calculate the slimes new area using the same equation
(6) calculate slime pressure p = k * (area - rest area), where k is a stiffnes coefficient
(7) apply a normal ("inwards" / "outwards") pressure force to each particle pair
(8) voila, now your slime actsas a peace of jelly!
All sorts of modifications can be made, but this is the gist of the idea. I might have a code sample lying around (BASIC dialect and C dialect), I'll post it if I fall over it.
Cheers,
Mike
Posted 20 March 2012 - 02:31 AM
Nice description. Let me point out, in case it isn't clear to the original poster, that this is almost exactly what the code I posted implements (the only difference is that my pressure grows with the 5th power, not linearly (any monotonic function will do)).
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.