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:
parent
ddb01d0509
commit
a0b6536432
2 changed files with 5 additions and 6 deletions
|
@ -200,10 +200,8 @@ float World::castRay(float originX, float originY, float orientation) const
|
||||||
(originY - hCheckY)*(originY - hCheckY));
|
(originY - hCheckY)*(originY - hCheckY));
|
||||||
float vDist = sqrtf((originX - vCheckX)*(originX - vCheckX) +
|
float vDist = sqrtf((originX - vCheckX)*(originX - vCheckX) +
|
||||||
(originY - vCheckY)*(originY - vCheckY));
|
(originY - vCheckY)*(originY - vCheckY));
|
||||||
float finalDist = hDist > vDist ? vDist : hDist;
|
|
||||||
|
|
||||||
/* 2 Is wall height in meters. */
|
return hDist > vDist ? vDist : hDist;
|
||||||
return player.focalLength*2/finalDist;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::fillColumn(sf::RenderWindow& window, unsigned int column,
|
void World::fillColumn(sf::RenderWindow& window, unsigned int column,
|
||||||
|
@ -250,7 +248,8 @@ void World::render(sf::RenderWindow& window) const
|
||||||
} else if (rayAngle > 360) {
|
} else if (rayAngle > 360) {
|
||||||
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);
|
fillColumn(window, i, obstacleScale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
World.h
4
World.h
|
@ -54,11 +54,11 @@ private:
|
||||||
void fillColumn(sf::RenderWindow&, unsigned int column, float scale,
|
void fillColumn(sf::RenderWindow&, unsigned int column, float scale,
|
||||||
sf::Color wallColor = sf::Color(84,56,34)) const;
|
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 originX Ray X origin, strictly positive
|
||||||
* @param originY Ray Y origin, strictly positive
|
* @param originY Ray Y origin, strictly positive
|
||||||
* @param orientation Angle to cast to, in degrees between 0 and 360
|
* @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;
|
float castRay(float originX, float originY, float orientation) const;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue