diff options
Diffstat (limited to 'in_practice/breakout/ball.c')
-rw-r--r-- | in_practice/breakout/ball.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/in_practice/breakout/ball.c b/in_practice/breakout/ball.c index 9aa42a2..1ec502e 100644 --- a/in_practice/breakout/ball.c +++ b/in_practice/breakout/ball.c @@ -1,6 +1,6 @@ #include "ball.h" -void move_ball(ball_t *ball, f32 dt, i32 width) +void move_ball(struct ball *ball, f32 dt, i32 width) { if (ball->stuck) return; @@ -18,7 +18,18 @@ void move_ball(ball_t *ball, f32 dt, i32 width) ball->o.pos.y = 0.0f; } } - -void reset_ball(ball_t *ball, v2 pos, v2 vel) + +struct collision check_ball_collision(struct ball ball, struct object obj) { + v2 ball_center = add_v2(ball.o.pos, v2a(ball.radius)); + v2 obj_half_size = scale_v2(obj.size, 0.5f); + v2 obj_center = add_v2(obj.pos, obj_half_size); + v2 diff = sub_v2(ball_center, obj_center); + v2 clamped = clamp_v2(diff, scale_v2(obj_half_size, -1.0f), obj_half_size); + v2 closest = add_v2(obj_center, clamped); + diff = sub_v2(closest, ball_center); + struct collision collision = {0}; + if (length_v2(diff) <= ball.radius) + collision = (struct collision){1, direction_v2(diff), diff}; + return collision; } |