56 lines
1.1 KiB
C
56 lines
1.1 KiB
C
/**
|
|
* Copyright (c) 2021 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "aabb.h"
|
|
|
|
bool aabbPoint2D(
|
|
float pointX, float pointY,
|
|
float x, float y, float width, float height,
|
|
aabbpointhit2d_t *hit
|
|
) {
|
|
|
|
float dx, px, sx, halfWidth;
|
|
float dy, py, sy, halfHeight;
|
|
|
|
if(hit == NULL) {
|
|
// Check X Axis, are we outside the box
|
|
if(pointX < x || pointY < y) return false;
|
|
if(pointX > x+width || pointY > y+height) return false;
|
|
return true;
|
|
}
|
|
|
|
halfWidth = width / 2;
|
|
halfHeight = height / 2;
|
|
x += halfWidth;
|
|
y += halfHeight;
|
|
|
|
dx = pointX - x;
|
|
px = halfWidth - mathAbs(dx);
|
|
if(px <= 0) return false;
|
|
|
|
dy = pointY - y;
|
|
py = halfHeight - mathAbs(dy);
|
|
if(py <= 0) return false;
|
|
|
|
if(px < py) {
|
|
sx = mathSign(dx);
|
|
// float deltaX = px *sx;
|
|
hit->normalX = sx;
|
|
hit->normalY = 0;
|
|
hit->hitX = x + (halfWidth * sx);
|
|
hit->hitY = pointY;
|
|
} else {
|
|
sy = mathSign(dy);
|
|
// float deltaY = py * sy;
|
|
hit->normalX = 0;
|
|
hit->normalY = sy;
|
|
hit->hitX = pointX;
|
|
hit->hitY = x + (halfHeight * sy);
|
|
}
|
|
|
|
return true;
|
|
} |