class Creature1 { enum states { FINISHED, CRAWLING }; private: float FootLength ; float LegLength ; float FrontPosX ; float FrontPosZ ; float KneeAngle ; float PosAngle ; int WalkSteps ; int StepsDone ; float Width ; float x [5]; float y [5]; int state; void Init (void) // Initialise and re-initialise { StepsDone = 0 ; KneeAngle = PI / 2.0; x[0] = 0.0; x[1] = FootLength ; x[2] = x[1] + LegLength*sin (KneeAngle); x[3] = 2.0*x[2] - x[1] ; x[4] = x[3] + FootLength; y[0] = 0.0; y[1] = 0.0; y[2] = LegLength* cos (KneeAngle); y[3] = 0.0; y[4] = 0.0; } // of Init public: Creature1(void) { Width = 10.0; FootLength = 20.0; LegLength = 50.0; FrontPosX = 0.0; FrontPosZ = -100.0; PosAngle = 0.0; WalkSteps = 51 ; // it takes Walksteps steps to complete a step... ;-) Init(); state = CRAWLING; } // of Constructor int Animate () // this hopefully makes the creature walk.... // in the first Walksteps/2 steps let the rest be dragged after // then make the front move { if (StepsDone == WalkSteps) // move the Creatures Position now and reset the counter { FrontPosX -= (2.0 * LegLength)*cos(PosAngle) ; FrontPosZ -= (2.0 * LegLength)*sin(PosAngle) ; Init(); state = FINISHED; } // of if else { state = CRAWLING; StepsDone ++; if (StepsDone <= WalkSteps/2) { // the front two vertices stay as they are... the rest moves. KneeAngle = (PI/2.0)*(1.0 - (float) 2.0*StepsDone/WalkSteps) ; x[2] = x[1] + LegLength * sin (KneeAngle); x[3] = 2.0*x[2] - x[1]; x[4] = x[3] + FootLength; y[2] = LegLength * cos (KneeAngle); } // of if (drag back) else // the reference is now the back!! { KneeAngle = (PI/2.0)*(-1.0+(float) 2.0*StepsDone/WalkSteps) ; /* x[4] = 2.0 * FootLength; x[3] = x[4] - FootLength; */ // the above two should be set already! x[2] = x[3] - LegLength * sin (KneeAngle); x[1] = 2.0*x[2] - x[3] ; x[0] = x[1] - FootLength; y[2] = LegLength * cos (KneeAngle); }// of else (move front) } // of else return (state); } // of Animate void Draw (void) { glColor3f(1.0,0.0,0.0); float SIN = sin( PosAngle); float COS = cos( PosAngle); glBegin (GL_QUAD_STRIP); for (int i=0; i<5; i++) { glVertex3f (FrontPosX+x[i]*COS,y[i],FrontPosZ+x[i]*SIN); glVertex3f (FrontPosX+x[i]*COS+SIN*Width,y[i],FrontPosZ+x[i]*SIN-COS*Width); // printf ("x:%f, y:%f, Z:%f \n",FrontPosX+x[i]*COS,y[i],FrontPosZ+x[i]*SIN); } // of for glEnd(); // of drawing Sequence } // of Draw void Rotate ( float Angle) { /* if (PosAngle> 2.0* PI) PosAngle = 0.0; else { if (PosAngle < 0.0) PosAngle = 2.0 * PI; else PosAngle += Angle; } // of first else printf ("Angle: %f\n",PosAngle); */ PosAngle += Angle; } // of Angle }; // of Creature1