Handle wrapping in physics_separation()
This commit is contained in:
29
physics.c
29
physics.c
@@ -4,6 +4,7 @@
|
|||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -97,16 +98,38 @@ void physics_escape(unsigned entity)
|
|||||||
escape_entity = entity;
|
escape_entity = entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void pos_views(vec2_t a, vec2_t b, vec2_t out[4])
|
||||||
|
{
|
||||||
|
const float dx = 2 * aspect;
|
||||||
|
const float dy = 2;
|
||||||
|
out[0] = b;
|
||||||
|
out[1] = (vec2_t) { b.x + copysign(dx, a.x), b.y };
|
||||||
|
out[2] = (vec2_t) { b.x, b.y + copysign(dy, a.y) };
|
||||||
|
out[3] = (vec2_t) { b.x + copysign(dx, a.x), b.y + copysign(dy, a.y) };
|
||||||
|
}
|
||||||
|
|
||||||
physics_sep_t physics_separation(unsigned a, unsigned b)
|
physics_sep_t physics_separation(unsigned a, unsigned b)
|
||||||
{
|
{
|
||||||
const physics_t *pa = physics_get(a);
|
const physics_t *pa = physics_get(a);
|
||||||
const physics_t *pb = physics_get(b);
|
const physics_t *pb = physics_get(b);
|
||||||
|
|
||||||
const vec2_t disp = vec2_sub(pb->pos, pa->pos);
|
vec2_t pos[4];
|
||||||
const vec2_t norm = vec2_norm(disp);
|
pos_views(pa->pos, pb->pos, pos);
|
||||||
|
|
||||||
|
float min_dist = INFINITY;
|
||||||
|
vec2_t min_disp;
|
||||||
|
for (unsigned i = 0; i < 4; ++i) {
|
||||||
|
const vec2_t disp = vec2_sub(pos[i], pa->pos);
|
||||||
|
const float dist = vec2_len(disp);
|
||||||
|
if (dist < min_dist) {
|
||||||
|
min_disp = disp;
|
||||||
|
min_dist = dist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const vec2_t norm = vec2_norm(min_disp);
|
||||||
return (physics_sep_t) {
|
return (physics_sep_t) {
|
||||||
.dist = vec2_len(disp),
|
.dist = min_dist,
|
||||||
.norm = norm,
|
.norm = norm,
|
||||||
.va = vec2_dot(pa->vel, norm),
|
.va = vec2_dot(pa->vel, norm),
|
||||||
.vb = vec2_dot(pb->vel, norm),
|
.vb = vec2_dot(pb->vel, norm),
|
||||||
|
|||||||
Reference in New Issue
Block a user