Changeset 9d22ee9 in opengl-game
- Timestamp:
- Aug 11, 2017, 2:33:45 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 7ee66ea
- Parents:
- 237ec28
- git-author:
- Dmitry Portnoy <dmp1488@…> (08/11/17 02:33:39)
- git-committer:
- Dmitry Portnoy <dmp1488@…> (08/11/17 02:33:45)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pong.cpp
r237ec28 r9d22ee9 140 140 GLint location = glGetUniformLocation(shader_program, "model"); 141 141 142 float speed = 1.0f; 143 float last_position = 0.0f; 142 float speedX = 1.0f; 143 float speedY = 0.0f; 144 float last_position_x = 0.0f; 145 float last_position_y = 0.0f; 144 146 float last_paddle_pos = 0.0f; 145 147 … … 150 152 previous_seconds = current_seconds; 151 153 152 if (last_position > 1.0f-ballRadius) { 153 speed = -speed; 154 } 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 154 if (last_position_x > 1.0f-ballRadius) { 155 speedX = -speedX; 156 } 157 if (last_position_y+ballRadius>1.0f || last_position_y-ballRadius<-1.0f) { 158 speedY = -speedY; 159 } 160 161 if (last_position_x < -1.0f+ballRadius+paddleThickness && 162 last_position_x > -1.0f) { 163 if (fabs(last_paddle_pos-last_position_y) < 0.15f) { 164 speedX = -speedX; 165 } else { 166 // if the ball hits the paddle on a corner, make it bounce off 167 // at an angle 168 float horDist = fabs(paddleThickness-1.0f-last_position_x); 169 float verDist1 = fabs(last_paddle_pos+0.15f-last_position_y); 170 float verDist2 = fabs(last_paddle_pos-0.15f-last_position_y); 171 172 float dist1 = pow(horDist, 2)+pow(verDist1, 2); 173 float dist2 = pow(horDist, 2)+pow(verDist2, 2); 174 175 if (dist1 < ballRadius) { 176 if (speedX == -1.0f || speedY == 0.7f) { 177 speedX = 0.7f; 178 speedY = 0.7f; 179 } else { 180 speedX = 1.0f; 181 speedY = 0.0f; 182 } 183 } else if (dist2 < ballRadius) { 184 if (speedX == -1.0f || speedY == -0.7f) { 185 speedX = 0.7f; 186 speedY = -0.7f; 187 } else { 188 speedX = 1.0f; 189 speedY = 0.0f; 190 } 191 } 192 } 193 } 163 194 164 195 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); … … 174 205 glDrawArrays(GL_TRIANGLES, 0, sizeof(points_paddle)/sizeof(GLfloat)/3); 175 206 176 model[12] = last_position + speed*elapsed_seconds; 177 last_position = model[12]; 178 model[13] = 0.0f; 207 model[12] = last_position_x + speedX*elapsed_seconds; 208 last_position_x = model[12]; 209 model[13] = last_position_y + speedY*elapsed_seconds; 210 last_position_y = model[13]; 179 211 glUniformMatrix4fv(location, 1, GL_FALSE, model); 180 212
Note:
See TracChangeset
for help on using the changeset viewer.