Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMoonkis

Posted 29 November 2012 - 10:47 AM

I'v been trying and studying a hell of a lot for making my 2D raycasting, I'v been visiting KahnAcademy for Trigonometry-lessons as well as reading various topics ( each with a completely different method of achieving it. )

I'v chosen to stick with the topic found here

Currently I'm just trying to get the ray-casting and movement to work, so far so good, almost.
I'm not entirely sure of how to do the casting, but I got something almost working, though it's a bit off. Here is how it looks so far:
Posted Image

The blue/magenta arrow is the direction the player will travel ( current angle ) and the white rays are supposed to be the ray-casting, as you can see it's off by quite a bit and I can't understand why.

Here is my current casting code:
  sf::VertexArray ray(sf::Lines, 2);
  const int travelingDistans = 400;
  double subsequentRayAngleIncrease = player.fov/w;
  // Start from column 0 to the X't column of the viewing plane.
  // I'm not sure what the viewing plane is but I'm guessing it's the screen.
  // Which would mean from 0 to our screens highest x, which is 800.
  // Since the resolution is 800x600
  for( int i = 0; i < w; i++)
  {
   double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );

   double rx =  player.x - travelingDistans * std::cos(Math::to_radians(rayAngle));
   double ry =  player.y - travelingDistans * std::sin(Math::to_radians(rayAngle));
   // + 5 is so it's centered of the players origin
   ray[0] = sf::Vector2f( player.x + 5 , player.y + 5);
   ray[1] = sf::Vector2f( rx + 5, ry +5 );
   ray[1].color = sf::Color(255,255,255,30);
   ray[0].color = sf::Color(255,255,255,30);
   rwind.draw(ray);
  }

What am I doing wrong?

Also a couple of side questions:

1. Why do you multiply by -d and not just d ( distance ) when calculating with sin and cos?
2. Does this technique look right considering it's supposed to transition into fake 3D using 2D raycasting ( Given that the final ray distance is determined on where it hit a wall ).

Kind regards
Moonkis


EDIT:

Realized that the offset was due to me accidentally subtracting the subsequent ray angle when calculating the offset using players FOV
Changed it from this:
double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );
into this:
double rayAngle = ( player.rot -  (player.fov/2)  ) + subsequentRayAngleIncrease*i;

It seems to work as intended now, checked using 90, 180, 270 and 360 FOV angles. ( 180 and 90 was the ones I based it on working, they looked as intended, also added some wrapping when calculated angles for the rx and ry:
if( rayAngle == 360 ) rayAngle = 0;
if( rayAngle > 360 )
{
    rayAngle = ( rayAngle - 360 );
}
if( rayAngle < 0 )
{
    rayAngle = ( 360 + rayAngle );
}

Seeing as I only like to work with the angels 0 - 359 ( sin(to_radiance(360)) and sin(to_radiance(0)) gave different angles where as 0 degrees to radians was the most correct one.

#4Moonkis

Posted 29 November 2012 - 10:41 AM

I'v been trying and studying a hell of a lot for making my 2D raycasting, I'v been visiting KahnAcademy for Trigonometry-lessons as well as reading various topics ( each with a completely different method of achieving it. )

I'v chosen to stick with the topic found here

Currently I'm just trying to get the ray-casting and movement to work, so far so good, almost.
I'm not entirely sure of how to do the casting, but I got something almost working, though it's a bit off. Here is how it looks so far:
Posted Image

The blue/magenta arrow is the direction the player will travel ( current angle ) and the white rays are supposed to be the ray-casting, as you can see it's off by quite a bit and I can't understand why.

Here is my current casting code:
  sf::VertexArray ray(sf::Lines, 2);
  const int travelingDistans = 400;
  double subsequentRayAngleIncrease = player.fov/w;
  // Start from column 0 to the X't column of the viewing plane.
  // I'm not sure what the viewing plane is but I'm guessing it's the screen.
  // Which would mean from 0 to our screens highest x, which is 800.
  // Since the resolution is 800x600
  for( int i = 0; i < w; i++)
  {
   double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );

   double rx =  player.x - travelingDistans * std::cos(Math::to_radians(rayAngle));
   double ry =  player.y - travelingDistans * std::sin(Math::to_radians(rayAngle));
   // + 5 is so it's centered of the players origin
   ray[0] = sf::Vector2f( player.x + 5 , player.y + 5);
   ray[1] = sf::Vector2f( rx + 5, ry +5 );
   ray[1].color = sf::Color(255,255,255,30);
   ray[0].color = sf::Color(255,255,255,30);
   rwind.draw(ray);
  }

What am I doing wrong?

Also a couple of side questions:

1. Why do you multiply by -d and not just d ( distance ) when calculating with sin and cos?
2. Does this technique look right considering it's supposed to transition into fake 3D using 2D raycasting ( Given that the final ray distance is determined on where it hit a wall ).

Kind regards
Moonkis


EDIT:

Realized that the offset was due to me accidentally subtracting the subsequent ray angle when calculating the offset using players FOV
Changed it from this:
double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );
into this:
double rayAngle = ( player.rot -  (player.fov/2)  ) + subsequentRayAngleIncrease*i;

