Implement asteroid bouncing
This commit is contained in:
23
game.c
23
game.c
@@ -261,6 +261,24 @@ static unsigned check_collisions(collision_t *out)
|
||||
return count;
|
||||
}
|
||||
|
||||
static void bounce_asteroids(entity_t *a, entity_t *b)
|
||||
{
|
||||
const float ma = a->radius * a->radius;
|
||||
const float mb = b->radius * b->radius;
|
||||
const float m = ma + mb;
|
||||
|
||||
const vec2_t n = vec2_norm(vec2_sub(b->pos, a->pos));
|
||||
|
||||
const float va1 = vec2_dot(a->vel, n);
|
||||
const float vb1 = vec2_dot(b->vel, n);
|
||||
|
||||
const float va2 = (va1 * (ma - mb) + 2 * mb * vb1) / m;
|
||||
const float vb2 = (vb1 * (mb - ma) + 2 * ma * va1) / m;
|
||||
|
||||
a->vel = vec2_add(a->vel, vec2_scale(n, va2 - va1));
|
||||
b->vel = vec2_add(b->vel, vec2_scale(n, vb2 - vb1));
|
||||
}
|
||||
|
||||
static void handle_collisions(const collision_t *collisions, unsigned count)
|
||||
{
|
||||
for (unsigned i = 0; i < count; ++i) {
|
||||
@@ -276,7 +294,10 @@ static void handle_collisions(const collision_t *collisions, unsigned count)
|
||||
if (a->tag == COLLISION_SHOT && b->tag == COLLISION_SHOT)
|
||||
continue;
|
||||
|
||||
if (a->tag == COLLISION_ASTEROID && b->tag == COLLISION_ASTEROID) { }
|
||||
if (a->tag == COLLISION_ASTEROID && b->tag == COLLISION_ASTEROID) {
|
||||
bounce_asteroids(a, b);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (a->tag == COLLISION_SHOT) {
|
||||
a->dead = true;
|
||||
|
||||
Reference in New Issue
Block a user