|
||
|
GP Mailing List
ATXGPSIG List
|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [gameprogrammer] Re: Spring model
Here's the code for the spring calculations. The gravity isn't applied=20 in this code but it just sets the force.y to some value. And when a =20 objects hit the ground the veloc.y get inverted.=20 =20 I read this gamasutra tutorial when I wrote this code:=20 http://www.gamasutra.com/features/20011005/oliveira_01.htm=20 =20 /* calculate spring forces */=20 for (int i =3D 0; i < (int)springs.size(); i++) {=20 double distance;=20 double intensity;=20 double delta;=20 point force;=20 =20 // calc distance=20 double dx =3D springs.at(i).attach1->pos.x -=20 springs.at(i).attach2->pos.x;=20 double dy =3D springs.at(i).attach1->pos.y - =20 springs.at(i).attach2->pos.y;=20 distance =3D sqrt ((dx*dx)+(dy*dy));=20 =20 if (distance < SPRING_TOLERANCE) {=20 springs.at(i).attach1->force.x =3D 0.0;=20 springs.at(i).attach1->force.y =3D 0.0;=20 springs.at(i).attach2->force.x =3D 0.0;=20 springs.at(i).attach2->force.y =3D 0.0;=20 }else {=20 force.x =3D dx;=20 force.y =3D dy;=20 =20 // normalize=20 force.x /=3D distance;=20 force.y /=3D distance;=20 =20 // force=20 delta =3D springs.at(i).length - distance;=20 intensity =3D springness * delta;=20 =20 // store=20 springs.at(i).attach1->veloc.x +=3D (force.x * intensity);=20 springs.at(i).attach1->veloc.y +=3D (force.y * intensity);=20 springs.at(i).attach2->veloc.x +=3D -(force.x * intensity);=20 springs.at(i).attach2->veloc.y +=3D -(force.y * intensity);=20 }=20 }=20 =20 /* apply forces */=20 for (int i =3D 0; i < (int)points.size(); i++) {=20 point accel;=20 =20 // acceleration=20 accel.x =3D points.at(i).force.x / weight;=20 accel.y =3D points.at(i).force.y / weight;=20 =20 // velocity=20 points.at(i).veloc.x +=3D accel.x * frametime;=20 points.at(i).veloc.y +=3D accel.y * frametime;=20 =20 // position=20 points.at(i).pos.x +=3D (points.at(i).veloc.x * frametime);=20 points.at(i).pos.y +=3D (points.at(i).veloc.y * frametime);=20 =20 // energy loss=20 points.at(i).veloc.x *=3D 1.0 * frametime;=20 points.at(i).veloc.y *=3D 1.0 * frametime;=20 =20 // reset force=20 points.at(i).force.x =3D 0.0;=20 points.at(i).force.y =3D 0.0; =20 }=20 =20 =20 Quoting Alan Wolfe <atrix2@cox.net>:=20 =20 > Heya,=20 > =20 > whats the equation (and/or code) for how your springs behave? maybe th= at=20 > will inspire some ideas (:=20 > =20 > ----- Original Message ----- =20 > From: <straver@apollo.nu>=20 > To: <gameprogrammer@freelists.org>=20 > Sent: Thursday, April 01, 2004 1:53 PM=20 > Subject: [gameprogrammer] Spring model=20 > =20 > =20 > > I'm playing with a spring model but I have run into a trouble.=3D20=20 > > The trouble is that I want a dampning effect on the springs so=3D20=20 > > they don't go all crazy but I don't want any damping effect on=3D20=20 > > the gravity movement for example.=3D20=20 > > =3D20=20 > > I can't think of any way to seperate them.=3D20=20 > >=20 > > -------------------------------------------------=20 > > This mail sent through IMP: http://horde.org/imp/=20 > >=20 > =20 > =20 =20 =20 =20 ------------------------------------------------- This mail sent through IMP: http://horde.org/imp/
|
|