Simple roboter rotating hand and legs

I started 2days ago learning opengl. I built a simple robot with 8 parts.Now I want that my robot is moving his hands and legs as if he was walking. I want do with glRotate. But I don’t know how I should start. I tried it but the body part(hand) got through rotated. Do you have some advice or something else? I want to keep it simple.

   #include <iostream> 
#include <GL/freeglut.h>         
#include "Wuerfel.h"

static float fRHand = 0., fRUnterHand = 0; //right upper hand and right under hand
static float fX = 1, fY = 1, fZ = 1;



void Init()
{
   
   glClearColor(0, 0, 0, 1);
   glEnable(GL_DEPTH_TEST);
}

void RenderScene() 
{
   glLoadIdentity();   
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


   gluLookAt(0,0., 3	 // Betrachterposition
   				, 0., 0., 0.,//Blickziel
   				0, 0.1, 0.); // Oben-Betrachter

   //KOPF/Head
   glTranslatef(0.0, 0.5, 0.0);
   glColor3f(0.8, 0.5, 0.2);
   Wuerfel(0.3);
   glPopMatrix();

   //KÖRPER/Body
   glPushMatrix();
   glScalef(1.3, 2., 2.);
   glTranslatef(0.0, -0.25, 0.0);
   Wuerfel(0.4);
   glPopMatrix();

   //LINKES BEIN /left Leg
   glPushMatrix();
   glScalef(0.5, 2., 0.3);
   glTranslatef(-0.4, -0.6, 0.0);
   Wuerfel(0.2);
   glPopMatrix();

   //RECHTES BEIN/right leg
   glPushMatrix();
   glScalef(0.5, 2., 0.3);
   glTranslatef(0.4, -0.6, 0.0);
   Wuerfel(0.2);
   glPopMatrix();


   //LINKER OBERARM /left upper arm
   glPushMatrix();
   //glRotatef(fRotation, 0.1, 0.0, 0.0);
   glScalef(0.4, 1.4, 0.3);
   glTranslatef(-1.0, -0.25, 0.0);
   Wuerfel(0.3);
   glPopMatrix();

   //LINKER UNTERARM/ left under arm
   glPushMatrix();
   glScalef(0.4, 0.8, 0.3);
   glTranslatef(-1., -0.8, 0.0);
   Wuerfel(0.2);
   glPopMatrix();



   //RECHTER OBERARM/right upper arm
   glRotatef(fRHand += 0.2, 1., 0., 0.);
   glPushMatrix();
   glScalef(0.4, 1.4, 0.3);
   glTranslatef(1.0, -0.25, 0.0);
   Wuerfel(0.3);
   glPopMatrix();

   //RECHTER UNTERARM /right under arm
   glPushMatrix();
   glScalef(0.4, 0.8, 0.3);
   glTranslatef(1., -0.8, 0.0);
   glRotatef(fRUnterHand += 0.2, 0.5, 0., 0.);
   Wuerfel(0.2);
   glPopMatrix();

   
   

   

   

   glutSwapBuffers();
}


void Reshape(int width, int height)
{
   
   
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glViewport(0, 0, width, height);
   gluPerspective(45., 1, 0.1, 8.); // perspektivische Kamera
   glMatrixMode(GL_MODELVIEW);


}

void Animate(int value)
{


   glutPostRedisplay();
   int wait_msec = 10;
   glutTimerFunc(wait_msec, Animate, ++value);
}

int main(int argc, char **argv)
{
   glutInit(&argc, argv);                
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize(600, 600);         
   glutCreateWindow("Name_1; Name_2");  
   glutDisplayFunc(RenderScene);        
   glutReshapeFunc(Reshape); 
   glutTimerFunc(10, Animate, 0);
   Init();
   glutMainLoop();
   return 0;
}


For a hierarchy of transforms:

parent
   child1
   child2
      child2.1

the transformations need to be nested:

glPushMatrix();
    // apply coordinate transforms for parent
    // draw parent
    glPushMatrix();
        // apply coordinate transforms for child1 (relative to parent)
        // draw child1
    glPopMatrix()
    // back in coordinate system of parent
    glPushMatrix()
        // apply coordinate transforms for child2 (relative to parent)
        // draw child2
        glPushMatrix();
            // apply coordinate transforms for child2.1 (relative to child2)
            // draw child2.1
        glPopMatrix();
        // back in coordinate system of child2
    glPopMatrix();
    // back in coordinate system of parent