It seems to work as intended now, checked using 90, 180, 270 and 360 FOV angles. ( 180 and 90 was the ones I based it on working, they looked as intended, also added some wrapping when calculated angles for the rx and ry:
if( rayAngle == 360 ) rayAngle = 0;
if( rayAngle > 360 )
{
    rayAngle = ( rayAngle - 360 );
}
if( rayAngle < 0 )
{
    rayAngle = ( 360 + rayAngle );
}

Seeing as I only like to work with the angels 0 - 369 ( sin(to_radiance(360)) and sin(to_radiance(0)) gave different angles where as 0 degrees to radians was the most correct one.

#3Moonkis

Posted 29 November 2012 - 10:40 AM

I'v been trying and studying a hell of a lot for making my 2D raycasting, I'v been visiting KahnAcademy for Trigonometry-lessons as well as reading various topics ( each with a completely different method of achieving it. )

I'v chosen to stick with the topic found here

Currently I'm just trying to get the ray-casting and movement to work, so far so good, almost.
I'm not entirely sure of how to do the casting, but I got something almost working, though it's a bit off. Here is how it looks so far:
Posted Image

The blue/magenta arrow is the direction the player will travel ( current angle ) and the white rays are supposed to be the ray-casting, as you can see it's off by quite a bit and I can't understand why.

Here is my current casting code:
  sf::VertexArray ray(sf::Lines, 2);
  const int travelingDistans = 400;
  double subsequentRayAngleIncrease = player.fov/w;
  // Start from column 0 to the X't column of the viewing plane.
  // I'm not sure what the viewing plane is but I'm guessing it's the screen.
  // Which would mean from 0 to our screens highest x, which is 800.
  // Since the resolution is 800x600
  for( int i = 0; i < w; i++)
  {
   double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );

   double rx =  player.x - travelingDistans * std::cos(Math::to_radians(rayAngle));
   double ry =  player.y - travelingDistans * std::sin(Math::to_radians(rayAngle));
   // + 5 is so it's centered of the players origin
   ray[0] = sf::Vector2f( player.x + 5 , player.y + 5);
   ray[1] = sf::Vector2f( rx + 5, ry +5 );
   ray[1].color = sf::Color(255,255,255,30);
   ray[0].color = sf::Color(255,255,255,30);
   rwind.draw(ray);
  }

What am I doing wrong?

Also a couple of side questions:

1. Why do you multiply by -d and not just d ( distance ) when calculating with sin and cos?
2. Does this technique look right considering it's supposed to transition into fake 3D using 2D raycasting ( Given that the final ray distance is determined on where it hit a wall ).

Kind regards
Moonkis


EDIT:

Realized that the offset was due to me accidentally subtracting the subsequent ray angle when calculating the offset using players FOV
Changed it from this:
double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );
into this:
double rayAngle = ( player.rot -  (player.fov/2)  ) + subsequentRayAngleIncrease*i;

