Add vec_sub, vec_norm, vec_to_ang and RAD2DEG to util

This commit is contained in:
8dcc 2023-07-29 16:23:26 +02:00
parent 9f64f544de
commit e9f3031ca1
2 changed files with 25 additions and 1 deletions

View File

@ -14,7 +14,8 @@ typedef struct {
uint8_t r, g, b;
} rgb_t;
#define DEG2RAD(n) ((n)*M_PI / 180.0)
#define DEG2RAD(n) ((n)*M_PI / 180.0f)
#define RAD2DEG(n) ((n)*180.0f / M_PI)
#define CLAMP(val, min, max) \
(((val) > (max)) ? (max) : (((val) < (min)) ? (min) : (val)))
@ -32,10 +33,13 @@ game_id get_cur_game(void);
vec3_t vec3(float x, float y, float z);
void vec_copy(vec3_t* dst, const vec3_t* src);
vec3_t vec_add(vec3_t a, vec3_t b);
vec3_t vec_sub(vec3_t a, vec3_t b);
bool vec_is_zero(vec3_t v);
float vec_len2d(vec3_t v);
void vec_clamp(vec3_t v);
void vec_norm(vec3_t v);
float angle_delta_rad(float a, float b);
vec3_t vec_to_ang(vec3_t v);
vec3_t matrix_3x4_origin(matrix_3x4 m);
bool world_to_screen(vec3_t vec, vec2_t screen);
void engine_draw_text(int x, int y, char* s, rgb_t c);

View File

@ -110,6 +110,10 @@ vec3_t vec_add(vec3_t a, vec3_t b) {
return vec3(a.x + b.x, a.y + b.y, a.z + b.z);
}
vec3_t vec_sub(vec3_t a, vec3_t b) {
return vec3(a.x - b.x, a.y - b.y, a.z - b.z);
}
bool vec_is_zero(vec3_t v) {
return v.x == 0.0f && v.y == 0.0f && v.z == 0.0f;
}
@ -124,6 +128,12 @@ void vec_clamp(vec3_t v) {
v.z = CLAMP(v.z, -50.0f, 50.0f);
}
void vec_norm(vec3_t v) {
v.x = isfinite(v.x) ? remainderf(v.x, 360.0f) : 0.0f;
v.y = isfinite(v.y) ? remainderf(v.y, 360.0f) : 0.0f;
v.z = 0.0f;
}
float angle_delta_rad(float a, float b) {
float delta = isfinite(a - b) ? remainder(a - b, 360) : 0;
@ -135,6 +145,16 @@ float angle_delta_rad(float a, float b) {
return delta;
}
vec3_t vec_to_ang(vec3_t v) {
vec3_t ret;
ret.x = RAD2DEG(atan2(-v.z, hypot(v.x, v.y)));
ret.y = RAD2DEG(atan2(v.y, v.x));
ret.z = 0.0f;
return ret;
}
vec3_t matrix_3x4_origin(matrix_3x4 m) {
vec3_t ret;