fixed aabb aabb collision detection and solving #17
|
@ -31,30 +31,37 @@ static inline
|
||||||
int _rect_overlap(float aminx, float aminy, float amaxx, float amaxy, float bminx, float bminy, float bmaxx, float bmaxy) {
|
int _rect_overlap(float aminx, float aminy, float amaxx, float amaxy, float bminx, float bminy, float bmaxx, float bmaxy) {
|
||||||
return
|
return
|
||||||
(
|
(
|
||||||
(aminx < bmaxx && aminx > bmaxx)
|
(aminx < bmaxx && aminx > bminx)
|
||||||
||
|
||
|
||||||
(amaxx > bminx && amaxx < bmaxx)
|
(bminx < amaxx && bminx > aminx)
|
||||||
) && (
|
) && (
|
||||||
(aminy < bmaxy && aminy > bmaxy)
|
(aminy < bmaxy && aminy > bminy)
|
||||||
||
|
||
|
||||||
(amaxy > bminy && amaxy < bmaxy)
|
(bminy < amaxy && bminy > aminy)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
short _collision_aabb_aabb(const object_t* a, const object_t* b) {
|
short _collision_aabb_aabb(const object_t* a, const object_t* b) {
|
||||||
const float aminx = a->physics.aabb.x + a->sprite.x, aminy = a->physics.aabb.y + a->sprite.x;
|
const float aminx = a->physics.aabb.x + a->sprite.x,
|
||||||
const float amaxx = aminx + a->physics.aabb.w, amaxy = aminy + a->physics.aabb.h;
|
aminy = a->physics.aabb.y + a->sprite.y;
|
||||||
const float bminx = b->physics.aabb.x, bminy = b->physics.aabb.y;
|
const float amaxx = aminx + a->physics.aabb.w,
|
||||||
const float bmaxx = b->physics.aabb.x + b->physics.aabb.w, bmaxy = b->physics.aabb.y + b->physics.aabb.h;
|
amaxy = aminy + a->physics.aabb.h;
|
||||||
|
|
||||||
|
const float bminx = b->physics.aabb.x + b->sprite.x,
|
||||||
|
bminy = b->physics.aabb.y + b->sprite.y;
|
||||||
|
const float bmaxx = bminx + b->physics.aabb.w,
|
||||||
|
bmaxy = bminy + b->physics.aabb.h;
|
||||||
|
|
||||||
return _rect_overlap(aminx, aminy, amaxx, amaxy, bminx, bminy, bmaxx, bmaxy);
|
return _rect_overlap(aminx, aminy, amaxx, amaxy, bminx, bminy, bmaxx, bmaxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
short _collision_circle_circle(const object_t* a, const object_t* b) {
|
short _collision_circle_circle(const object_t* a, const object_t* b) {
|
||||||
const float ax = a->sprite.x + a->physics.circle.x, ay = a->sprite.y + a->physics.circle.y,
|
const float ax = a->sprite.x + a->physics.circle.x,
|
||||||
bx = b->sprite.x + b->physics.circle.x, by = b->sprite.y + b->physics.circle.y;
|
ay = a->sprite.y + a->physics.circle.y,
|
||||||
|
bx = b->sprite.x + b->physics.circle.x,
|
||||||
|
by = b->sprite.y + b->physics.circle.y;
|
||||||
const float dx = fabsf(ax-bx), dy = fabsf(ay-by);
|
const float dx = fabsf(ax-bx), dy = fabsf(ay-by);
|
||||||
const float sqrdist = dx*dx+dy*dy;
|
const float sqrdist = dx*dx+dy*dy;
|
||||||
const float mindist = a->physics.circle.radius + b->physics.circle.radius;
|
const float mindist = a->physics.circle.radius + b->physics.circle.radius;
|
||||||
|
@ -139,10 +146,20 @@ float _solve_circle_circle(const object_t* a, const object_t* b, float* out_px,
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
float _solve_aabb_aabb(const object_t* a, const object_t* b, float* out_px, float* out_py) {
|
float _solve_aabb_aabb(const object_t* a, const object_t* b, float* out_px, float* out_py) {
|
||||||
float right = (a->physics.aabb.x + a->physics.aabb.w + a->sprite.x) - (b->physics.aabb.x + b->sprite.x);
|
float aminx = a->physics.aabb.x + a->sprite.x;
|
||||||
float left = (a->physics.aabb.x + a->sprite.x) - (b->physics.aabb.x + b->physics.aabb.w + b->sprite.x);
|
float amaxx = aminx + a->physics.aabb.w;
|
||||||
float top = (a->physics.aabb.y + a->sprite.y) - (b->physics.aabb.y + b->physics.aabb.w + b->sprite.y);
|
float bminx = b->physics.aabb.x + b->sprite.x;
|
||||||
float bottom = (a->physics.aabb.y + a->physics.aabb.h) - (b->physics.aabb.y + b->sprite.y);
|
float bmaxx = bminx + b->physics.aabb.w;
|
||||||
|
|
||||||
|
float aminy = a->physics.aabb.y + a->sprite.y;
|
||||||
|
float amaxy = aminy + a->physics.aabb.h;
|
||||||
|
float bminy = b->physics.aabb.y + b->sprite.y;
|
||||||
|
float bmaxy = bminy + b->physics.aabb.h;
|
||||||
|
|
||||||
|
float right = bmaxx - aminx;
|
||||||
|
float left = bminx - amaxx;
|
||||||
|
float top = bminy - amaxy;
|
||||||
|
float bottom = bmaxy - aminy;
|
||||||
|
|
||||||
float ret = right;
|
float ret = right;
|
||||||
*out_px = right;
|
*out_px = right;
|
||||||
|
@ -183,7 +200,7 @@ float get_solve_force(const object_t* a, const object_t* b, float* out_px, float
|
||||||
|
|
||||||
void solve_collision_slide(object_t* left, object_t* right) {
|
void solve_collision_slide(object_t* left, object_t* right) {
|
||||||
float dx, dy;
|
float dx, dy;
|
||||||
const float d = get_solve_force(left, right, &dx, &dy);
|
get_solve_force(left, right, &dx, &dy);
|
||||||
left->sprite.x += dx;
|
left->sprite.x += dx;
|
||||||
left->sprite.y += dy;
|
left->sprite.y += dy;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue