OFPEC Forum
Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting Resources Beta Testing & Submission => Topic started by: MachoMan on 26 Nov 2004, 14:10:37
-
Hi all,
I was working on a dust storm script and noticed a needed a few functions. I got a bit carried away and ended up with a couple of simple yet handy functions. ;D
I made a little pack of .sqf files, for Vector and Point calculus.
It contains:
- dot-product (http://mathworld.wolfram.com/DotProduct.html)
- cross-product (http://mathworld.wolfram.com/CrossProduct.html)
- length
- normalize
functions.
Length:
// MachoMan's :
// ===============
// length.sqf
// ===============
// calculates the length of a (3d) Vector.
// Can alsoo be used to calculate distances in 3d, very fast and accurate.
_x = this select 0;
_y = this select 1;
_z = this select 2;
_squaredLength = (_x*_x + _y*_y + _z*_z);
//return
sqrt _squaredLength
Normalize:
// MachoMan's :
// ===============
// normalize.sqf
// ===============
// Normalizes a 3d Vector
// Requires length.sqf to be preprocessed.
// Usage: someVector = [x,y,z] call normalize
_x = _this select 0;
_y = _this select 1;
_z = _this select 2;
_length = _this call length;
_invLength = (1.0 / _length);
_x = _x * _invLength;
_y = _y * _invLength;
_z = _z * _invLength;
//return
[_x, _y, _z]
Dot-product
// MachoMan's :
// ================
// dotProduct.sqf
// ================
//Use : [[x1,y1,z1],[x2,y2,z2]] call dotProduct.
_a = _this select 0
_b = _this select 1
_a = _a call normalize
_b = _b call normalize
//a
_x1 = _a select 0;
_y1 = _a select 1;
_z1 = _a select 2;
//b
_x2 = _b select 0;
_y2 = _b select 1;
_z2 = _b select 2;
//return
(x1*x2+y1*y2+z1*z2)
Cross-product
// MachoMan's :
// ================
// crossProduct.sqf
// ================
//Use : [[x1,y1,z1],[x2,y2,z2]] call crossProduct.
_a = _this select 0
_b = _this select 1
_a = _a call normalize
_b = _b call normalize
//a
_x1 = _a select 0;
_y1 = _a select 1;
_z1 = _a select 2;
//b
_x2 = _b select 0;
_y2 = _b select 1;
_z2 = _b select 2;
[(y1*z2-z1*y2),(z1*x2-x1*z2),(x1*y2-y1*x2)]
Please give me as much comment as possible, feel free to request more Vector / Point functions.
-
pay attention to evitate 'division by zero' errors in normalize.sqf
It shouldn't happen, but if the passed vector is a null vector this will
generate the error. Since normalization is even used to calculate direction,
the vector passed could be a difference between two points or vectors,
and if happens that the points coincide, this would generate the error.
-
true, hmm wil try to fix that ...
Now I'm wishing ofp had "try" & "catch" functions.
Fortunately I have another idea. :)
So this is the new version :
// MachoMan's :
// ===============
// normalize.sqf
// ===============
// Normalizes a 3d Vector
// Requires length.sqf to be preprocessed.
// Usage: someVector = [x,y,z] call normalize
_x = _this select 0;
_y = _this select 1;
_z = _this select 2;
_length = _this call length;
if (_length != 0) then
{
_invLength = (1.0 / _length);
_x = _x * _invLength;
_y = _y * _invLength;
_z = _z * _invLength;
_a = [_x, _y, _z];
};
else { _a = [0,0,0]};
//return
_a
I haven't had a chance to test it though, the script using it isn't really finished yet :-\
-
Is there any reason why you used:
_squaredLength = (_x*_x + _y*_y + _z*_z);
Instead of:
_squaredLength = (_x^2 + _y^2 + _z^2);
Oh and could you add the private command to each function, _x e.t.c could be used elsewhere.
Cheers, now onto the serious stuff.
I understand length.sqf, it could convert the output of the velocity command into speed m/s?
But I dont have a clue what the others do, could you explain?
Thanks
-
Is there any reason why you used .....
Power of habbit I guess:
With Java ^2 doesn't work on floating points, only on integers.
I understand length.sqf, it could convert the output of the velocity command into speed m/s?
True, but it has many other uses.
But I dont have a clue what the others do, could you explain?
Dot-product (http://mathworld.wolfram.com/DotProduct.html) lets u calculate the angle between two vectors.
Cross-product (http://mathworld.wolfram.com/CrossProduct.html) will give you a vector which is perpendiculair to both vectors you provided.
But you'll notice how usefull they are when I release something I'm not gonna tell you all about. 8)
-
I already make heavy use of Trigonometry :) but it's always nice to know more efficient ways. I'd be pushing it to find a use for the cross-product at the moment, but the Vector Algebra section of the links you posted should keep me busy for a while.
Cheers
-
Is there any reason why you used:
In most language implementations x*x is quicker to execute than x^2 (though you do need to have a lot of them before you notice much difference). The reason being that the power function (^) needs to be capable of working with any number following it.
-
In most language implementations x*x is quicker to execute than x^2
I've been getting some lag on scripts using distance calculations with x^2 in a loop, so I will give it a try. Along with the Dot-Product function to replace some of the Trigonometry I've been using.
though you do need to have a lot of them before you notice much difference
I'll let you know if it has any obvious effect, but I'm happy with at least the idea of doing it as quickly as possible.
Cheers