PDA

View Full Version : Sphere moves out of sight !!



Ikhan
12-02-2011, 03:13 AM
Please reply soon !!
As i am still newbie to OpenGL, finding it little difficult in 3D movement & rotation !!

Here are the movement functions !!



void moveSphere(float fraction){
float dirX,dirZ;
dirX=atX-eyeX;
dirZ=atZ-eyeZ;
eyeX += dirX * fraction;
eyeZ += dirZ * fraction;
atX += dirX * fraction;
atZ += dirZ * fraction;
sphereX += dirX * fraction;
sphereZ += dirZ * fraction;
}

void strafeSphere(float fraction){
float orthoX,orthoZ;
orthoX = -(atZ-eyeZ);
orthoZ = atX-eyeX;
eyeX += orthoX * fraction;
eyeZ += orthoZ * fraction;
atX += orthoX * fraction;
atZ += orthoZ * fraction;
sphereX += orthoX * fraction;
sphereZ += orthoZ * fraction;
}

void rotateView(float fraction){
float dirX,dirZ;
dirX = eyeX - atX;
dirZ = eyeZ - atZ;
eyeZ = atZ + sin(fraction) * dirX + cos(fraction) * dirZ;
eyeX = atX + cos(fraction) * dirX - sin(fraction) * dirZ;
dirX = atX - eyeX;
dirZ = atZ - eyeZ;
sphereZ = eyeZ + sin(fraction) * dirX + cos(fraction) * dirZ;
sphereX = eyeX + cos(fraction) * dirX - sin(fraction) * dirZ;
}

void pitchSphere(){
int i=0,j=0;
while(i<6){
sphereY += 0.2;
atY += 0.2;
eyeY += 0.2;
i++;
if(keyBuffer[GLUT_KEY_UP])
moveSphere(0.01);
if(keyBuffer[GLUT_KEY_DOWN])
moveSphere(-0.01);
renderScene();
causeDelay();
}
while(j<3){
j++;
if(keyBuffer[GLUT_KEY_UP])
moveSphere(0.01);
if(keyBuffer[GLUT_KEY_DOWN])
moveSphere(-0.01);
renderScene();
causeDelay();
}
while(i>0){
causeDelay();
sphereY -= 0.2;
atY -= 0.2;
eyeY -= 0.2;
i--;
if(keyBuffer[GLUT_KEY_UP])
moveSphere(0.1);
if(keyBuffer[GLUT_KEY_DOWN])
moveSphere(-0.1);
renderScene();
}
}


And here is my sphere rendering code !!



void renderSphere(){
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();
glTranslatef(sphereX,sphereY,sphereZ);
glRotatef(theta[0],1.0,0.0,0.0);
glRotatef(theta[1],0.0,1.0,0.0);
glRotatef(theta[2],0.0,0.0,1.0);
glColor3f(0.5,0.5,1.0);
if(wired)
glutWireSphere(radius,size<3?3:size,size<3?3:size);
else
glutSolidSphere(radius,size<3?3:size,size<3?3:size);
glPopMatrix();
glPopAttrib();
}


And finally display function !!



// Initial camera parameters
float eyeX=0.0,eyeY=0.5,eyeZ=4.0; // Camera eye
float atX=0.0,atY=0.5,atZ=0.0; // Center of projection : Look at
float sphereX=0.0,sphereY=-0.8,sphereZ=-1.0;
void renderGame(){
processKeys();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( eyeX,eyeY,eyeZ,
atX,atY,atZ,
0.0,1.0,0.0);
renderSkybox();
renderBase();
renderSphere();
renderTime();
}


Problem: When i rotate, sphere jerks !! which is not clean !!

Any Solution ?? And is there any efficient way to jump ??