OFPEC Forum

Editors Depot - Mission Editing and Scripting => Arma2 - Editing/Scripting General => Topic started by: johnnyboy on 14 Jul 2010, 17:12:32

Title: How do you calculate Velocity to move Object A to Object B in 3d space?
Post by: johnnyboy on 14 Jul 2010, 17:12:32
Hi guys,

I am terrible at math, and would really appreciate some help.

I want a script to move Object A to Object B, in 3D space.   3D is important, so I need the Z factor calculated.

My inputs would be:

1.  Target Object
2.  Object to move
3.  Speed of Object to move.

I think I have to use SetVelocity and SetVectorDir on the Object to move, but don't know how to calculate the values.

Below is my weak attempt.

Can anybody help  me with this?

Thanks!


Code: [Select]
_target_object = _this select 0;    // target object
_vehicle = _this select 1;          // object to move to target
_speed = _this select 2;           // desired speed of object to move

_targetPos = getpos(_target_object);
_speed = speed _vehicle;

// assume vehicle is facing target
_dir = getDir _veh;

// Calculate velocity x,y,z factors
_vx = (sin _dir)*_speed;
_vy = (cos _dir)*_speed;

_vy = ????    //// How do I cacluate Y value??

// I think this is how you move the vehicle toward the target
_vehicle setVectorDir[_vx/_vh, _vy/_vh, _vz/_vh];
_vehicle setVelocity [_vx, _vy, _vz];
Title: Re: How do you calculate Velocity to move Object A to Object B in 3d space?
Post by: CH on 19 Jul 2010, 13:45:31
Maybe this could be of some help to you as far as the math is concerned.

If A and B are two points in space with the following coordinates…

A=(d,e,f)
B=(g,h,i)

…and AB is a vector pointing from A to B, then

AB=(g-d,h-e,i-f)=(j,k,l)

The distance between A and B or the magnitude (in your case, speed) of AB is (j²+k²+l²)^(½).
Title: Re: How do you calculate Velocity to move Object A to Object B in 3d space?
Post by: LurchiDerLurch on 20 Jul 2010, 13:18:00
My script is exactly doing this. I don't know if it's perfect (respective mathematics etc) but it works:

http://www.ofpec.com/forum/index.php?topic=34855.0

Quote
[object,target,modelToWorld,speed,distance,correct,final]spawn float;


object: the object you want to move
target: unit or position (if you pass a unit the object will follow that unit)
modelToWorld: Array position from object model space f.e: [0,5,0] is 5m in front of the target, [0,0,5] is 5m above the target
speed: Speed for rockets (f.e: 0.1 fast, 1 slow)
distance: The script will terminate at this distance to the target
correct: Correct angle to target
final: Set the object to the final position as soon as the script terminates

Helpful?
Title: Re: How do you calculate Velocity to move Object A to Object B in 3d space?
Post by: johnnyboy on 21 Jul 2010, 00:06:31
Thanks gents! 

CH: Thanks for the mathematical solution.  That is excellent for understanding the formula.

Lurch:  Thanks for the working example!

This helps me alot.
Title: Re: How do you calculate Velocity to move Object A to Object B in 3d space?
Post by: CH on 21 Jul 2010, 11:03:05
Just noticed a mistake

(j²,k²,l²)^(½)

(j²+k²+l²)^(½)

sorry  :-[

Title: Why so serious?!
Post by: Gaia on 22 Jul 2010, 08:31:11
Code: [Select]
[getposasl objA,getposASL objB] call BIS_fnc_vectorFromXToY; :whistle: