1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/raycastlib.git synced 2024-11-21 20:29:59 +01:00

Add multi hit function

This commit is contained in:
Miloslav Číž 2018-08-23 02:36:51 +02:00
parent b7d9db3edf
commit 5327bbc5c6

View file

@ -40,7 +40,6 @@ typedef struct
coordinate. */ coordinate. */
} HitResult; } HitResult;
/** /**
Casts a single ray and returns the first collision result. Casts a single ray and returns the first collision result.
@ -56,6 +55,14 @@ typedef struct
HitResult castRay(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t), HitResult castRay(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t),
uint16_t maxSteps); uint16_t maxSteps);
/**
Casts a single ray and returns a list of collisions.
*/
void castRayMultiHit(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t),
uint16_t maxSteps, HitResult *hitResults, uint16_t *hitResultsLen,
uint16_t maxHits);
//============================================================================= //=============================================================================
// privates // privates
@ -174,43 +181,71 @@ void castRaySquare(Ray localRay, Vector2D *nextCellOffset,
#undef helper #undef helper
} }
HitResult castRay(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t), void castRayMultiHit(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t),
uint16_t maxSteps) uint16_t maxSteps, HitResult *hitResults, uint16_t *hitResultsLen,
uint16_t maxHits)
{ {
HitResult result;
Vector2D initialPos = ray.start; Vector2D initialPos = ray.start;
Vector2D currentPos = ray.start;
result.distance = -1; Vector2D currentSquare;
result.square.x = ray.start.x / UNITS_PER_SQUARE;
result.square.y = ray.start.y / UNITS_PER_SQUARE;
result.position = ray.start;
int16_t squareType = arrayFunc(result.square.x,result.square.y); currentSquare.x = ray.start.x / UNITS_PER_SQUARE;
currentSquare.y = ray.start.y / UNITS_PER_SQUARE;
*hitResultsLen = 0;
int16_t squareType = arrayFunc(currentSquare.x,currentSquare.y);
for (uint16_t i = 0; i < maxSteps; ++i) for (uint16_t i = 0; i < maxSteps; ++i)
{ {
if (arrayFunc(result.square.x,result.square.y) != squareType) int16_t currentType = arrayFunc(currentSquare.x,currentSquare.y);
{
result.distance = dist(initialPos,result.position);
//result.textureCoord =
if (currentType != squareType)
{
// collision
HitResult h;
h.position = currentPos;
h.square = currentSquare;
h.distance = dist(initialPos,currentPos);
hitResults[*hitResultsLen] = h;
*hitResultsLen += 1;
squareType = currentType;
if (*hitResultsLen >= maxHits)
break; break;
} }
ray.start.x = result.position.x % UNITS_PER_SQUARE; ray.start.x = currentPos.x % UNITS_PER_SQUARE;
ray.start.y = result.position.y % UNITS_PER_SQUARE; ray.start.y = currentPos.y % UNITS_PER_SQUARE;
Vector2D no, co; Vector2D no, co;
castRaySquare(ray,&no,&co); castRaySquare(ray,&no,&co);
result.square.x += no.x; currentSquare.x += no.x;
result.square.y += no.y; currentSquare.y += no.y;
result.position.x += co.x; currentPos.x += co.x;
result.position.y += co.y; currentPos.y += co.y;
} }
}
HitResult castRay(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t),
uint16_t maxSteps)
{
HitResult result;
uint16_t len;
castRayMultiHit(ray,arrayFunc,maxSteps,&result,&len,1);
if (len == 0)
result.distance = -1;
return result; return result;
} }