paddle collision

This commit is contained in:
Zack Buhman 2025-12-05 12:45:58 -06:00
parent 8913983ada
commit 617b12c9f9
2 changed files with 51 additions and 26 deletions

View File

@ -198,8 +198,8 @@ int main()
float sensitivity = 0.4f; float sensitivity = 0.4f;
paddle_dx = (right - left) * sensitivity; paddle_dx = (right - left) * sensitivity;
state.ball_dx = deadzone(axes[0]); //state.ball_dx = deadzone(axes[0]);
state.ball_dy = deadzone(axes[1]); //state.ball_dy = deadzone(axes[1]);
break; break;
} }
} }
@ -214,22 +214,6 @@ int main()
double time = glfwGetTime(); double time = glfwGetTime();
update(&state, time); update(&state, time);
if ((state.ball_x + state.ball_dx * 0.4) > 12.25f) {
state.ball_x = 12.25f;
state.ball_dx = -state.ball_dx;
} else if ((state.ball_x + state.ball_dx * 0.4) < -0.25f) {
state.ball_x = -0.25f;
state.ball_dx = -state.ball_dx;
}
if ((state.ball_y + state.ball_dy * 0.4) > 27.0f) {
state.ball_y = 27.0f;
state.ball_dy = -state.ball_dy;
} else if ((state.ball_y + state.ball_dy * 0.4) < 0.0f) {
state.ball_y = 0.0f;
state.ball_dy = -state.ball_dy;
}
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_GREATER); glDepthFunc(GL_GREATER);

View File

@ -36,8 +36,23 @@ void reset_level(struct game_state * state)
} }
} }
static inline void ball_collision_response(struct game_state * state,
struct collision_data& cd)
{
state->ball_x = cd.escape_position.x / 4.0f;
state->ball_y = -cd.escape_position.y / 2.0f;
vec3 vel = reflect(vec3(state->ball_dx, state->ball_dy, 0), cd.bounds_normal);
state->ball_dx = vel.x;
state->ball_dy = vel.y;
}
void update(struct game_state * state, double time) void update(struct game_state * state, double time)
{ {
vec3 ball_position = vec3(state->ball_x * 4.0f, -state->ball_y * 2.0f, 0.0);
//////////////////////////////////////////////////////////////////////
// block collision
//////////////////////////////////////////////////////////////////////
for (int y = 0; y < 28; y++) { for (int y = 0; y < 28; y++) {
for (int x = 0; x < 13; x++) { for (int x = 0; x < 13; x++) {
int block_ix = y * 13 + x; int block_ix = y * 13 + x;
@ -48,26 +63,52 @@ void update(struct game_state * state, double time)
continue; continue;
vec3 block_position = vec3(x * 4.0f, -y * 2.0f, 0.0f); vec3 block_position = vec3(x * 4.0f, -y * 2.0f, 0.0f);
vec3 ball_position = vec3(state->ball_x * 4.0f, -state->ball_y * 2.0f, 0.0);
// paddle 6.0 // paddle 6.0
// block 4.0 // block 4.0
// const vec3 paddle_bounds = vec3(3, 1, 0);
const vec3 block_bounds = vec3(2, 1, 0); const vec3 block_bounds = vec3(2, 1, 0);
struct collision_data cd; struct collision_data cd;
bool collided = aabb_circle_collision(block_position, ball_position, block_bounds, &cd); bool collided = aabb_circle_collision(block_position, ball_position, block_bounds, &cd);
if (collided) { if (collided) {
state->ball_x = cd.escape_position.x / 4.0f; ball_collision_response(state, cd);
state->ball_y = -cd.escape_position.y / 2.0f;
vec3 vel = reflect(vec3(state->ball_dx, state->ball_dy, 0), cd.bounds_normal);
state->ball_dx = vel.x;
state->ball_dy = vel.y;
state->blocks[block_ix].destroyed_time = time; state->blocks[block_ix].destroyed_time = time;
} }
} }
} }
vec3 paddle_position = vec3(state->paddle_x * 4.0f, -state->paddle_y * 2.0f, 0.0);
//////////////////////////////////////////////////////////////////////
// paddle collision
//////////////////////////////////////////////////////////////////////
{
const vec3 paddle_bounds = vec3(3, 1, 0);
struct collision_data cd;
bool collided = aabb_circle_collision(paddle_position, ball_position, paddle_bounds, &cd);
if (collided) {
ball_collision_response(state, cd);
}
}
//////////////////////////////////////////////////////////////////////
// arena collision
//////////////////////////////////////////////////////////////////////
if ((state->ball_x + state->ball_dx * 0.4) > 12.25f) {
state->ball_x = 12.25f;
state->ball_dx = -state->ball_dx;
} else if ((state->ball_x + state->ball_dx * 0.4) < -0.25f) {
state->ball_x = -0.25f;
state->ball_dx = -state->ball_dx;
}
if ((state->ball_y + state->ball_dy * 0.4) > 27.0f) {
//state->ball_y = 27.0f;
//state->ball_dy = -state->ball_dy;
} else if ((state->ball_y + state->ball_dy * 0.4) < 0.0f) {
state->ball_y = 0.0f;
state->ball_dy = -state->ball_dy;
}
state->ball_x += state->ball_dx; state->ball_x += state->ball_dx;
state->ball_y += state->ball_dy; state->ball_y += state->ball_dy;