- afegit el WAD del Doom 1 - Ja llegim FLATS i TEXTURES del WAD, falten els PATCH
81 lines
2.2 KiB
C++
81 lines
2.2 KiB
C++
#include "util.h"
|
|
#include <SDL3/SDL.h>
|
|
|
|
// Funcions de càlcul
|
|
|
|
// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines
|
|
// intersect the intersection point may be stored in the floats i_x and i_y.
|
|
const bool get_line_intersection(vec2 p0, vec2 p1, vec2 p2, vec2 p3, vec2 *i)
|
|
{
|
|
vec2 s1, s2;
|
|
s1.x = p1.x - p0.x; s1.y = p1.y - p0.y;
|
|
s2.x = p3.x - p2.x; s2.y = p3.y - p2.y;
|
|
|
|
float s, t;
|
|
s = (-s1.y * (p0.x - p2.x) + s1.x * (p0.y - p2.y)) / (-s2.x * s1.y + s1.x * s2.y);
|
|
t = ( s2.x * (p0.y - p2.y) - s2.y * (p0.x - p2.x)) / (-s2.x * s1.y + s1.x * s2.y);
|
|
|
|
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
|
|
{
|
|
// Collision detected
|
|
if (i != NULL)
|
|
{
|
|
i->x = p0.x + (t * s1.x);
|
|
i->y = p0.y + (t * s1.y);
|
|
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return false; // No collision
|
|
}
|
|
|
|
const float distance(vec2 p1, vec2 p2)
|
|
{
|
|
const float xx = p2.x-p1.x;
|
|
const float yy = p2.y-p1.y;
|
|
return SDL_sqrtf(xx*xx+yy*yy);
|
|
}
|
|
|
|
const void normalize(vec2 *v)
|
|
{
|
|
const float length = SDL_sqrtf(v->x*v->x + v->y*v->y);
|
|
if (length > 0.0f) {
|
|
v->x /= length;
|
|
v->y /= length;
|
|
}
|
|
}
|
|
|
|
const float dot(vec2 v1, vec2 v2)
|
|
{
|
|
return v1.x*v2.x + v1.y*v2.y;
|
|
}
|
|
|
|
float is_enemy_in_fov(double Ax, double Ay, double orientation_deg, double Bx, double By) {
|
|
// Convert orientation angle to radians
|
|
double orientation_rad = orientation_deg * DEG_TO_RAD;
|
|
|
|
// Compute view direction vector from angle
|
|
double view_dx = SDL_cosf(orientation_rad);
|
|
double view_dy = SDL_sinf(orientation_rad);
|
|
|
|
// Vector from A to B
|
|
double to_enemy_dx = Bx - Ax;
|
|
double to_enemy_dy = By - Ay;
|
|
|
|
// Normalize both vectors
|
|
double len = SDL_sqrtf(to_enemy_dx * to_enemy_dx + to_enemy_dy * to_enemy_dy);
|
|
if (len == 0) return 0.0; // Same position
|
|
|
|
to_enemy_dx /= len;
|
|
to_enemy_dy /= len;
|
|
|
|
// Compute signed angle using atan2 of perp-dot and dot
|
|
double dot = view_dx * to_enemy_dx + view_dy * to_enemy_dy;
|
|
double cross = view_dx * to_enemy_dy - view_dy * to_enemy_dx;
|
|
|
|
float angle_rad = SDL_atan2f(cross, dot);
|
|
return angle_rad * RAD_TO_DEG; // Signed angle in degrees
|
|
}
|
|
|