PDA

View Full Version : problem in translation and rotation



prabhakaran
01-30-2008, 06:08 AM
hai friends

what i want means i want to move the helicopter to its front and back sides only using 'x' and 'X' keys respectivly. and i will rotate the helicopter where it was using 'r' and 'R' keys respectively.

this is my sample helicopter program i have doubt in translation and rotation. here i have used wx,wy wz coordinates for the helicopter.i want the helicopter move front and back only. here i have used x and 'X' for move front and back respectively. i have used 'r' and 'R' for rotaion in Positive y axis and negative y axis.
what is my problem is i will move the helicopter to 4 points to x axis. then i will rotate 90 degree in y axis. then i will move the helicopter to 4 points. remain it would go to x axis. but it will not going to its front face.




#include<gl/glut.h>



void drawcube();

GLUquadricObj *body,*back,*sidepipe;

int wx=0,wy=0,wz=0;

int fanangle=0;

int i=0;
float backpoint=0;

int angle=0;

void init()
{
glClearColor(0,0,0,0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
body=gluNewQuadric();
back=gluNewQuadric();
sidepipe=gluNewQuadric();

}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0,5,10,0,0,-2,0,1,0);

//body part
glPushMatrix();
glTranslatef(wx,wy,wz);
glRotatef(angle,0,1,0);
glTranslatef(-wx,-wy,-wz);
glPushMatrix();
glScalef(.5,.5,.5);
//glRotatef(-90,0,1,0);
//glRotatef(-30,0,0,1);
glColor3f(1,1,1);
glPushMatrix();
glTranslatef(wx-3,wy,wz);
glScalef(1,1,.7);
gluSphere(body,1.5,30,30);
glScalef(1,1,1.3);
glTranslatef(wx+0.6-wx,wy-wy,wz-wz);
glScalef(1,1,.7);
gluSphere(body,1.5,30,30);
glPopMatrix();



//first fan

//beam
glColor3f(0,0,1);
glPushMatrix();
glTranslatef(wx-2.5,wy+1.5,wz);
glRotatef(fanangle,0,1,0);
glScalef(.5,5,.5);
glutSolidCube(0.3);
//glutWireCube(.3);
glPopMatrix();

//fan

glColor3f(1,0,1);
glPushMatrix();
glTranslatef(wx-2.5,wy+2.1,wz);
glRotatef(fanangle,0,1,0);
glScalef(14,.5,.5);
glutSolidCube(.3);
//glutWireCube(.3);
glPopMatrix();

glPushMatrix();
glTranslatef(wx-2.5,wy+2.1,wz);
glRotatef(fanangle,0,1,0);
glScalef(.5,.5,14);
glutSolidCube(.3);
//glutWireCube(.3);
glPopMatrix();

//back
//back body one
glPushMatrix();
glTranslatef(wx-2,wy+0.4,wz);
glScalef(1,0.6,.8);
glRotatef(90,0,1,0);
gluCylinder(back,1.2,0.5,4,30,30);
glPopMatrix();

//back body two
glPushMatrix();
glTranslatef(wx-2,wy+0.4,wz);
glRotatef(90,0,1,0);
gluCylinder(back,1.0,0.5,4,30,30);

glPopMatrix();

//back body three
glPushMatrix();
glTranslatef(wx+2,wy+0.4,wz);
glRotatef(90,0,1,0);
gluCylinder(back,0.5,0.3,1,30,30);
glPopMatrix();

//back body four
glColor3f(0,0,1);
glPushMatrix();
glTranslatef(wx+3,wy+0.4,wz);
gluSphere(back,0.3,30,30);
glPopMatrix();

//back top

glPushMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();



fanangle=(fanangle+5)%360;
glutSwapBuffers();
glutPostRedisplay();
}
void reshape(int w,int h)
{
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(52.0f,w/h,1,1000);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key,int x,int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'x':
wx++;
break;
case 'X':
wx--;
break;
case 'z':
wz++;
break;
case 'Z':
wz--;
break;
case 'r':
angle=(angle+1)%360;
break;
case 'R':
angle=(angle-1)%360;
break;
}
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RG B);
glutInitWindowSize(1024,768);
glutInitWindowPosition(0,0);
glutCreateWindow("2d Circle");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

dletozeun
01-30-2008, 10:19 AM
Hi,

It is very difficult to understand what you want reading this post...
But looking at your code I see some very strange things. You talk about a helicopter which should be (In my opinion) just one object in the scene and I see many transformations for each helicopter part.
All the parts that are stuck together have to move together, so you should compute one transformation for all of these.

Finally remember that the transformations order, especially between translations and rotations is very important.
When you compute a translation for example, remember that this operation will affect all objects drawn after it. If you don't want to affect the next object you would then compute the inverse translation.

prabhakaran
01-30-2008, 10:29 PM
thank you dletozeun,
my problem is i have an helicopter that is looking towards x axis. i want the helicopter move along all directions using x key for forword and X key for backwrd . the helicopter moves only fornt and back direction of its. if i rotate the helicopter in 90 degree y axis means then the helicopter must go front direction of the it now that is z axis.

consider

gluCylinder(object,1,0,4,303,0);

this is my helicopter

give me the solution for it friend

Dark Photon
01-31-2008, 06:53 AM
<< thread re-direct >> (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=233516)