It seems to work as intended now, checked using 90, 180, 270 and 360 FOV angles. ( 180 and 90 was the ones I based it on working, they looked as intended, also added some wrapping when calculated angles for the rx and ry:
if( rayAngle == 360 ) rayAngle = 0;
if( rayAngle > 360 )
{
    rayAngle = ( rayAngle - 360 );
}
if( rayAngle < 0 )
{
    rayAngle = ( 360 + rayAngle );
}

Seeing as I only like to work with the angels 0 - 369 ( sin(to_radiance(360)) and sin(to_radiance(0)) gave different angles where as 0 degrees to radians was the most correct one.

#2Moonkis

Posted 29 November 2012 - 10:39 AM

I'v been trying and studying a hell of a lot for making my 2D raycasting, I'v been visiting KahnAcademy for Trigonometry-lessons as well as reading various topics ( each with a completely different method of achieving it. )

I'v chosen to stick with the topic found here

Currently I'm just trying to get the ray-casting and movement to work, so far so good, almost.
I'm not entirely sure of how to do the casting, but I got something almost working, though it's a bit off. Here is how it looks so far:
Posted Image

The blue/magenta arrow is the direction the player will travel ( current angle ) and the white rays are supposed to be the ray-casting, as you can see it's off by quite a bit and I can't understand why.

Here is my current casting code:
  sf::VertexArray ray(sf::Lines, 2);
  const int travelingDistans = 400;
  double subsequentRayAngleIncrease = player.fov/w;
  // Start from column 0 to the X't column of the viewing plane.
  // I'm not sure what the viewing plane is but I'm guessing it's the screen.
  // Which would mean from 0 to our screens highest x, which is 800.
  // Since the resolution is 800x600
  for( int i = 0; i < w; i++)
  {
   double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );

   double rx =  player.x - travelingDistans * std::cos(Math::to_radians(rayAngle));
   double ry =  player.y - travelingDistans * std::sin(Math::to_radians(rayAngle));
   // + 5 is so it's centered of the players origin
   ray[0] = sf::Vector2f( player.x + 5 , player.y + 5);
   ray[1] = sf::Vector2f( rx + 5, ry +5 );
   ray[1].color = sf::Color(255,255,255,30);
   ray[0].color = sf::Color(255,255,255,30);
   rwind.draw(ray);
  }

What am I doing wrong?

Also a couple of side questions:

1. Why do you multiply by -d and not just d ( distance ) when calculating with sin and cos?
2. Does this technique look right considering it's supposed to transition into fake 3D using 2D raycasting ( Given that the final ray distance is determined on where it hit a wall ).

Kind regards
Moonkis


EDIT:

Realized that the offset was due to me accidentally subtracting the subsequent ray angle when calculating the offset using players FOV
Changed it from this:
double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );
into this:
double rayAngle = ( player.rot -  (player.fov/2)  ) + subsequentRayAngleIncrease*i;

It seems to work as intended now, checked using 90, 180, 270 and 360 FOV angles. ( 180 and 90 was the ones I based it on working, they looked as intended, also added some wrapping when calculated angles for the rx and ry:
if( rayAngle == 360 ) rayAngle = 0;
if( rayAngle > 360 )
{
    rayAngle = ( rayAngle - 360 );
}
if( rayAngle < 0 )
{
    rayAngle = ( 360 + rayAngle );
}

Seeing as I only like to work with the angels 0 - 369 ( sin(to_radiance(360)) and sin(to_radiance(0)) gave different angles where as 0 degrees to radians was the most correct one.

#1Moonkis

Posted 29 November 2012 - 09:30 AM

I'v been trying and studying a hell of a lot for making my 2D raycasting, I'v been visiting KahnAcademy for Trigonometry-lessons as well as reading various topics ( each with a completely different method of achieving it. )

I'v chosen to stick with the topic found here

Currently I'm just trying to get the ray-casting and movement to work, so far so good, almost.
I'm not entirely sure of how to do the casting, but I got something almost working, though it's a bit off. Here is how it looks so far:
Posted Image

The blue/magenta arrow is the direction the player will travel ( current angle ) and the white rays are supposed to be the ray-casting, as you can see it's off by quite a bit and I can't understand why.

Here is my current casting code:
  sf::VertexArray ray(sf::Lines, 2);
  const int travelingDistans = 400;
  double subsequentRayAngleIncrease = player.fov/w;
  // Start from column 0 to the X't column of the viewing plane.
  // I'm not sure what the viewing plane is but I'm guessing it's the screen.
  // Which would mean from 0 to our screens highest x, which is 800.
  // Since the resolution is 800x600
  for( int i = 0; i < w; i++)
  {
   double rayAngle = player.rot - ( (player.fov/2) + subsequentRayAngleIncrease*i );
  
   double rx =  player.x - travelingDistans * std::cos(Math::to_radians(rayAngle));
   double ry =  player.y - travelingDistans * std::sin(Math::to_radians(rayAngle));
   // + 5 is so it's centered of the players origin
   ray[0] = sf::Vector2f( player.x + 5 , player.y + 5);
   ray[1] = sf::Vector2f( rx + 5, ry +5 );
   ray[1].color = sf::Color(255,255,255,30);
   ray[0].color = sf::Color(255,255,255,30);
   rwind.draw(ray);
  }

What am I doing wrong?

Also a couple of side questions:

1. Why do you multiply by -d and not just d ( distance ) when calculating with sin and cos?
2. Does this technique look right considering it's supposed to transition into fake 3D using 2D raycasting ( Given that the final ray distance is determined on where it hit a wall ).

Kind regards
Moonkis

PARTNERS