B
B
Bulat Antonov2021-10-16 23:45:10
C++ / C#
Bulat Antonov, 2021-10-16 23:45:10

Fix beam throw function?

I'm making a 3D game and using the ray-casting method for this, everything seems to work fine for me, except for the cast ray function itself:

sf::Color cast_ray(float x, float y, float angle, int max_dist) {
    float x_pos = x;
    float y_pos = y;
    float x_step = sin(angle);
    float y_step = cos(angle);
    for (int i = 0; i < max_dist; i++) {
        x_pos += x_step;
        y_pos += y_step;
        int x_ = x_pos / x_step;
        int y_ = y_pos / y_step;
        printf("%i %i\n", x_, y_);
        if (x_>-1 && x_ < 10 && y_>-1 && y_ < 10) {
            if (map[x_][y_] == 1) {
                return sf::Color(255, 255, 255);
            }
        }
    }
    return sf::Color(0, 0, 0, 0);
}

I almost forgot to say that I use sfml and there is another very important point, if you look at the function and see that it is normal, please notify me about this, I could be mistaken about where the problem occurs.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question