c - OpenGL - Rotating a 2D Asteroids Ship -


so i've been googling out wazoo , going through sorts of resources - texts, lectures, etc. - i'm either missing or not grasping correctly. class in opengl need design asteroids game, seems simple enough concept, i'm finding difficult movement of ship down right. know need translate our ship origin, perform rotation, , translate appropriate position - problem i'm facing understanding execution of it. code have drawing ship far:

void drawship(ship *s) {     glclear(gl_color_buffer_bit);       glmatrixmode(gl_modelview);     glloadidentity();     glpushmatrix();     mytranslate2d(s->x, s->y);     float x = s->x, y = s->y;     myrotate2d(s->phi);     glbegin(gl_line_loop);     glvertex2f(s->x + s->dx, s->y + s->dy + 3);     glvertex2f(s->x + s->dx-2, s->y + s->dy - 3);     glvertex2f(s->x + s->dx, s->y + s->dy -1.5);     glvertex2f(s->x + s->dx + 2, s->y + s->dy - 3);     glend();     mytranslate2d(s->x - x, s->y - y);     glpopmatrix(); } 

with rotation , motion determined following variables, correctly modified in keyboard function:

if (left == 1)     {         ship.phi -= 0.3;     }     if (right == 1)     {         ship.phi += 0.3;     }     if (up == 1)     {         ship.dx += -2.0*sin(ship.phi);         ship.dy += 2.0*cos(ship.phi);     }     if (down == 1)     {         ship.dx += 2.0*sin(ship.phi);         ship.dy += -2.0*cos(ship.phi);     }     drawship(&ship); 

once rotation down, i'm confident can rest of done. is, above code allow object either rotate on origin or around it, not rotate in place unless directly on origin. ship has been initialized points of (0,0), (25, 25), , (50, 50), , purposes origin of screen (0,0) bottom left corner.

can me understand why ship not rotate in place, , origin? expect has how i'm performing translations , rotation, i'm @ loss what.

your draw function seems wrong. ship design shouldn't depend on it's position. means coordinates of vertices should same. use gltranslate , rotate right position. translate after draw , before pop matrix. doesn't because matrix changes apply vertices defined after matrix change.

the structure should this:

// make sure origin in 0, 0  of space. glpushmatrix();  // save setup gltranslate(ship.x, ship.y);  // location of ship. glrotate(ship.phi);  // or whatever name of angle of ship.  drawship();  // draws sprite/wireframe/object want. glpopmatrix();  // return original setup. 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -