2

I'm trying to convert some rotational values in C# for Unity. I want to be able to let a user enter a value between 0 and 360 degrees and this value needs to be mapped to two axes, x and z. Both the x and z axes go from -1 to 1 and I'm wondering how to convert the degrees into the desired range. This is even more confusing since only both axes together determine the rotation.

The two axes come from Camera.forward.x and Camera.forward.z. I could of course just have values between -1 and 1 entered for both axes but it would be more natural to have a degree between 0 and 360 entered. How would I achieve this?

UPDATE:

My code now looks like this to convert the degrees into radians for x an z:

// rotation is a value between 0.0 and 360.0
float rad = rotation * Mathf.Deg2Rad;
float z = Mathf.Sin(rad);
float x = Mathf.Cos(rad);
Debug.Log(x + "   " + z);

However depending on the rotation value, the result isn't always between -1 and 1. For example:

  • If I set rotation to 0 I get x:1 z:0
  • If I set it to 360 I get x:1 z:1.748456E-07
  • If I set it to 90 I get x:-4.371139E-08 z:0

What's going on there?

UPDATED LOG:

rotation:0    rad:0         z:0              x:1
rotation:90   rad:1.570796  z:1              x:-4.371139E-08
rotation:180  rad:3.141593  z:-8.742278E-08  x:-1
rotation:360  rad:6.283185  z:1.748456E-07   x:1
BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • I think this could be usefull: [why-is-math-cos-returning-the-wrong-value](http://stackoverflow.com/questions/4168151/why-is-math-cos-returning-the-wrong-value) – Christian Holm Jørgensen Oct 19 '15 at 20:05
  • Can you add "rad" and "rotation" to your debug log. Something is wrong with your calculation of z at 90 degrees. z @ 90 = 1... not 0. – Rob S. Oct 20 '15 at 14:51

1 Answers1

3

for a unit circle (r = 1) where the rotation is in the x-z plane, you can determine x and z using the two parametric equations.

z = sin(0...360)

x = cos(0...360)

of course depending on how your camera's coordinates are labeled it may be exactly the opposite.

Edit: Also make sure your math functions are calculating in degree mode and not radians. If they are calculating in radians you can convert degrees to radians using the following formula.

rad = deg * (2*pi)/360

Rob S.
  • 1,044
  • 7
  • 25
  • But then I get radian value between 0 and 1.748456E-07 (for degree 0 to 360) and I still don't know how to map it into the range -1 to 1. – BadmintonCat Oct 19 '15 at 20:46
  • 1
    The `sin` and `cos` functions return values -1 to 1, given a radian value. – SimpleVar Oct 19 '15 at 20:55
  • 2
    then you are calculating incorrectly. the conversion for radians outputs a value between 0 and 2pi for all values between 0 and 360 degrees. – Rob S. Oct 19 '15 at 20:56
  • 1
    @RobS. Still having some issues with this. Can you check my updated edit above? – BadmintonCat Oct 20 '15 at 13:21
  • your output is correct 1.748456E-07 and -4.371139E-08 are both between -1 and 1 ----- 1.748456E-07 can also be represented as 0.0000001748456 and -4.371139E-08 can be represented as -0.00000004371139 which are both VERY close to zero... this has to do with how the math library you are using calculates sin and cos, they are not exact, they are estimates. Both of those values are actually zero, but just slightly off due to estimating errors. – Rob S. Oct 20 '15 at 14:03
  • at second glance the calculation for z at 90 deg is incorrect.. that should be one.... can you add the vars "rotation" and "rad" to your debug log? – Rob S. Oct 20 '15 at 14:13
  • @RobS. Ok, I've updated the log output above. Still haven't gotten behind what is wrong with the calculations. – BadmintonCat Oct 20 '15 at 16:27
  • All the calculations you show are correct. Your statement "If I set it to 90 I get x:-4.371139E-08 z:0" is therefore incorrect. I would suggest rounding your values to to 3 or 4 decimal places. See http://stackoverflow.com/questions/164926/c-sharp-how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-pa – Rob S. Oct 20 '15 at 16:32
  • You're right! Just saw that the value for 90 degrees is actually correct in the last test. I will look further into this. Anyway thanks for all the help! – BadmintonCat Oct 20 '15 at 16:39
  • No problem. If I answered your question please give it a check mark, otherwise let me know what I didn't answer. – Rob S. Oct 20 '15 at 16:46