diff --git a/src/include/util.h b/src/include/util.h index 31fae2e..0b6298b 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -15,6 +15,8 @@ typedef struct { } rgb_t; #define DEG2RAD(n) ((n)*M_PI / 180.0) +#define CLAMP(val, min, max) \ + (((val) > (max)) ? (max) : (((val) < (min)) ? (min) : (val))) #define gl_drawline_points(p0, p1, w, col) \ gl_drawline(p0[0], p0[1], p1[0], p1[1], w, col); @@ -28,6 +30,7 @@ char* get_name(int ent_idx); vec3_t vec3(float x, float y, float z); bool vec_is_zero(vec3_t v); float vec_len2d(vec3_t v); +void vec_clamp(vec3_t v); float angle_delta_rad(float a, float b); bool world_to_screen(vec3_t vec, vec2_t screen); void gl_drawbox(int x, int y, int w, int h, rgb_t c); diff --git a/src/util.c b/src/util.c index 1d7a371..8884bbe 100644 --- a/src/util.c +++ b/src/util.c @@ -52,6 +52,12 @@ float vec_len2d(vec3_t v) { return sqrtf(v.x * v.x + v.y * v.y); } +void vec_clamp(vec3_t v) { + v[0] = CLAMP(v[0], -89.0f, 89.0f); + v[1] = CLAMP(remainderf(v[1], 360.0f), -180.0f, 180.0f); /* v.y % 360 */ + v[2] = CLAMP(v[2], -50.0f, 50.0f); +} + float angle_delta_rad(float a, float b) { float delta = isfinite(a - b) ? remainder(a - b, 360) : 0;