Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMoonkis

Posted 28 November 2012 - 04:27 AM

I'm currently trying to build an 2D ray casting engine, however I'm on the fly learning the trigonometry as well.
I'm currently working my head around angles in degrees and radians.

I'm trying to use 0-360 degrees system that when the double reaches a value of 0 or 360 it wraps around so lets say the angle is increased/decreased by 90.

Going from 360 + 90 = 90
Going from 0 - 90 = 270



This works when trying to move with cos and sin for the angular movement. But I realized that 0* and 360* is not the same value for sin ( cos 0 and 360 is equal to 0 ).

sin(0*) = 0
sin(360*) = -1 * 10^-13



While I realize that this number is incredible small and wouldn't make a noticeable difference, it's still a factor that can change the output of movement.

When checking a real time example of it ( Example Movement ):

y = 5
sin(0*(Pi/180) )*(-6) = 6*10^-13
sin(360*(Pi/1780) )*(-6) = 0



It's obvious that these are not the same, though the calculator and computer outputs the same number if it was supposed to add to y, say: 5 + 0 = 5 and 5 + 6*10^-13 = 5 ( Computer output and Ti-84+ Calculator ).



But when trying to compare these as equal each other in an if statement it did not validate as true, so there is a very small difference.

My current wrap-around code looks like this:
  if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
  {
   player.rot -= 5;
   if( player.rot < 0 )
   {
	player.rot = ( 360 + player.rot );
   }
  }
  if( sf::Keyboard::isKeyPressed( sf::Keyboard:<img src='http://public.gamedev.net//public/style_emoticons/default/biggrin.png' class='bbc_emoticon' alt=':D' /> ) )
  {
   player.rot += 5;
   if( player.rot > 360 )
   {
	player.rot = ( player.rot - 360 );
   }
  }

So, is this going to be a problem in the future or is it so small that it's negligible? After all it's only a problem on the 0 and 360 degrees.

* Converted to radians under the hood when inserted into cos or sin functions.

#1Moonkis

Posted 28 November 2012 - 04:24 AM

I'm currently trying to build an 2D ray casting engine, however I'm on the fly learning the trigonometry as well.
I'm currently working my head around angles in degrees and radians.

I'm trying to use 0-360 degrees system that when the double reaches a value of 0 or 360 it wraps around so lets say the angle is increased/decreased by 90.

Going from 360 + 90 = 90
Going from 0 - 90 = 270



This works when trying to move with cos and sin for the angular movement. But I realized that 0* and 360* is not the same value for sin ( cos 0 and 360 is equal to 0 ).

sin(0*) = 0
sin(360*) = -1 * 10^-13



While I realize that this number is incredible small and wouldn't make a noticeable difference, it's still a factor that can change the output of movement.

When checking a real time example of it ( Example Movement ):

y = 5
sin(0*(Pi/180) )*(-6) = 6*10^-13
sin(360*(Pi/1780) )*(-6) = 0



It's obvious that these are not the same, though the calculator and computer outputs the same number if it was supposed to add to y, say: 5 + 0 = 5 and 5 + 6*10^-13 = 5 ( Computer output and Ti-84+ Calculator ).



But when trying to compare these as equal each other in an if statement it did not validate as true, so there is a very small difference.

My current wrap-around code looks like this:
  if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
  {
   player.rot -= 5;
   if( player.rot < 0 )
   {
	player.rot = ( 360 + player.rot );
   }
  }
  if( sf::Keyboard::isKeyPressed( sf::Keyboard:[img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] ) )
  {
   player.rot += 5;
   if( player.rot > 360 )
   {
	player.rot = ( player.rot - 360 );
   }
  }



* Converted to radians under the hood when inserted into cos or sin functions.

PARTNERS