From 048a6afde99b0a60e7e8e33625ebb800f304fb83 Mon Sep 17 00:00:00 2001 From: LorenLuke Date: Tue, 6 Mar 2018 10:45:10 -0800 Subject: [PATCH 1/5] Create vectorRotate3D.sqf File for rotating one vector about a second vector as a rotation axis by a set angle measure. --- addons/vectors/fnc_vectorRotate3D.sqf | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 addons/vectors/fnc_vectorRotate3D.sqf diff --git a/addons/vectors/fnc_vectorRotate3D.sqf b/addons/vectors/fnc_vectorRotate3D.sqf new file mode 100644 index 000000000..1197ffd9f --- /dev/null +++ b/addons/vectors/fnc_vectorRotate3D.sqf @@ -0,0 +1,34 @@ +/* + * Author: LorenLuke + * Rotates the first vector around the second, clockwise by angle theta + * + * Arguments: + * 0: Vector + * 1: Rotation Axis + * 2: Angle (degrees) + * + * Return Value: + * Transformed Vector + * + * [weaponDir player, [0,0,1], 25] call CBA_fnc_vectorRotate3D + * + * Public: No + */ +#include "script_component.hpp" + +params ["vector", "_rotationAxis", "_theta"]; + +private _normalVector = vectorNormalized _rotationAxis; +private = _s_theta = sin(_theta); +private = _c_theta = cos(_theta); + +// Rodrigues Rotation Formula; +// https://wikimedia.org/api/rest_v1/media/math/render/svg/2d63efa533bdbd776434af1a7af3cdafaff1d578 +private _returnVector = + (_vector1 vectorMultiply _c_theta) vectorAdd + ((_normalVector vectorCrossProduct _vector1) vectorMultiply _s_theta) vectorAdd + ( + _normalVector vectorMultiply ((_normalVector vectorDotProduct _vector1) * (1 - _c_theta)) + ); + +_returnVector; From d91502de71e0d8bf863dc6626f562d3a3fadfc65 Mon Sep 17 00:00:00 2001 From: LorenLuke Date: Tue, 6 Mar 2018 11:17:55 -0800 Subject: [PATCH 2/5] Reformats header and fixes typos. --- ...ectorRotate3D.sqf => fnc_vectRotate3D.sqf} | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) rename addons/vectors/{fnc_vectorRotate3D.sqf => fnc_vectRotate3D.sqf} (55%) diff --git a/addons/vectors/fnc_vectorRotate3D.sqf b/addons/vectors/fnc_vectRotate3D.sqf similarity index 55% rename from addons/vectors/fnc_vectorRotate3D.sqf rename to addons/vectors/fnc_vectRotate3D.sqf index 1197ffd9f..db0711e5b 100644 --- a/addons/vectors/fnc_vectorRotate3D.sqf +++ b/addons/vectors/fnc_vectRotate3D.sqf @@ -1,26 +1,22 @@ -/* - * Author: LorenLuke - * Rotates the first vector around the second, clockwise by angle theta - * - * Arguments: - * 0: Vector - * 1: Rotation Axis - * 2: Angle (degrees) - * - * Return Value: - * Transformed Vector - * - * [weaponDir player, [0,0,1], 25] call CBA_fnc_vectorRotate3D - * - * Public: No - */ + /* ---------------------------------------------------------------------------- +Function: CBA_fnc_vectRotate3D +Description: + Rotates the first vector around the second, clockwise by theta degrees + +Parameters: + Vector, Rotation Axis, Angle +Returns: + The rotated vector +Author: + LorenLuke +---------------------------------------------------------------------------- */ #include "script_component.hpp" params ["vector", "_rotationAxis", "_theta"]; private _normalVector = vectorNormalized _rotationAxis; -private = _s_theta = sin(_theta); -private = _c_theta = cos(_theta); +private _s_theta = sin(_theta); +private _c_theta = cos(_theta); // Rodrigues Rotation Formula; // https://wikimedia.org/api/rest_v1/media/math/render/svg/2d63efa533bdbd776434af1a7af3cdafaff1d578 From cdd2f3657543837fcc4db7547be63fe4d6a7cfd6 Mon Sep 17 00:00:00 2001 From: LorenLuke Date: Tue, 6 Mar 2018 11:27:14 -0800 Subject: [PATCH 3/5] Added new SUPER! header --- addons/vectors/fnc_vectRotate3D.sqf | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/addons/vectors/fnc_vectRotate3D.sqf b/addons/vectors/fnc_vectRotate3D.sqf index db0711e5b..1b1bcd562 100644 --- a/addons/vectors/fnc_vectRotate3D.sqf +++ b/addons/vectors/fnc_vectRotate3D.sqf @@ -1,12 +1,21 @@ - /* ---------------------------------------------------------------------------- -Function: CBA_fnc_vectRotate3D +/* ---------------------------------------------------------------------------- +Function: CBA_vectors_fnc_vectRotate3D Description: - Rotates the first vector around the second, clockwise by theta degrees - + Rotates the first vector around the second, clockwise by theta degrees. Parameters: - Vector, Rotation Axis, Angle + _vector - 3D vector that is to be rotated + _rotationAxis - 3D vector that the first argument is rotated around + _theta - Angle, in degrees clockwise, about which the first vector is rotated Returns: - The rotated vector + _returnVector - 3D vector returned after rotation +Examples: + (begin example) + //Rotate 25 degrees right of player weapon direction; + [weaponDirection player, [0,0,1], 25] call CBA_vectors_fnc_vectRotate3D; + + //Pitch a projectile's velocity down 10 degrees; + [velocity _projectile, (velocity _projectile) vectorCrossProduct [0,0,1], 10] call CBA_vectors_fnc_vectRotate3D; + (end) Author: LorenLuke ---------------------------------------------------------------------------- */ From 65c669f99aa92dacec2baaa4818f21264b6c5d98 Mon Sep 17 00:00:00 2001 From: LorenLuke Date: Tue, 6 Mar 2018 11:42:08 -0800 Subject: [PATCH 4/5] Adds Tests --- addons/vectors/test_vectors.sqf | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/addons/vectors/test_vectors.sqf b/addons/vectors/test_vectors.sqf index 42167cac5..7431bcc33 100644 --- a/addons/vectors/test_vectors.sqf +++ b/addons/vectors/test_vectors.sqf @@ -247,6 +247,33 @@ _result = [[5,-5],[-10,0],-270] call CBA_fnc_vectRotate2D; _expected = [0,-20]; TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals,_fn); +// UNIT TESTS (vectRotate3D) +_fn = "CBA_fnc_vectRotate3D"; +TEST_DEFINED("CBA_fnc_vectRotate3D",""); + +_result = [[0,0,1],[0,0,1],0] call CBA_fnc_vectRotate3D; +_expected = [0,0,1]; +TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals,_fn); + +_result = [[0,0,1],[0,0,1],90] call CBA_fnc_vectRotate3D; +_expected = [0,0,1]; +TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals,_fn); + +_result = [[0,1,0],[0,0,-1],90] call CBA_fnc_vectRotate3D; +_expected = [1,0,0]; +TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals,_fn); + +_result = [[0,1,0],[0,0,-10],90] call CBA_fnc_vectRotate3D; +_expected = [1,0,0]; +TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals,_fn); + +_result = [[0,1,0],[1,0,0],90] call CBA_fnc_vectRotate3D; +_expected = [0,0,1]; +TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals,_fn); + +_result = [[0,1,0],[0,0,1],-45] call CBA_fnc_vectRotate3D; +_expected = [0,sqrt(2)/2,sqrt(2)/2]; +TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals,_fn); // UNIT TESTS (vectSubtract) _fn = "CBA_fnc_vectSubtract"; @@ -269,4 +296,4 @@ _result = _temp call CBA_fnc_polar2vect; TEST_TRUE([ARR_2(_result,_expected)] call _fnc_vectorEquals, "complex polar 2"); -nil; \ No newline at end of file +nil; From 07174a7148f016fc4f783b9520d03f82aca0a981 Mon Sep 17 00:00:00 2001 From: commy2 Date: Tue, 6 Mar 2018 21:58:29 +0100 Subject: [PATCH 5/5] update formatting, camel case varnames, fixed undefined variable --- addons/vectors/fnc_vectRotate3D.sqf | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/addons/vectors/fnc_vectRotate3D.sqf b/addons/vectors/fnc_vectRotate3D.sqf index 1b1bcd562..9de4e1a78 100644 --- a/addons/vectors/fnc_vectRotate3D.sqf +++ b/addons/vectors/fnc_vectRotate3D.sqf @@ -1,13 +1,17 @@ /* ---------------------------------------------------------------------------- Function: CBA_vectors_fnc_vectRotate3D + Description: Rotates the first vector around the second, clockwise by theta degrees. + Parameters: _vector - 3D vector that is to be rotated _rotationAxis - 3D vector that the first argument is rotated around _theta - Angle, in degrees clockwise, about which the first vector is rotated + Returns: _returnVector - 3D vector returned after rotation + Examples: (begin example) //Rotate 25 degrees right of player weapon direction; @@ -16,24 +20,20 @@ Examples: //Pitch a projectile's velocity down 10 degrees; [velocity _projectile, (velocity _projectile) vectorCrossProduct [0,0,1], 10] call CBA_vectors_fnc_vectRotate3D; (end) + Author: LorenLuke ---------------------------------------------------------------------------- */ #include "script_component.hpp" -params ["vector", "_rotationAxis", "_theta"]; +params ["_vector", "_rotationAxis", "_theta"]; private _normalVector = vectorNormalized _rotationAxis; -private _s_theta = sin(_theta); -private _c_theta = cos(_theta); +private _sinTheta = sin _theta; +private _cosTheta = cos _theta; // Rodrigues Rotation Formula; // https://wikimedia.org/api/rest_v1/media/math/render/svg/2d63efa533bdbd776434af1a7af3cdafaff1d578 -private _returnVector = - (_vector1 vectorMultiply _c_theta) vectorAdd - ((_normalVector vectorCrossProduct _vector1) vectorMultiply _s_theta) vectorAdd - ( - _normalVector vectorMultiply ((_normalVector vectorDotProduct _vector1) * (1 - _c_theta)) - ); - -_returnVector; +(_vector vectorMultiply _cosTheta) vectorAdd +((_normalVector vectorCrossProduct _vector) vectorMultiply _sinTheta) vectorAdd +(_normalVector vectorMultiply ((_normalVector vectorDotProduct _vector) * (1 - _cosTheta)))