I'm trying to render isometric maps, my final goal is to render scenes that resembles Snake Rattle 'n' Roll:

So I started by rendering cubes:

For now I have an array with a collection of 3D vectors with integer components (sf::Vector3<std::size_t>
) (no cubes located on half tiles) and I am brute forcing each cube position by multiplying that position by how much each cube should be displaced in each axis. Sorry for my poor communication skills, I hope that the code explains itself:
template <auto degrees>
constexpr auto degrees_to_radians_v = degrees * std::numbers::pi_v<decltype(degrees)> / decltype(degrees){180};
constexpr auto radians_60dg = degrees_to_radians_v<60.f>;
constexpr auto radians_180dg = degrees_to_radians_v<180.f>;
constexpr auto radians_300dg = degrees_to_radians_v<300.f>;
const static sf::Vector2f X_axis = {std::sin( radians_60dg), std::cos( radians_60dg)}; // 60º
const static sf::Vector2f Y_axis = {std::sin(radians_180dg), std::cos(radians_180dg)}; // 180º
const static sf::Vector2f Z_axis = {std::sin(radians_300dg), std::cos(radians_300dg)}; // 300º
struct Map : public sf::Drawable
{
using block_position_t = sf::Vector3<std::size_t>;
...
};
sf::Vector2f to_screen_position(const Map::block_position_t &position)
{
return
{
(position.x * X_axis.x) + (position.z * Z_axis.x),
(position.x * X_axis.y) - position.y + (position.z * Z_axis.y) // Y axis reversed
};
}
So, each time I want to draw a cube in a certain 3D position I call to_screen_position
to know where this position translates in the 2D position of the screen. I have the feeling that I'm overcomplicating things and I can do the same with matrix multiplication -which will allow me to even rotate the scene- so knowing that I'm bad at math I jumped to wikipedia's Transformation matrix page which is amazing but doesn't explain the kind of transformations I'm in need for.
If I'm uderstanding it properly, all transformations that wikipedia is explaining are from 2D ➡ 2D and 3D ➡ 3D but what I need is 3D ➡ 2D. What kind of matrix operations I should use for this goal?
{X_axis.x, 0, Z_axis.x, X_axis.y, -1, Z_axis.y}
with the 1x2 matrix{position.x, position.y}
I'll get the desired transformation? – PaperBirdMaster Oct 20 '21 at 23:48