Refactor AABB functions
parent
9223dcc0e9
commit
87b2db4ea4
|
@ -0,0 +1,46 @@
|
||||||
|
#include "AABB.h"
|
||||||
|
|
||||||
|
bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap)
|
||||||
|
{
|
||||||
|
// No Overlap
|
||||||
|
if (l1.y < l2.x || l2.y < l1.x) return false;
|
||||||
|
|
||||||
|
if (l1.x >= l2.x && l1.y <= l2.y)
|
||||||
|
{
|
||||||
|
// Complete Overlap, not sure what to do tbh
|
||||||
|
*overlap = l2.y-l2.x + l1.y-l1.x;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//Partial overlap
|
||||||
|
// x is p1, y is p2
|
||||||
|
*overlap = (l2.y >= l1.y)? l2.x - l1.y : l2.y - l1.x;
|
||||||
|
}
|
||||||
|
if (fabs(*overlap) < 0.01) // Use 2 dp precision
|
||||||
|
{
|
||||||
|
*overlap = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2 * const overlap)
|
||||||
|
{
|
||||||
|
// Note that we include one extra pixel for checking
|
||||||
|
// This avoid overlapping on the border
|
||||||
|
Vector2 l1, l2;
|
||||||
|
bool overlap_x, overlap_y;
|
||||||
|
l1.x = tl1.x;
|
||||||
|
l1.y = tl1.x + sz1.x;
|
||||||
|
l2.x = tl2.x;
|
||||||
|
l2.y = tl2.x + sz2.x;
|
||||||
|
|
||||||
|
overlap_x = find_1D_overlap(l1, l2, &overlap->x);
|
||||||
|
l1.x = tl1.y;
|
||||||
|
l1.y = tl1.y + sz1.y;
|
||||||
|
l2.x = tl2.y;
|
||||||
|
l2.y = tl2.y + sz2.y;
|
||||||
|
overlap_y = find_1D_overlap(l1, l2, &overlap->y);
|
||||||
|
|
||||||
|
return overlap_x && overlap_y;
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef __AABB_H
|
||||||
|
#define __AABB_H
|
||||||
|
#include "raylib.h"
|
||||||
|
#include "raymath.h"
|
||||||
|
bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap);
|
||||||
|
bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2 * const overlap);
|
||||||
|
#endif // __AABB_H
|
|
@ -3,6 +3,7 @@ add_library(lib_scenes STATIC
|
||||||
scene.c
|
scene.c
|
||||||
scene_impl.c
|
scene_impl.c
|
||||||
game_systems.c
|
game_systems.c
|
||||||
|
AABB.c
|
||||||
)
|
)
|
||||||
target_include_directories(lib_scenes
|
target_include_directories(lib_scenes
|
||||||
PUBLIC
|
PUBLIC
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#include "game_systems.h"
|
#include "game_systems.h"
|
||||||
|
#include "AABB.h"
|
||||||
#include "constants.h"
|
#include "constants.h"
|
||||||
#include "raylib.h"
|
|
||||||
#include "raymath.h"
|
|
||||||
|
|
||||||
static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE};
|
static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE};
|
||||||
static const Vector2 GRAVITY = {0, GRAV_ACCEL};
|
static const Vector2 GRAVITY = {0, GRAV_ACCEL};
|
||||||
|
@ -26,51 +25,6 @@ static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width
|
||||||
return tile_y * tilemap_width + tile_x;
|
return tile_y * tilemap_width + tile_x;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap)
|
|
||||||
{
|
|
||||||
// No Overlap
|
|
||||||
if (l1.y < l2.x || l2.y < l1.x) return false;
|
|
||||||
|
|
||||||
if (l1.x >= l2.x && l1.y <= l2.y)
|
|
||||||
{
|
|
||||||
// Complete Overlap, not sure what to do tbh
|
|
||||||
*overlap = l2.y-l2.x + l1.y-l1.x;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//Partial overlap
|
|
||||||
// x is p1, y is p2
|
|
||||||
*overlap = (l2.y >= l1.y)? l2.x - l1.y : l2.y - l1.x;
|
|
||||||
}
|
|
||||||
if (fabs(*overlap) < 0.01) // Use 2 dp precision
|
|
||||||
{
|
|
||||||
*overlap = 0;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2 * const overlap)
|
|
||||||
{
|
|
||||||
// Note that we include one extra pixel for checking
|
|
||||||
// This avoid overlapping on the border
|
|
||||||
Vector2 l1, l2;
|
|
||||||
bool overlap_x, overlap_y;
|
|
||||||
l1.x = tl1.x;
|
|
||||||
l1.y = tl1.x + sz1.x;
|
|
||||||
l2.x = tl2.x;
|
|
||||||
l2.y = tl2.x + sz2.x;
|
|
||||||
|
|
||||||
overlap_x = find_1D_overlap(l1, l2, &overlap->x);
|
|
||||||
l1.x = tl1.y;
|
|
||||||
l1.y = tl1.y + sz1.y;
|
|
||||||
l2.x = tl2.y;
|
|
||||||
l2.y = tl2.y + sz2.y;
|
|
||||||
overlap_y = find_1D_overlap(l1, l2, &overlap->y);
|
|
||||||
|
|
||||||
return overlap_x && overlap_y;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool check_collision_at(Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 point)
|
static bool check_collision_at(Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 point)
|
||||||
{
|
{
|
||||||
Vector2 new_pos = Vector2Add(pos, point);
|
Vector2 new_pos = Vector2Add(pos, point);
|
||||||
|
|
Loading…
Reference in New Issue