Home   Help Search Login Register  

Author Topic: Vector functions  (Read 2712 times)

0 Members and 1 Guest are viewing this topic.

Offline MachoMan

  • Honoured
  • Former Staff
  • ****
  • KISS, Keep it Simple Stupid
Vector functions
« 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:
functions.

Length:
Code: [Select]
//         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:
Code: [Select]
//         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
Code: [Select]
//         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
Code: [Select]
//         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.
 
« Last Edit: 26 Nov 2004, 14:41:00 by MachoMan »
Get those missions out there you morons!

sa8gecko

  • Guest
Re:Vector functions
« Reply #1 on: 02 Dec 2004, 06:37:47 »
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.

Offline MachoMan

  • Honoured
  • Former Staff
  • ****
  • KISS, Keep it Simple Stupid
Re:Vector functions
« Reply #2 on: 02 Dec 2004, 10:13:03 »
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 :
Code: [Select]
//         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  :-\
« Last Edit: 02 Dec 2004, 10:43:52 by MachoMan »
Get those missions out there you morons!

Unnamed

  • Guest
Re:Vector functions
« Reply #3 on: 02 Dec 2004, 23:19:44 »
Is there any reason why you used:

Code: [Select]
_squaredLength = (_x*_x + _y*_y + _z*_z);
Instead of:

Code: [Select]
_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
« Last Edit: 02 Dec 2004, 23:20:45 by Unnamed »

Offline MachoMan

  • Honoured
  • Former Staff
  • ****
  • KISS, Keep it Simple Stupid
Re:Vector functions
« Reply #4 on: 03 Dec 2004, 09:32:43 »
Quote
Is there any reason why you used .....
Power of habbit I guess:
With Java ^2 doesn't work on floating points, only on integers.

Quote
I understand length.sqf, it could convert the output of the velocity command into speed m/s?
True, but it has many other uses.

Quote
But I dont have a clue what the others do, could you explain?
Dot-product lets u calculate the angle between two vectors.
Cross-product 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)
« Last Edit: 03 Dec 2004, 09:43:06 by MachoMan »
Get those missions out there you morons!

Unnamed

  • Guest
Re:Vector functions
« Reply #5 on: 03 Dec 2004, 11:20:55 »
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

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Vector functions
« Reply #6 on: 11 Dec 2004, 13:21:54 »
Quote
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.

Unnamed

  • Guest
Re:Vector functions
« Reply #7 on: 11 Dec 2004, 17:38:25 »
Quote
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.

Quote
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