Add CLAMP macro and vec_clamp to util.c

This commit is contained in:
8dcc 2023-07-24 10:14:50 +02:00
parent e8e6e48952
commit 930ee5cef5
2 changed files with 9 additions and 0 deletions

View File

@ -15,6 +15,8 @@ typedef struct {
} rgb_t; } rgb_t;
#define DEG2RAD(n) ((n)*M_PI / 180.0) #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) \ #define gl_drawline_points(p0, p1, w, col) \
gl_drawline(p0[0], p0[1], p1[0], p1[1], 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); vec3_t vec3(float x, float y, float z);
bool vec_is_zero(vec3_t v); bool vec_is_zero(vec3_t v);
float vec_len2d(vec3_t v); float vec_len2d(vec3_t v);
void vec_clamp(vec3_t v);
float angle_delta_rad(float a, float b); float angle_delta_rad(float a, float b);
bool world_to_screen(vec3_t vec, vec2_t screen); bool world_to_screen(vec3_t vec, vec2_t screen);
void gl_drawbox(int x, int y, int w, int h, rgb_t c); void gl_drawbox(int x, int y, int w, int h, rgb_t c);

View File

@ -52,6 +52,12 @@ float vec_len2d(vec3_t v) {
return sqrtf(v.x * v.x + v.y * v.y); 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 angle_delta_rad(float a, float b) {
float delta = isfinite(a - b) ? remainder(a - b, 360) : 0; float delta = isfinite(a - b) ? remainder(a - b, 360) : 0;