1
0

fn_rotateObject.sqf 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Rotate an object around each axis
  3. By bapedibupa
  4. Parameters: [object, [x,y,z]]
  5. Returns: nothing
  6. Rotates an object, giving it the specified rotation angle around each axis, in degrees.
  7. The rotation is about the world coordinates. When the object you want to rotate is attached to another object, it rotates relative to this coordinates (it doesent rotates around the axis from the rotating object).
  8. A positive number rotates the object in positive axis rotation. Negative numbers in the other direction.
  9. Positive axis rotation means when you take your right hand, make a fist and spread out your thumb, make sure that the thumb is heading in positive axis direction: Your other fingers show you now the positive rotation around this axis.
  10. Default value [0,0,0] make the object face straight north.
  11. If you want to face it straight west so you want it to turn 90° in positive axis rotation around Z. Your array parameters should look like this [0,0,90].
  12. If you want to face it straight east so you want it to turn 90° in negative axis rotation around Z. Your array parameters should look like this [0,0,-90].
  13. All axis work in the same way. Sure you can do this with the setDir command, but not in 3D.
  14. You also can combine a rotation around X, Y and Z. But then you need a good visual thinkimg or the trial and error methode ;) Note, that the object rotates first around X, then Y and in the end Z.
  15. This function is useful when you want to place an object on the map or on a attached object in the direction you want. If you use it in combination with attachTo use first the attachTo command an then this function.
  16. If you want to use this function just to look which parameters you should fill in the setVectorDirAndUp command (whenever you use it), just uncomment the hintSilent in the end and you can see the parameters in the game.
  17. Example: [object[45,0,-90]] call rotateObject; // rotates 45° back and 90° right (east).
  18. */
  19. _object = _this select 0;
  20. _rotations = _this select 1;
  21. _aroundX = _rotations select 0;
  22. _aroundY = _rotations select 1;
  23. _aroundZ = _rotations select 2;
  24. // set default values
  25. _dirX = 0;
  26. _dirY = 1;
  27. _dirZ = 0;
  28. _upX = 0;
  29. _upY = 0;
  30. _upZ = 1;
  31. // rotate around X
  32. if (_aroundX != 0) then {
  33. _dirY = cos _aroundX;
  34. _dirZ = sin _aroundX;
  35. _upY = -sin _aroundX;
  36. _upZ = cos _aroundX;
  37. };
  38. // rotate around Y
  39. if (_aroundY != 0) then {
  40. _dirX = _dirZ * sin _aroundY;
  41. _dirZ = _dirZ * cos _aroundY;
  42. _upX = _upZ * sin _aroundY;
  43. _upZ = _upZ * cos _aroundY;
  44. };
  45. // rotate around Z
  46. if (_aroundZ != 0) then {
  47. _dirXTemp = _dirX;
  48. _dirX = (_dirY * -sin _aroundZ) + (_dirXTemp* cos _aroundZ);
  49. _dirY = (_dirY * cos _aroundZ) + (_dirXTemp * sin _aroundZ);
  50. _upXTemp = _upX;
  51. _upX = (_upY * -sin _aroundZ) + (_upXTemp * cos _aroundZ);
  52. _upY = (_upY * cos _aroundZ) + (_upXTemp * sin _aroundZ);
  53. };
  54. // round
  55. _dirX = [_dirX,3] call BIS_fnc_cutDecimals;
  56. _dirY = [_dirY,3] call BIS_fnc_cutDecimals;
  57. _dirZ = [_dirZ,3] call BIS_fnc_cutDecimals;
  58. _upX = [_upX,3] call BIS_fnc_cutDecimals;
  59. _upY = [_upY,3] call BIS_fnc_cutDecimals;
  60. _upZ = [_upZ,3] call BIS_fnc_cutDecimals;
  61. // set vector dir and up
  62. _dir = [_dirX,_dirY,_dirZ];
  63. _up = [_upX,_upY,_upZ];
  64. //hintSilent format ["dir: %1 up: %2",_dir,_up];
  65. _object setVectorDirAndUp [_dir,_up];