Public Group

# Player animation going funny.

This topic is 2765 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I have a player drawn in OpenGL with animation on it to make the legs and arms move. I have two arrays in the body of my constructor to keep track of movement:

float armAngle[2] = {0.0f, 0.0f};
float legAngle[2] = {0.0f, 0.0f};

I draw a cube in OpenGL and call this function to draw the player arms, legs etc also using color, translate and scale.

The drawPlayer function then draws and animates the player:

void CPlayer::drawPlayer()
{
static bool leg1 = true; // robot's leg states
static bool leg2 = false; // true = forward, false = back

static bool arm1 = true;
static bool arm2 = false;

glPushMatrix();

glTranslatef(m_xPos, m_yPos, m_zPos);// draw robot at desired coordinates

// draw components
drawTorso(1.5f, 0.0f, 0.0f);

glPushMatrix();
// if leg is moving forward, increase angle, else decrease angle
if (arm1)
armAngle[0] = armAngle[0] + 0.1f;
else
armAngle[0] = armAngle[0] - 0.1f;

// once leg has reached its maximum angle in a direction,
// reverse it
if (armAngle[0] >= 15.0f)
arm1 = false;
if (armAngle[0] <= -15.0f)
arm1 = true;

// move the leg away from the torso and rotate it to give "walking" effect
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(armAngle[0], 1.0f, 0.0f, 0.0f);
drawArm(3.5f, 0.0f, -0.5f);
glPopMatrix();

glPushMatrix();
// if leg is moving forward, increase angle, else decrease angle
if (arm2)
armAngle[1] = armAngle[1] + 0.1f;
else
armAngle[1] = armAngle[1] - 0.1f;

// once leg has reached its maximum angle in a direction,
// reverse it
if (armAngle[1] >= 15.0f)
arm2 = false;
if (armAngle[1] <= -15.0f)
arm2 = true;

// move the leg away from the torso and rotate it to give "walking" effect
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(armAngle[1], 1.0f, 0.0f, 0.0f);
drawArm(-2.5f, 0.0f, -0.5f);
glPopMatrix();

//DrawArm(-1.5f, 0.0f, -0.5f);

// we want to rotate the legs relative to the robot's position in the world
// this is leg 1, the robot's right leg
glPushMatrix();

// if leg is moving forward, increase angle, else decrease angle
if (leg1)
legAngle[0] = legAngle[0] + 0.1f;
else
legAngle[0] = legAngle[0] - 0.1f;

// once leg has reached its maximum angle in a direction,
// reverse it
if (legAngle[0] >= 15.0f)
leg1 = false;
if (legAngle[0] <= -15.0f)
leg1 = true;

// move the leg away from the torso and rotate it to give "walking" effect
glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(legAngle[0], 1.0f, 0.0f, 0.0f);

// draw the leg
drawLeg(-0.5f, -5.0f, -0.5f);

glPopMatrix();

// do the same as above with leg 2, the robot's left leg
glPushMatrix();

if (leg2)
legAngle[1] = legAngle[1] + 0.1f;
else
legAngle[1] = legAngle[1] - 0.1f;

if (legAngle[1] >= 15.0f)
leg2 = false;
if (legAngle[1] <= -15.0f)
leg2 = true;

glTranslatef(0.0f, -0.5f, 0.0f);
glRotatef(legAngle[1], 1.0f, 0.0f, 0.0f);
drawLeg(1.5f, -5.0f, -0.5f);
glPopMatrix();
glPopMatrix();
}

This player also has movement on it using GetKeyState called in main.

if(GetKeyState(VK_UP) & 0x80)
{
g_Player1.move(0.0, 0.05, 0.0);
}

The problem is that after moving the player around the screen after a while the arms and legs on the player keep animating round in a full circle rather than the short steps they are supposed to take. I would be grateful for some help with this.

##### Share on other sites
This bug is now fixed.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 19
• 36
• 9
• 16
• 75