glPopMatrix();
// back in world space - or whatever was the current coordinate system before the first glPushMatrix() call above.
1 Like

Hey thanks for the advice. Now i think i fix the parents problem. But i still dont know how to implement the rotation for the “hand gesture” and “walking”. and i want that the rotation value should be can changed dynamicly in the Animate method
You have any advice or solution proposal ?

 #include <iostream> 
#include <GL/freeglut.h>         
#include "Wuerfel.h"
static float fRotSonne = 0., fRotErde = 0;
static float fX = 1, fY = 1, fZ = 1;



void Init()
{
	// Hier finden jene Aktionen statt, die zum Programmstart einmalig 
	// durchgef�hrt werden m�ssen	
	glClearColor(0, 0, 0, 1);
	glEnable(GL_DEPTH_TEST);
}

void RenderScene() 
{
	
	glLoadIdentity();   
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Kameraposition, Blickwinkel und Up-Vector
	gluLookAt(0,0., 3	 // Betrachterposition
					, 0., 0., 0.,//Blickziel
					0, 0.1, 0.); // Oben-Betrachter

	//KOPF / Head
	glPushMatrix();
	glTranslatef(0.0, 0.5, 0.0);
	glColor3f(0.8, 0.5, 0.2);
	Wuerfel(0.3);

	//KÖRPER / Body
	glPushMatrix();
	glScalef(1.3, 2., 2.);
	glTranslatef(0.0, -0.25, 0.0);
	Wuerfel(0.4);
	glPopMatrix();

	//LINKES BEIN / Left Leg
	glPushMatrix();
	glScalef(0.5, 2., 0.3);
	glTranslatef(-0.4, -0.35, 0.0);
	Wuerfel(0.2);
	glPopMatrix();

	//RECHTES BEIN / Right Leg
	glPushMatrix();
	glScalef(0.5, 2., 0.3);
	glTranslatef(0.4, -0.35, 0.0);
	Wuerfel(0.2);
	glPopMatrix();


	//LINKER OBERARM / Left Upperarm
	glPushMatrix();
	//glRotatef(fRotSonne += 0.2, 1., 0., 0.);;
	glScalef(0.4, 2.0, 0.3);
	glTranslatef(-1.0, 0.05, 0.0);
	Wuerfel(0.3);
	

	//LINKER UNTERARM / Left Underarm 
	glPushMatrix();
	glScalef(0.7, 0.5, 1.5);
	glTranslatef(0.05, -0.45, 0.15);
	Wuerfel(0.3);
	glPopMatrix();



	//RECHTER OBERARM / Right Upperarm
	glPushMatrix();
	//glRotatef(fRotSonne += 0.2, 1., 0., 0.);
	glScalef(0.4, 2, 0.3);
	glTranslatef(1.0, 0.05, 0.0);
	Wuerfel(0.3);
	

	//RECHTER UNTERARM //Right Underarm
	glPushMatrix();
	glScalef(0.7, 0.5, 1.5);
	glTranslatef(-0.05, -0.45, 0.15);
	//glRotatef(fRotSonne += 0.2, 0.5, 0., 0.);
	Wuerfel(0.3);
	glPopMatrix();

	
	

	

	

	glutSwapBuffers();
}


void Reshape(int width, int height)
{

	glMatrixMode(GL_PROJECTION);
	
	glLoadIdentity();

	glViewport(0, 0, width, height);
	gluPerspective(45., 1, 0.1, 8.); // perspektivische Kamera
	glMatrixMode(GL_MODELVIEW);


}

void Animate(int value)
{

	std::cout << "value=" << value << std::endl;
	// RenderScene aufrufen
	glutPostRedisplay();
	int wait_msec = 10;
	glutTimerFunc(wait_msec, Animate, ++value);
}

int main(int argc, char **argv)
{
	glutInit(&argc, argv);                
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(600, 600);         
	glutCreateWindow("Name_1; Name_2");   
	glutDisplayFunc(RenderScene);         
	glutReshapeFunc(Reshape);
	glutTimerFunc(10, Animate, 0);
	Init();
	glutMainLoop();
	return 0;
}
```

Introduce some variables that store the rotation angles of the parts and update those in your Animate function based on how much time has elapsed. Use the variables as input to glRotatef calls in RenderScene.

1 Like