1
0
Fork 0

World: make CastRay generic

Previously CastRay did the scale computation and used the player view statistics.
This makes it quite specialized, and the rest of the computations are made outside.

Move the computations in the render function and make castRay generic.
This commit is contained in:
Teo-CD 2024-01-25 22:30:38 +00:00
parent ddb01d0509
commit a0b6536432
2 changed files with 5 additions and 6 deletions

View file

@ -200,10 +200,8 @@ float World::castRay(float originX, float originY, float orientation) const
(originY - hCheckY)*(originY - hCheckY));
float vDist = sqrtf((originX - vCheckX)*(originX - vCheckX) +
(originY - vCheckY)*(originY - vCheckY));
float finalDist = hDist > vDist ? vDist : hDist;
/* 2 Is wall height in meters. */
return player.focalLength*2/finalDist;
return hDist > vDist ? vDist : hDist;
}
void World::fillColumn(sf::RenderWindow& window, unsigned int column,
@ -250,7 +248,8 @@ void World::render(sf::RenderWindow& window) const
} else if (rayAngle > 360) {
rayAngle -= 360;
}
float obstacleScale = castRay(player.x, player.y, rayAngle)/player.sensorSize;
float obstacleScale = player.focalLength*2/(castRay(player.x, player.y, rayAngle)*player.sensorSize);
/* 2 Is wall height in meters. */
fillColumn(window, i, obstacleScale);
}
}

View file

@ -54,11 +54,11 @@ private:
void fillColumn(sf::RenderWindow&, unsigned int column, float scale,
sf::Color wallColor = sf::Color(84,56,34)) const;
/**
* Cast a ray from a given position and return the on-screen scale.
* Cast a ray from a given position and its distance to the origin.
* @param originX Ray X origin, strictly positive
* @param originY Ray Y origin, strictly positive
* @param orientation Angle to cast to, in degrees between 0 and 360
* @return Scale on the screen of the hit wall.
* @return Distance of the hit to the origin.
*/
float castRay(float originX, float originY, float orientation) const;
};