Open GL question
Can anybody tell me what is wrong with the following code?
if
(sphere_posx>=320){
gl.glTranslatef(sphere_posx,sphere_posy,sphere_posz);
drawable.getGLU().gluSphere(cylinder_obj,20,20,20);
sphere_posx-=0.4;
}
else
{ gl.glTranslatef(sphere_posx,sphere_posy,sphere_posz);drawable.getGLU().gluSphere(cylinder_obj,20,20,20);
sphere_posx+=0.4;
}
I have a sphere which is moving along tthe positive side of the x axis but I want it to turn back when it reaches 320.
At the moment it reaches 320 and just stops. I would appreciate any help.
Thank you
[638 byte] By [
fonzia] at [2007-10-2 9:37:50]

Well, assuming sphere_posx starts > 320, it will decrease to 320(drawing the sphere all the way) then to say, 319.6. Then next frame it will go to the else clause and go up to 320, then the next frame sphere_posx will be >=320 and it will go back down to 319.6 and repeat forever
Thanks, it's OK, someone did it for me:
gl.glTranslatef(sphere_posx,sphere_posy,sphere_posz);
drawable.getGLU().gluSphere(cylinder_obj,20,20,20);
if(sphere_posx>300|| back){
sphere_posx-=1.0;
back = true;
if(sphere_posx<-300)
back = false;
}
else{
sphere_posx+=1.0;
}
'back' is a boolean variable set to false at the start
fonzia at 2007-7-16 23:43:58 >
