Like Tree1Likes
  • 1 Post By Bobby230

Opengl walking leg animation

This is a discussion on Opengl walking leg animation within the C Programming forums, part of the General Programming Boards category; Hey Peeps I have a problem with designing a walking leg animation using opengl (in C language). Im fairly new ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Opengl walking leg animation

    Hey Peeps

    I have a problem with designing a walking leg animation using opengl (in C language). Im fairly new to programming and this is my first animation. I've managed to get so far (not sure if its correct though), but im having trouble working out how to actually get the legs walking in a forward direction with the correct leg motion.

    I know I have to use the idle function at some point and probably some 'If' statements. I just can't seem to get it right though.

    Any help you could offer would be highly appreciated. The code so far is below.

    Cheers

    Code:
    #include "stdafx.h"
    
    #include <stdlib.h>
    #include <GL/glut.h>
    #include <math.h>
    #include <stdio.h>
    
    //GLOBALS for communication
    static float G_theta[3];
    static float G_body_rot=0, G_up_leg_angle=0, G_low_leg_angle=-45.0, G_footr_angle=-45.0;
    static float G_zoom=0.3;
    
    //PROTOTYPES
    void draw_scene(float body_rot, float up_leg_angle, float low_leg_angle, float footr_angle);
    void view_control();
    void draw_floor();
    void draw_body(float up_leg_angle, float low_leg_angle, float footr_angle);
    void draw_leg(float low_leg_angle, float footr_angle,float length_hip, float length_upper_leg, float length_lower_leg, float length_foot);
    void draw_below_knee(float low_leg_ang, float length_upper_leg, float length_lower_leg, float length_foot);
    void draw_foot(float length_foot);
    void MyIdle(void)
    
    
    //=============================================
    // DRAW CUBOID ROUTINES
    //=============================================
    //Cube points and colours
    
    	float vertices[][3] = 
    	{
    		{-0.5,-0.5,-0.5},{0.5,-0.5,-0.5},
    		{0.5,0.5,-0.5}, {-0.5,0.5,-0.5}, {-0.5,-0.5,0.5}, 
    		{0.5,-0.5,0.5}, {0.5,0.5,0.5}, {-0.5,0.5,0.5}
    	};
    
    	float colors[][3] = {{0.0,0.5,0.5},{1.0,0.0,0.0},
    	{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, 
    	{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};
    
    
    void polygon(int a, int b, int c , int d)
    {
    	// draw a polygon using colour of first vertex
    
     	glBegin(GL_POLYGON);
    		glColor3fv(colors[a]);
    		glVertex3fv(vertices[a]);
    		glVertex3fv(vertices[b]);
    		glVertex3fv(vertices[c]);
    		glVertex3fv(vertices[d]);
    	glEnd();
    }
    
    void cube(void)
    {
    	//Draw unit cube centred on the origin
    
    /* map vertices to faces */
    
    	polygon(0,3,2,1);
    	polygon(2,3,7,6);
    	polygon(4,7,3,0);
    	polygon(1,2,6,5);
    	polygon(7,4,5,6);
    	polygon(5,4,0,1);
    }
    
    void draw_cuboid(float x, float y, float z)
    {
    	glPushMatrix();
    		//size cuboid
    		glScalef(x,y,z);
    		// move base up so that bottom face is at origin
    		glTranslatef(0,0.5,0.0); 
    		cube();
    	glPopMatrix();
    }	
    
    
    //=============================================
    // KEYBOARD CALLBACK ROUTINE 
    //=============================================
    void keyboard(unsigned char key, int x, int y)
    {
    	printf("Keyboard call back: key=%c, x=%d, y=%d\n", key, x, y);
    	switch(key)
    	{
                   //walk legs
                   case '1':
                        G_
    
    //=============================================
    //Draw_Scene
    //=============================================
    
    void draw_scene (float body_rot, float up_leg_angle, float low_leg_angle, float foot_angle);
    {
         glPushMatrix();
                      draw_floor();
                      //rotate complete body about y-axis
                      glRotatef(body_rot, 0.0,1.0,00);
                      
                      //Draw body
                      draw_body (float up_leg_angle, float low_leg_angle, float footr_angle);
         glPopMatrix();
    }
    
    //=============================================
    //Draw_Floor
    //=============================================
    
    void draw_floor()
    {
    	glPushMatrix();
    		glBegin(GL_POLYGON);
    			glColor3f(.75,.75,.75);
    			glVertex3f(-10,0,10);
    			glVertex3f(-10,0,-10);
    			glVertex3f(10,0,-10);
    			glVertex3f(10,0,10);
    		glEnd();
    	glPopMatrix();
    }
    //=============================================//Draw_Body
    //=============================================
    
    void draw_body(float up_leg_angle, float low_leg_angle, float footr_angle);
    {
         float length_upper_leg=5.5 //length of top part of leg
         float length_lower_leg=5.5 //length of bottom part of leg
         float length_foot=1 //length of foot
         float length_hip=1.5//length of hip
         
         glPushMatrix();
                      draw_hip();
                         //draw box for Hips
                         glPushMatrix();
                                        draw_cuboid(2.0,length_hip,3.5);
                                        //Change frame of reference to the base of the box
                                        glTranslatef(0.0,length_hip/2,-10)
                         glPopMatrix();                              
    
                      //rotate upper leg around z-axis
                      glRotatef(up_leg_angle, 0.0,0.0,1.0);
                      
                      //Draw leg
                      void draw_leg(float low_leg_angle, float footr_angle,float length_hip, float length_up_leg, float length_low_leg, float length_footr);
         glPopMatrix();
    }
    
                   
    //=============================================
    //Leg_Displacement
    //=============================================
    
       
                   
    //=============================================
    //Draw_Leg
    //=============================================
    
    void draw_leg(float low_leg_angle, float footr_angle,float length_hip, float length_upper_leg, float length_lower_leg, float length_foot);
    {
         glPushMatrix();
                      draw_upper_leg();
                          glPushMatrix();
                                         draw_cuboid(2.0,length_upper_leg,1.5);
                                          //change frame of reference to the end of the 2nd joint/base of upper leg
                                         glTranslatef(0.0,length_upper_leg,0);
                          glPopMatrix();
                        
                      //rotate lower leg around z-axis
                      glRotatef(low_leg_angle, 0.0,00,1.0);
                      
                      //Draw below the knee
                      void draw_below_knee(float footr_angle,float length_leg, float length_);
         glPopMatrix();
    }
    
    //=============================================
    //Draw_Below_Knee
    //=============================================
    
    void draw_below_knee(float footr_angle, float length_lower_leg, float length_foot);
    {
         glPushMatrix();
                        draw_lower_leg();
                             glPushMatrix();
                                            draw_cuboid(2.0,length_lower_leg,1.5);
                                            //change frame of reference to the end of the 3rd joint/base of lower leg
                                            glTranslatef(0.0,length_lower_leg,0);
                             glPopMatrix();
                        //rotate foot around z-axis
                        glRotatef(footr_angle,0.0,0.0,1.0);
                        
                        draw_foot();
         glPopMatrix();
    }
    
    //=============================================
    //Draw_Foot
    //=============================================
         
    void draw_foot(float length_foot);
    {
         glPushMatrix();
                        draw_cuboid(3.5,length_foot,1.5);
         glPopMatrix();
               
    //=============================================
    //Idle_Function
    //=============================================
                                  
    void MyIdle(void)
    {
    /* Some code to modify the variables defining next frame */
    ....
    };
    kourosh likes this.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    //=============================================
    // KEYBOARD CALLBACK ROUTINE 
    //=============================================
    void keyboard(unsigned char key, int x, int y)
    {
    	printf("Keyboard call back: key=%c, x=%d, y=%d\n", key, x, y);
    	switch(key)
    	{
                   //walk legs
                   case '1':
                        G_
    
    //=============================================
    //Draw_Scene
    //=============================================
    That isn't your whole function, right? Because if it is, you're missing some closing braces etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    No, thats not my full code for that function, I said I was new to C programming, but I'm not that new. I just did'nt know what to relate the call back function to, or how to relate it.

    I'm seriously stuck, just any advice would be great. Getting the legs to move forward in a walking motion is difficult because I have three joints that need to be moved in relation to oneanother; upper_leg_joint, lower_leg_joint and foot joint.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Hard-coding any walking is going to look strange. Most games rotoscope real actors, grab the vertices and create models from them, and then use keyframes along with linear interpolation and/or vertex interpolation/tweening to animate the object.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  2. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 05:48 AM
  3. OpenGL: glutIdleFunc and animation
    By gazsux in forum Game Programming
    Replies: 1
    Last Post: 03-21-2003, 06:24 AM
  4. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 06:57 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21