Changeset 237ec28 in opengl-game for pong.cpp


Ignore:
Timestamp:
Aug 10, 2017, 1:46:16 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
9d22ee9
Parents:
4046b51
Message:

Finish writing the logic to create circles, and make the ball bounce of the paddle and the back wall

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pong.cpp

    r4046b51 r237ec28  
    8787   // glFrontFace(GL_CW);
    8888
    89    unsigned int numBallPoints = 3;
    90 
    91    GLfloat* points = createCirclePoints(numBallPoints, 0.0f, 0.0f, 0.2f);
     89   unsigned int numBallPoints = 36;
     90
     91   GLfloat ballRadius = 0.2f;
     92   GLfloat paddleThickness = 0.1f;
     93
     94   GLfloat* points = createCirclePoints(numBallPoints, 0.0f, 0.0f, ballRadius);
    9295   GLfloat* colors = createCircleColors(numBallPoints);
    9396
     
    117120   };
    118121
    119    GLuint ball_points_vbo = createDataBuffer(numBallPoints*3*sizeof(GLfloat), points);
    120    GLuint ball_colors_vbo = createDataBuffer(numBallPoints*3*sizeof(GLfloat), colors);
     122   GLuint ball_points_vbo = createDataBuffer(numBallPoints*9*sizeof(GLfloat), points);
     123   GLuint ball_colors_vbo = createDataBuffer(numBallPoints*9*sizeof(GLfloat), colors);
    121124   GLuint ball_vao = createArrayBuffer(ball_points_vbo, ball_colors_vbo);
    122125
     
    147150      previous_seconds = current_seconds;
    148151
    149       if (fabs(last_position) > 1.0f) {
     152      if (last_position > 1.0f-ballRadius) {
    150153         speed = -speed;
    151154      }
     155
     156      if (last_position < -1.0f+ballRadius+paddleThickness &&
     157         fabs(last_paddle_pos) < 0.15f) {
     158         speed = -speed;
     159      }
     160
     161      // if the ball hits the paddle on a corner, I should make it bounce
     162      // off at an angle
    152163
    153164      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    163174      glDrawArrays(GL_TRIANGLES, 0, sizeof(points_paddle)/sizeof(GLfloat)/3);
    164175
    165       // model[12] = last_position + speed*elapsed_seconds;
    166       model[12] = 0.0f;
     176      model[12] = last_position + speed*elapsed_seconds;
    167177      last_position = model[12];
    168178      model[13] = 0.0f;
     
    173183      glEnableVertexAttribArray(1);
    174184
    175       glDrawArrays(GL_TRIANGLES, 0, numBallPoints);
     185      glDrawArrays(GL_TRIANGLES, 0, numBallPoints*3);
    176186
    177187      glfwPollEvents();
     
    250260
    251261GLfloat* createCirclePoints(unsigned int smoothness, GLfloat centerX, GLfloat centerY, GLfloat radius) {
    252    GLfloat* points = new GLfloat[smoothness*3];
    253 
    254    points[0] = 0.0f;
    255    points[1] = 0.5f;
    256    points[2] = 0.5f;
    257    points[3] = -0.5f;
    258    points[4] = -0.5f;
    259    points[5] = 0.5f;
    260    points[6] = 0.5f;
    261    points[7] = -0.5f;
    262    points[8] = 0.5f;
    263 
    264    cout << "Sphere center: (" << centerX << ", " << centerY << ")" << endl;
    265 
    266    smoothness *= 2;
     262   GLfloat* points = new GLfloat[smoothness*9];
     263
     264   GLfloat curX, curY, nextX, nextY;
     265   curX = centerX;
     266   curY = centerY+radius;
     267
    267268   for (unsigned int i=0; i<smoothness; i++) {
    268       double radians = PI/2 + 2*PI * i/smoothness;
    269       cout << radians << endl;;     
    270       GLfloat x = centerX + cos(radians)*radius;
    271       GLfloat y = centerY + sin(radians)*radius;
    272       cout << "point " << i << ": (" << x << ", " << y << ")" << endl;
     269      double radians = PI/2 + 2*PI * (i+1)/smoothness;
     270      nextX = centerX + cos(radians)*radius;
     271      nextY = centerY + sin(radians)*radius;
     272
     273      points[i*9] = curX;
     274      points[i*9+1] = curY;
     275      points[i*9+2] = 0.5f;
     276      points[i*9+3] = nextX;
     277      points[i*9+4] = nextY;
     278      points[i*9+5] = 0.5f;
     279      points[i*9+6] = centerX;
     280      points[i*9+7] = centerY;
     281      points[i*9+8] = 0.5f;
     282
     283      curX = nextX;
     284      curY = nextY;
    273285   }
    274286
     
    277289
    278290GLfloat* createCircleColors(unsigned int smoothness) {
    279    GLfloat* colors = new GLfloat[smoothness*3];
    280 
    281    for (unsigned int i=0; i<smoothness; i++) {
     291   GLfloat* colors = new GLfloat[smoothness*9];
     292
     293   for (unsigned int i=0; i<smoothness*3; i++) {
    282294      colors[i*3] = 0.0f;
    283295      colors[i*3+1] = 1.0f;
    284296      colors[i*3+2] = 0.0f;
    285297   }
    286    colors[smoothness*3-3] = 0.0f;
    287    colors[smoothness*3-2] = 0.0f;
    288    colors[smoothness*3-1] = 1.0f;
    289298
    290299   return colors;
Note: See TracChangeset for help on using the changeset viewer.