World: Introduce a struct for raycast results
In order to implement more complexe behaviors, more information than the distance is needed from a raycast. Add a new struct to store and return this information, currently hit position and hit type, and update castRay to return that instead.
This commit is contained in:
parent
b0c9d24787
commit
8199fd3449
2 changed files with 27 additions and 5 deletions
20
World.cpp
20
World.cpp
|
@ -99,7 +99,7 @@ std::ostream& operator<<(std::ostream& ostream, World const& world)
|
|||
return(ostream);
|
||||
}
|
||||
|
||||
float World::castRay(float originX, float originY, float orientation) const
|
||||
RaycastResult World::castRay(float originX, float originY, float orientation) const
|
||||
{
|
||||
/*
|
||||
* Reference used for ray intersection computations :
|
||||
|
@ -214,7 +214,21 @@ float World::castRay(float originX, float originY, float orientation) const
|
|||
float vDist = sqrtf((originX - vCheckX)*(originX - vCheckX) +
|
||||
(originY - vCheckY)*(originY - vCheckY));
|
||||
|
||||
return hDist > vDist ? vDist : hDist;
|
||||
|
||||
RaycastResult result{};
|
||||
if (hDist > vDist) {
|
||||
result.distance = vDist;
|
||||
result.hitX = vCheckX;
|
||||
result.hitY = vCheckY;
|
||||
} else {
|
||||
result.distance = hDist;
|
||||
result.hitX = hCheckX;
|
||||
result.hitY = hCheckY;
|
||||
}
|
||||
result.hitBlock = getBlock(floorf(result.hitX) + vRound,
|
||||
floorf(result.hitY));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void World::fillColumn(sf::RenderWindow& window, unsigned int column,
|
||||
|
@ -268,7 +282,7 @@ void World::render(sf::RenderWindow& window)
|
|||
rayAngle -= 360;
|
||||
}
|
||||
float obstacleScale = worldToCamera / (
|
||||
castRay(player.x, player.y, rayAngle) *
|
||||
castRay(player.x, player.y, rayAngle).distance *
|
||||
cosf(deltaAngle*deg_to_rad)
|
||||
);
|
||||
/* 2 Is wall height in meters. */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue