Home   Help Search Login Register  

Author Topic: How to calculate the direction between object and object?  (Read 494 times)

0 Members and 1 Guest are viewing this topic.

kevind2003

  • Guest
Hi,

Can someone teach me how to get the direction between 2 objects by the following elements? or any command can do it?

position of 2 objects
distance between 2 objects
direction of object A

Thank you very much.

deaddog

  • Guest
Re:How to calculate the direction between object and object?
« Reply #1 on: 30 Dec 2003, 23:10:23 »
The distance between objects is easy:

_dis=obj1 distance obj2

The direction requires a bit of calculation.  There is probably an easier way but this is how I do it, using a function.

Make a file called "direction.sqf"
Code: [Select]
private ["_ux","_uy","_dx","_dy","_diffx","_diffy"];

_ux = (_this select 0) select 0;
_uy = (_this select 0) select 1;
_dx = (_this select 1) select 0;
_dy = (_this select 1) select 1;

_diffx = (_dx-_ux);
if (_diffx==0) then {_diffx=.001};

_diffY = (_dy-_uy);

_dir = atan( _diffy/_diffx );

if (_diffx>0) then {_dir=90-_dir};
if (_diffx<0) then {_dir=270-_dir};
_dir

In your init.sqs file, put this line:
Code: [Select]
direction = preprocessfile "direction.sqf"
This actually finds the direction from one position to another position, not actually objects.  To use, do this:

_dir=[getpos object1,getpos object2] call direction

_dir will be the direction of object1 to object2.

Like I said, there is probably a shorter way but this works.

kevind2003

  • Guest
Re:How to calculate the direction between object and object?
« Reply #2 on: 30 Dec 2003, 23:15:16 »
thanks for your quick response and nice help  :)