From 60847df031d8b7f1ebcd437e90a8a79b382b585d Mon Sep 17 00:00:00 2001 From: 8dcc <8dcc.git@gmail.com> Date: Sun, 23 Jul 2023 16:21:53 +0200 Subject: [PATCH] Add vec_len2d and angle_delta_rad to util.c --- src/include/util.h | 2 ++ src/util.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/include/util.h b/src/include/util.h index 55ee058..656227a 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -26,6 +26,8 @@ bool valid_client(cl_entity_t* ent); 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); +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); void gl_drawline(int x0, int y0, int x1, int y1, float w, rgb_t col); diff --git a/src/util.c b/src/util.c index f5c062b..7f7ace8 100644 --- a/src/util.c +++ b/src/util.c @@ -1,8 +1,10 @@ + #include "include/util.h" #include "include/sdk.h" #include "include/globals.h" +#include #include bool is_alive(cl_entity_t* ent) { @@ -34,6 +36,21 @@ bool vec_is_zero(vec3_t v) { return v[0] == 0.0f && v[1] == 0.0f && v[2] == 0.0f; } +float vec_len2d(vec3_t v) { + return sqrtf(v.x * v.x + v.y * v.y); +} + +float angle_delta_rad(float a, float b) { + float delta = isfinite(a - b) ? remainder(a - b, 360) : 0; + + if (a > b && delta >= M_PI) + delta -= M_PI * 2; + else if (delta <= -M_PI) + delta += M_PI * 2; + + return delta; +} + bool world_to_screen(vec3_t vec, vec2_t screen) { if (vec_is_zero(vec)) return false;