jeudi 21 mai 2015

If statement to go through number line

I'm trying to animate a model about a center point, and I want it to swing back and forth (it's a leg). I'm unsure about my if conditions. Particularly when it's trying to reduce the angle. What I've got now, it seems to:

  1. Increase angle until > 46
  2. Go in the last if statement, minus 0.1 from angle, (makes it < 46)
  3. Now instead of continuing to go in the last if statement until angle = 0, it goes back to second if function.

I want it keep on decreasing until 0, then increase again. (Please note that this whole block of code is in a for loop when it's called).

P/s: CenterPointVec[] is a vector containing x, y and z axis values.

void vertex::AnimatePart()
{
    static float angle = 0;
    bool goback = false;

         if(angle == 0)
{
    glTranslatef(CenterPointVec[0], CenterPointVec[1], CenterPointVec[2]);
    glRotatef(angle, 1,0,0);
    glTranslatef(-CenterPointVec[0], -CenterPointVec[1], -CenterPointVec[2]);

    angle += 0.1;
    goback = false;
}

if(angle < 46 && goback == false)
{
    glTranslatef(CenterPointVec[0], CenterPointVec[1], CenterPointVec[2]);
    glRotatef(angle, 1,0,0);
    glTranslatef(-CenterPointVec[0], -CenterPointVec[1], -CenterPointVec[2]);

    angle += 0.1;

    if(angle > 46)
    {
        goback = true;
        cout << "Angle : " << angle << endl;
        cout << "Go back? " << goback << endl;
    }

}

if(angle > 0 && goback == true)
{
    glTranslatef(CenterPointVec[0], CenterPointVec[1], CenterPointVec[2]);
    glRotatef(angle, 1,0,0);
    glTranslatef(-CenterPointVec[0], -CenterPointVec[1], -CenterPointVec[2]);

    angle -= 0.1;

    if(angle <= 0)
        {
            goback = false;
            cout << "Angle : " << angle << endl;
        }
}
}

Aucun commentaire:

Enregistrer un commentaire