2D Ray-casting problems, don't align correctly.

Started by
11 comments, last by alvaro 11 years, 4 months ago
This seems to work. See if you understand how it does everything and ask about anything that is not entirely clear.

#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
void draw_line(sf::RenderWindow &app, sf::Vector2f from, sf::Vector2f to) {
app.Draw(sf::Shape::Line(std::floor(400.0f + from.x * 200.0f),
std::floor(300.0f - from.y * 200.0f),
std::floor(400.0f + to.x * 200.0f),
std::floor(300.0f - to.y * 200.0f),
1, sf::Color::White));
}
int main() {
sf::RenderWindow app(sf::VideoMode(800, 600), "Ray casting computations");

while (app.IsOpened()) {
sf::Event Event;
while (app.GetEvent(Event)) {
if (Event.Type == sf::Event::Closed)
app.Close();
}

app.Clear();

int const width = 100;

float const degrees = std::atan(1.0f)/45.0f;
float K = std::tan(30.0f*degrees);
sf::Vector2f position(0.0f, 0.0f);
sf::Vector2f heading(std::cos(30.0f*degrees), std::sin(30.0f*degrees));
sf::Vector2f left(-heading.y, heading.x);
sf::Vector2f left_edge = position + heading + K * left;
sf::Vector2f step = -(2.0f * K / width) * left ;

for (int i=0; i<width; ++i) {
sf::Vector2f ray_end = left_edge + step * (i+0.5f);
draw_line(app, position, ray_end);
}
app.Display();
}

return EXIT_SUCCESS;
}
Advertisement
Ugh, a lot of magic numbers, but the code works ( needed to make a bit conversion magic into SFML 2.0 )!
Is it okay if I respond tomorrow? It's getting quite late here :)

On the top of my head here are some questions:

void draw_line(sf::RenderWindow &app, sf::Vector2f from, sf::Vector2f to) {
sf::VertexArray line(sf::Lines, 2);
line[0] = sf::Vector2f(std::floor(400.0f + from.x * 200.0f),std::floor(300.0f - from.y * 200.0f));
line[1] = sf::Vector2f(std::floor(400.0f + to.x * 200.0f), std::floor(300.0f - to.y * 200.0f) );
app.draw(line);
}

I'm guessing 200.0f is the distance to draw the rays, 400.0f is the center of the map, and so is 300.0f?



float const degrees = std::atan(1.0f)/45.0f;

What exactly are the values used here? What does 1 and 45 represent?

float K = std::tan(30.0f*degrees);
What exactly is 30 representing in the code? Is it the same 30? for all the 30's in the code?

sf::Vector2f left(-heading.y, heading.x);
Why is the y and x inverted?

sf::Vector2f step = -(2.0f * K / width) * left ;
I have no idea what this is.

sf::Vector2f ray_end = left_edge + step * (i+0.5f);
What exactly is end here and what is the number 0.5 coming from.

Also in the drawing exercise before what coordination system are we using? Polar or Cartesian? I'd love to be able to draw It sucessfully since that might just be the key to being able to visualise it manually.

Also, great work!

Ugh, a lot of magic numbers, but the code works ( needed to make a bit conversion magic into SFML 2.0 )!

Yeah, that's fair criticism. I thought the origin of the numbers would be more or less obvious, but it would be better to give things names.

Is it okay if I respond tomorrow? It's getting quite late here smile.png[/quote]
Of course.

On the top of my head here are some questions:

void draw_line(sf::RenderWindow &app, sf::Vector2f from, sf::Vector2f to) {
sf::VertexArray line(sf::Lines, 2);
line[0] = sf::Vector2f(std::floor(400.0f + from.x * 200.0f),std::floor(300.0f - from.y * 200.0f));
line[1] = sf::Vector2f(std::floor(400.0f + to.x * 200.0f), std::floor(300.0f - to.y * 200.0f) );
app.draw(line);
}

I'm guessing 200.0f is the distance to draw the rays, 400.0f is the center of the map, and so is 300.0f?[/quote]
400.0f and 300.0f are indeed the center of the screen. I am using coordinates like I would in Math everywhere in the program and this is the function that translates them into screen coordinates. 200.0f is a scale factor, so the window covers the interval (-2, 2) horizontally and (-1.5, +1.5) vertically.



float const degrees = std::atan(1.0f)/45.0f;

What exactly are the values used here? What does 1 and 45 represent?[/quote]
The angle whose tan is 1 is 45 degrees, so by multiplying a number of degrees by this number, you turn it into radians.


float K = std::tan(30.0f*degrees);
What exactly is 30 representing in the code? Is it the same 30? for all the 30's in the code?[/quote]
30.0f if half the FOV of 60 degrees.

sf::Vector2f left(-heading.y, heading.x);
Why is the y and x inverted?[/quote]
I think I covered this one already: This is just a 90-degree rotation of the heading vector.

sf::Vector2f step = -(2.0f * K / width) * left ;
I have no idea what this is.[/quote]
That's how much I need to advance for each pixel.

sf::Vector2f ray_end = left_edge + step * (i+0.5f);
What exactly is end here and what is the number 0.5 coming from.[/quote]
I think of the ray as an arrow, with a beginning (the player's position) and an end. The 0.5 is there so the ray aims to the center of the pixel, instead of the left side.


Also in the drawing exercise before what coordination system are we using? Polar or Cartesian? I'd love to be able to draw It sucessfully since that might just be the key to being able to visualise it manually.[/quote]
I have only used Cartesian coordinates everywhere. Maybe I'll try to make a picture, but it's a bit of a pain.

Also, great work!
[/quote]
Thanks!

This topic is closed to new replies.

Advertisement