GLUT - how to smoothly translate a ball? -
i'm making ball in field may moves right or left based on keyboard input. here's full code:
#include <windows.h> //for sleep(...) functionality #include <stdlib.h> #include <stdio.h> #include <gl/glut.h> glfloat angle = 0.0; static int mywindow; void keyboard_handler(unsigned char key, int x, int y); float ball_posx = 0.0; void ball(void) { glcolor4f(1.0f,0.0f,0.0f,0.6f); //set ball colour gltranslatef(ball_posx, 0.0, 2); //moving toward screen bit on creation glutsolidsphere(0.25,100,100); //this damn ball } void floor(void) { //floors flat surface facing down support logic ball slide glbegin(gl_lines); glcolor3f(1.0, 1.0, 1.0); (glfloat = -2.5; <= 2.5; += 0.25) { glvertex3f(i, -0.25, 2.5); glvertex3f(i, -0.25, -2.5); glvertex3f(2.5, -0.25, i); glvertex3f(-2.5, -0.25, i); } glend(); } void init(void) { glenable(gl_depth_test); glenable(gl_lighting); glenable(gl_light0); } float cam_posx = 0.0; float cam_posy = 0.5; float cam_posz = 5.0; void display(void) { glclearcolor(1.0,1.0,1.0,1.0); //to add background color (white) glclear(gl_color_buffer_bit | gl_depth_buffer_bit); //clear buffer colour , depth glloadidentity(); glulookat(cam_posx, cam_posy, cam_posz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //camera! (cam position x, cam position y, cam position z, cam target x, cam target y, cam target z, position x, position y, position z) floor(); ball(); glutswapbuffers(); angle += 0.05; //to affect glrotate function } void reshape(int w, int h) { glviewport(0, 0, (glsizei)w, (glsizei)h); glmatrixmode(gl_projection); glloadidentity(); gluperspective(60, (glfloat)w / (glfloat)h, 1.0, 100.0); glmatrixmode(gl_modelview); } int main(int argc, char **argv) { glutinit(&argc, argv); glutinitdisplaymode(glut_double | glut_rgba | glut_depth); // set display buffer glutinitwindowsize(500, 500); //window's size glutinitwindowposition(100, 100); //window's position mywindow = glutcreatewindow("hendra ganteng, read controls.txt, insist!"); //window's title init(); glutdisplayfunc(display); glutkeyboardfunc(keyboard_handler); glutidlefunc(display); glutreshapefunc(reshape); glutmainloop(); return 0; } void keyboard_handler(unsigned char key, int x, int y) { switch(key) { //camera case 'a': if(cam_posx > -3) { cam_posx = cam_posx - 1; } break; case 'd': if(cam_posx < 3) { cam_posx = cam_posx + 1; } break; case 's': if(cam_posy > 0.5) { cam_posy = cam_posy - 1; } break; case 'w': if(cam_posy < 3) { cam_posy = cam_posy + 1; } break; case 'x': if(cam_posz == 5.0) { cam_posz = 4.0; } else cam_posz = 5.0; break; //ball case ',': if(ball_posx > -2) { for(int i=0; i<5; i++) { sleep(200); ball_posx = ball_posx - 0.2; } } break; case '.': if(ball_posx < 2) { ball_posx = ball_posx + 1; } break; } }
however, ball won't move smoothly if change translation. tried "sleep" method when moving left, instead of moving every 0,2 second until reach destination, waits 1 second translate without delay. how make translation smooth?
try this: make 1 more function glutidlefunc in main. let's call function idle(). in idle move last line of display function.
angle += 0.05; //to affect glrotate function also there no glrotate call in code. try
void display(void) { glclearcolor(1.0,1.0,1.0,1.0); //to add background color (white) glclear(gl_color_buffer_bit | gl_depth_buffer_bit); //clear buffer colour , depth glloadidentity(); glrotate(angle, 0.0, 1.0, 0.0);//for horizontal rotation glulookat(cam_posx, cam_posy, cam_posz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //camera! (cam position x, cam position y, cam position z, cam target x, cam target y, cam target z, position x, position y, position z) floor(); ball(); glutswapbuffers(); } your new idle() function variable computations.
Comments
Post a Comment