Jump to content


Help me!! (drawing a robot)


2 replies to this topic

#1 ~*CuTe GiRl*~

    New Member

  • Members
  • Pip
  • 1 posts

Posted 07 December 2006 - 03:41 PM

I am a total newbie to using Microsoft Visual C++ 6.0 and OpenGL, so go easy on me. My problem is I dont know how to set the angle of its legs and arms so that the robot move like this image

"Make the robot move forward and move the arms and legs appropriately. This should be achieved by pressing the key ‘F’. The robot should start at the left side of the window and continue walking even if it exits the window on the right. Hint: you need to increment the angles until a certain limit, then you need to decrease, so you need to have a way of knowing whether you should increment or decrement."

[url=http://www.flashfp.net/uploader/modules/up-pic/pic/uploads/bc968e8862.jpg][img]

here is my code
#include <windows.h>
#include <iostream>
#include <Gl.h>
#include <glut.h>
using namespace std;

#define WINDOW_HEIGHT	300
#define WINDOW_WIDTH	600

int wh = WINDOW_HEIGHT;
int ww = WINDOW_WIDTH;
int wl,wr,wb,wt; // window left, right,bottom and top
int angle=0,head_angle=0;
int tx=-300,ty=-150;


void myInit();
void myReshape(GLsizei W, GLsizei H);
void Axes();
void rectangle (float width, float height);
void myKeyboard(unsigned char theKey, int mouseX, int mouseY);
void myDisplay();

void myInit()
// Initialization. Includes setting up the window and viewport
{
	glClearColor(1,1,1,0);		// sets the background color
	glPointSize(4);
	glLineWidth(2);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	wl=-300; wr=300; wb=-300; wt=300;
	gluOrtho2D(wl,wr,wb,wt);
	

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

void myReshape(GLsizei W, GLsizei H)
// update ww and wh according to new screen window width and height
{
	ww=W;
	wh=H;
	myInit();
}

void Axes()
// Draws x-axis and y-axis to help us see where the origin is.
// Draws a frame around the viewport/window
{
	glBegin(GL_LINES);
		glColor3f(1,0,0); // red for x-axis
		glVertex2i(0,0);
		glVertex2i(100,0);
		glColor3f(0,0,1); // green for y-axis
		glVertex2i(0,0);
		glVertex2i(0,100);
	glEnd();
	glBegin(GL_LINE_LOOP);
		glColor3f(0,0,0); // black for window frame
		glVertex2i(wl,wb);
		glVertex2i(wr,wb);
		glVertex2i(wr,wt);
		glVertex2i(wl,wt);
	glEnd();
}
/*************************** Rectangle *********************/
// Draw a rectangle with its lower left corner at the origin 
// and it has the following properties:
// width  (input): length of rectangle along the x dimension
// height (input): length of rectangle along the y dimension
// r,g,b  (input): color of the rectangle
void rectangle (float width, float height)
{
	//glColor3f(r,g,;);
	glBegin(GL_POLYGON);
		glVertex2f(0,0);
		glVertex2f(width,0);
		glVertex2f(width,height);
		glVertex2f(0,height);
	glEnd();
}



/***************************************************************/
void myKeyboard(unsigned char theKey, int mouseX, int mouseY)
{
	//make the robot move forward starting from the left side of the window
	//and continue walking even if it exits the window on the right
	if (theKey == 'F' && angle <45)
	{
    	angle+=45;
		tx+=30;
	}
		if (theKey == 'F' && angle >-45)
	{
	      angle-=45;
		  tx+=30;
		}
	if (theKey == 'R')//reset the robot to its initial position
		              //on the left side of the window
	{tx=-300;
	angle=0;
	head_angle=0;
	}
	if (theKey == 'U')//make the robot look up at angle of 20
	head_angle=20;

	if (theKey == 'D')//make the robot look down at the angle of 20
	head_angle=-20;

	if (theKey == 'S')//make the robot look straight ahead(angle=0)
	head_angle=0;
	
	myDisplay();
}
//
void segment(int angle,int size [])
{
	glRotated(angle,0,0,1);
	glScalef(size[0],size[1],0);
	rectangle(4,8);
}

void myRobot(int angle)
{
	int size[2]={6,15};
	glTranslated(tx,ty,0);

	//legs
	glPushMatrix();
	glTranslated(5,0,0);
	glColor3f(0,1.0,0);
	segment(angle,size);
	glPopMatrix();

	glPushMatrix();
	glTranslated(5,0,0);
	glColor3f(1.0,0,0);
	segment(-angle,size);
	glPopMatrix();

	//feet
	glPushMatrix();
	size[0]=6; size[1]=3;
	glTranslated(5,0,0);
	glColor3f(1,1,0);
	segment(angle,size);
	glPopMatrix();

	glPushMatrix();
	glTranslated(5,0,0);
	glColor3f(1,1,0);
	segment(-angle,size);
	glPopMatrix();

	//body
	glPushMatrix();
	size[0]=10; size[1]=20;
	glTranslated(0,120,0);
	glColor3f(0,0,0);
	segment(0,size);
	glPopMatrix();

	//arms
	glPushMatrix();
	size[0]=4; size[1]=10;
	glTranslated(10,190,0);
	glColor3f(0,1.0,0);
	segment(-angle,size);
	glPopMatrix();

	glPushMatrix();
	glTranslated(10,190,0);
	glColor3f(1.0,0,0);
	segment(angle,size);
	glPopMatrix();

	//hands
	glPushMatrix();
	size[0]=4; size[1]=2;
	glTranslated(10,190,0);
	glColor3f(1.0,1.0,0);
	segment(-angle,size);
	glPopMatrix();
	
		glPushMatrix();
	glTranslated(10,190,0);
	glColor3f(1.0,1.0,0);
	segment(angle,size);
	glPopMatrix();

	//head
	glPushMatrix();
	size[0]=6; size[1]=6;
	glTranslated(8,280,0);
	glColor3f(1.0,1.0,0);
	segment(head_angle,size);
	glPopMatrix();
	
	//eyes
	glPushMatrix();
	size[0]=3; size[1]=1;
	glTranslated(20,305,0);
	glColor3f(1.0,1.0,1.0);
	segment(head_angle,size);
	glPopMatrix();
}
void myDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT);
	//glViewport(0,0,300,600);
	myInit();
	myRobot(angle);
	glFinish();
	glFlush();
}

int main(int argc, char* argv[])
{

	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(WINDOW_WIDTH,WINDOW_HEIGHT);
	glutInitWindowPosition(500,100);
	glutCreateWindow("Sheet 5");
	glutDisplayFunc(myDisplay);
	glutReshapeFunc(myReshape);
	glutKeyboardFunc(myKeyboard);
	myInit();
	glutMainLoop();

	return 1;
}


#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 07 December 2006 - 04:58 PM

If this is a homework problem, you should ask your teacher for help. We are not here to help with homework problems.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 geon

    Senior Member

  • Members
  • PipPipPipPip
  • 812 posts

Posted 08 December 2006 - 01:26 PM

And your image link does not work.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users