Home   Help Search Login Register  

Author Topic: How do you calculate Velocity to move Object A to Object B in 3d space?  (Read 1708 times)

0 Members and 1 Guest are viewing this topic.

Offline johnnyboy

  • OFPEC Patron
  • ****
  • Matan los Pantalones!!!
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];
El Cojon: "Do you like to Tango?"
You: "Only in Bagango."
Download Last Tango in Bagango and discover how El Cojon earned his name...

Offline CH

  • Members
  • *
« Last Edit: 21 Jul 2010, 11:00:48 by CH »

Offline LurchiDerLurch

  • Members
  • *
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?

Offline johnnyboy

  • OFPEC Patron
  • ****
  • Matan los Pantalones!!!
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.
El Cojon: "Do you like to Tango?"
You: "Only in Bagango."
Download Last Tango in Bagango and discover how El Cojon earned his name...

Offline CH

  • Members
  • *
Just noticed a mistake


Offline Gaia

  • Members
  • *
  • Bohemia Interactive Senior Game Designer
    • My personal webpage (only in Czech)
Why so serious?!
« Reply #5 on: 22 Jul 2010, 08:31:11 »
Code: [Select]
[getposasl objA,getposASL objB] call BIS_fnc_vectorFromXToY; :whistle: