Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualÁlvaro

Posted 29 November 2012 - 05:00 PM

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;
}

#1Álvaro

Posted 29 November 2012 - 04:59 PM

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()) {
    // Process events
    sf::Event Event;
    while (app.GetEvent(Event)) {
	  // Close window : exit
	  if (Event.Type == sf::Event::Closed)
	    app.Close();
    }
   
    // Clear screen
    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);
    }
    // Draw predefined shapes
   
    // Finally, display the rendered frame on screen
    app.Display();
  }
 
  return EXIT_SUCCESS;
}

PARTNERS