Advertisement
Guest User

Direction to Rotation

a guest
Dec 6th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. Vector3 DirToRot(Vector3 Dir)
  2.         {
  3.             try
  4.             {
  5.                 /*
  6.                 This is rotation to direction:
  7.                 rotx = 0.2
  8.                 retx = 0.2 *  0.0174532924 = 0.00349065848 //radians to degrees
  9.                 Cos(0.2 *  0.0174532924) = 0.9999939076578741
  10.                 absx = Abs(Cos(0.2 *  0.0174532924)) = 0.9999939076578741
  11.  
  12.                 rotz = 0.3
  13.                 retz = 0.3 * 0.0174532924 = 0.00523598772 //radians to degrees
  14.  
  15.                 dirx = -Sin(retz) * absx =  -0.005235963795437085 * 0.9999939076578741 = -0.0052359318961542843713968514009985 //-0.0052359318961542846 if rounded
  16.                 diry = Cos(retz) * absx = 0.9999862922476151 * 0.9999939076578741 = 0.999980199989
  17.                 dirz = Sin(retx) = 0.0052359637954370846088922220789420743196847079456760
  18.  
  19.                 now this is direction to rotation:
  20.                 dirz = Sin(retx) =  0.0052359637954370846088922220789420743196847079456760
  21.                 num1 = retx = Asin(dirz)
  22.                 rotx = num1 / 0.0174532924
  23.  
  24.                 num2 = absx = Abs(Cos(num1)
  25.                 num3 = diry / num2 = Cos(retz)
  26.                 num4 = retz = Acos(num3)
  27.                 rotz = num4 / 0.0174532924
  28.  
  29.                 roty?
  30.  
  31.  
  32.                 */
  33.                 float dirz = Dir.Z;
  34.                 float num1 = (float)Math.Asin(dirz);
  35.                 float rotx = num1 / 0.0174532924f;
  36.                
  37.                 float dirx = Dir.X;
  38.                 float num2 = (float)Math.Cos(num1);
  39.                 float num3 = dirx / num2;
  40.                 float num4 = (float)Math.Asin(-num3);
  41.                 float rotz1 = num4 / 0.0174532924f;
  42.  
  43.                 float diry = Dir.Y;
  44.                 float num5 = (float)Math.Cos(num1);
  45.                 float num6 = diry / num5;
  46.                 float num7 = (float)Math.Acos(num6);
  47.                 float rotz2 = num7 / 0.0174532924f;
  48.                
  49.                 if (rotz1 > 0)
  50.                 {
  51.                     if (rotz2 < 90)
  52.                     {
  53.                         trueRotZ = rotz1; //trueRotZ is a global float variable.
  54.                     }
  55.                     else
  56.                     {
  57.                         trueRotZ = rotz2;
  58.                     }
  59.                 }
  60.                 else
  61.                 {
  62.                     if (rotz2 < 90)
  63.                     {
  64.                         trueRotZ = rotz1;
  65.                     }
  66.                     else
  67.                     {
  68.                         trueRotZ = -rotz2;
  69.                     }
  70.                 }
  71.  
  72.                 return new Vector3(rotx, 0, trueRotZ);
  73.             }
  74.             catch
  75.             {
  76.                 return Vector3.Zero;
  77.             }
  78.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement