Implement DDA raycasting, fix bug in path smoothing
This commit is contained in:
@@ -29,21 +29,6 @@ static PathData *pushPathWaypoint(PathData *pathData, Position waypoint, BzObjec
|
||||
return pathData;
|
||||
}
|
||||
|
||||
static bool canRayCastLine(BzTileMap *map, Position from, Position to) {
|
||||
Vector2 step = Vector2Subtract(to, from);
|
||||
step = Vector2Normalize(step);
|
||||
|
||||
while (Vector2DistanceSqr(from, to) > 1.0f) {
|
||||
from = Vector2Add(from, step);
|
||||
|
||||
BzTile tileX = 0, tileY = 0;
|
||||
bzTileMapPosToTile(map, from, &tileX, &tileY);
|
||||
if (bzTileMapHasCollision(map, tileX, tileY))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void reversePath(PathData *pathData) {
|
||||
while (pathData) {
|
||||
for (i32 i = 0; i < pathData->numWaypoints / 2; i++) {
|
||||
@@ -61,8 +46,9 @@ static void reversePath(PathData *pathData) {
|
||||
static void smoothPath(BzTileMap *map, PathData *pathData, BzObjectPool *pool) {
|
||||
// Our smoothed path
|
||||
PathData *outPath = pathData;
|
||||
size_t outIdx = 1;
|
||||
Position outPos = outPath->waypoints[0];
|
||||
size_t outIdx = 1;
|
||||
Position lastPos = outPos;
|
||||
|
||||
#define NEXT_WAYPOINT(path, idx, len) \
|
||||
do { \
|
||||
@@ -74,18 +60,20 @@ do { \
|
||||
} while (0)
|
||||
|
||||
PathData *currPath = pathData;
|
||||
size_t currIdx = 0;
|
||||
PathData *nextPath = pathData;
|
||||
size_t currIdx = 0;
|
||||
size_t nextIdx = 0;
|
||||
Position lastPos = outPos;
|
||||
// Needed, because we overwrite numWaypoints
|
||||
size_t currPathLen = currPath->numWaypoints;
|
||||
size_t nextPathLen = nextPath->numWaypoints;
|
||||
|
||||
// Second element
|
||||
currIdx++;
|
||||
NEXT_WAYPOINT(currPath, currIdx, currPathLen);
|
||||
// Third element
|
||||
nextIdx++;
|
||||
NEXT_WAYPOINT(nextPath, nextIdx, nextPathLen);
|
||||
nextIdx++;
|
||||
NEXT_WAYPOINT(nextPath, nextIdx, nextPathLen);
|
||||
|
||||
outPath->numWaypoints = 1;
|
||||
@@ -100,7 +88,7 @@ do { \
|
||||
NEXT_WAYPOINT(currPath, currIdx, currPathLen);
|
||||
NEXT_WAYPOINT(nextPath, nextIdx, nextPathLen);
|
||||
|
||||
if (!canRayCastLine(map, outPos, nextPos)) {
|
||||
if (!bzTileMapCanRayCastLine(map, outPos, nextPos)) {
|
||||
outPos = currPos;
|
||||
outPath->waypoints[outIdx++] = currPos;
|
||||
outPath->numWaypoints = outIdx;
|
||||
@@ -113,7 +101,6 @@ do { \
|
||||
}
|
||||
}
|
||||
#undef NEXT_WAYPOINT
|
||||
BZ_ASSERT(lastPos.x != INFINITY && lastPos.y != INFINITY);
|
||||
outPath->waypoints[outIdx++] = lastPos;
|
||||
outPath->numWaypoints = outIdx;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user