Home   Help Search Login Register  

Author Topic: relative position 3D  (Read 2602 times)

0 Members and 2 Guests are viewing this topic.

Offline Goullou

  • Members
  • *
relative position 3D
« on: 21 Feb 2008, 01:08:43 »
Hi all,

I was using a function "getRelPos" made by Alimag since OFP. This function returns a relative position from an object.
It's very useful for a lot of things.
Here it is:
Code: [Select]
// getRelPos.sqf
// AliMag - 02/2003
// return relative position from _obj using distance and direction

private ["_obj","_dis","_posX","_posY","_dir","_pos"];

_obj = _this select 0;
_dis = _this select 1;
_dir = _this select 2;

_posX = getPos _obj select 0;
_posY = getPos _obj select 1;
_pos = [_posX + ((sin _dir) * _dis), _posY + ((cos _dir) * _dis),0];
_pos
This function is still compatible with ArmA but as you can see it works really for a 2D position. Z axe is always set to 0.
This is the place where I need help. If someone could tell me a way to modify that to make the function returning a real 3D relative position.
Thanks for your help.
Si vis pacem, para bellum.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: relative position 3D
« Reply #1 on: 21 Feb 2008, 02:00:50 »
You may try ArmA command worldToModel. For you case:
Code: [Select]
obj1 worldToModel (getPos obj2)
That will return the relative position in 3D of obj2 using obj1 as coordinates origin.

Offline Goullou

  • Members
  • *
Re: relative position 3D
« Reply #2 on: 21 Feb 2008, 02:26:06 »
Oh... Can you tell me more about how to use it?

With getRelPos when i was wanting to get a relative position from an object i was giving this:

_pos = [_heli,20,(direction _heli)-180] call getRelPos

Which was returning a position 20m behind the object. How to do that with worldToModel?
« Last Edit: 21 Feb 2008, 02:29:48 by Goullou »
Si vis pacem, para bellum.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: relative position 3D
« Reply #3 on: 21 Feb 2008, 09:21:32 »
Code: [Select]
_heli worldToModel [(getPos _heli select 0)+sin((getDir _heli)-180)*20,(getPos _heli select 1)+cos((getDir _heli)-180)*20,getPos _heli select 2];

Anyway, the relative coordinates for that are directly [0,-20,0], you can use modelToWorld to convert to world coordinates: _coords = _heli modelToWorld [0,-20,0];

Offline Tajin

  • Members
  • *
Re: relative position 3D
« Reply #4 on: 21 Feb 2008, 18:12:50 »
modeltoworld and worldtomodel are amazing commands, make good use of them. :)

This might be helpful aswell, gets you the vector, directions and xydistance between two objects.:
Code: [Select]
_pos1 = getposasl _obj1;
_pos2 = getposasl _obj2;
_xdis = (_pos2 select 0) - (_pos1 select 0);
_ydis = (_pos2 select 1) - (_pos1 select 1);
_zdis = (_pos2 select 2) - (_pos1 select 2);
_dir = _xdis atan2 _ydis;
_xydis = sqrt((_xdis*_xdis)+(_ydis*_ydis));
_dirup = _xydis atan2 _zdis;

_xydis is the 2dimensional distance (not counting the Z axis) for the total distance you can simply use the "obj1 distance obj2" command.



Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: relative position 3D
« Reply #5 on: 22 Feb 2008, 11:45:58 »
Note that model coordinates use the model as origin and the axes are transformed depending on current vectorUp of the model. So, in model coordinates, [0,0,2] will always point 2m below the object, which will result in a higher altitude if the object is upsidedown. As another example, [0,10,0] will point also to a real 3D position 10m above the object if the object is a plane climbing vertically (10m ahead of its center). So, dont forget that modelToWorld and WorldToModel functions always consider roll and pitch of the object which define its vectorUp.

Offline Goullou

  • Members
  • *
Re: relative position 3D
« Reply #6 on: 23 Feb 2008, 19:50:34 »
Hi guys,

All i can read here was very instructive.
I'm back on this post after a lot of tests around modelToWorld which can effectively replace the getRelPos function.

However i have to explain what i'm trying to do.

The main goal seems to be crazy because i just try to make a script to stop the back rotor of a UH60 to simulate a happening failure.
The only way i found was to create some bullets close to the rotor to make a local damage.
To make it i used maths given by Spooner in another topic talking about a Claymore mine simulation.
Here's my script.

BRfailure.sqf
Code: [Select]
//_heli = _this select 0;
_heli = vehicle player;
rotordead = false;
_HEid = _heli addEventHandler ["dammaged",
{
if((_this select 1)=="mala vrtule"and(_this select 2)==1)then{rotordead=true};
hint format ["s0: %1\ns1: %2\ns2: %3",_this select 0,_this select 1,_this select 2]
}];
sleep 0.5;
_spd = 1100;

for [{_lp=0},{_lp<5},{_lp=_lp+1}] do
{
_pos = _heli modelToWorld [1,-7.9,0.95];
_hAngle = (direction _heli) + 270;
_vAngle = asin ((vectorDir _heli) select 2);
_vel = [(sin _hAngle)*(cos _vAngle)*_spd,(cos _hAngle)*(cos _vAngle)*_spd,(sin _vAngle)*_spd];
_bullet = "B_127x99_Ball_noTracer" createvehicle _pos;
_bullet setVectorDir _vel;
_bullet setVelocity _vel;
sleep 0.1;
if (rotordead) then {_lp=5};
};
_heli removeEventHandler ["Dammaged",_HEid];

This script works but not perfectly. I think the problem comes from the line _vAngle = asin ((vectorDir _heli) select 2); which makes the axe of the bullet not always correct depending the pitch and inclination of the chopper.
My problem is that my math level is extremly low. And i often do not understand what i copy and paste.   :confused:
You'd be great to help me to correct this axe.
« Last Edit: 23 Feb 2008, 19:53:58 by Goullou »
Si vis pacem, para bellum.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: relative position 3D
« Reply #7 on: 23 Feb 2008, 20:14:41 »
try the following:
Code: [Select]
_heli = _this select 0;
for [{_i=0},{_i<5},{_i=_i+1}] do
{
// Adjust [0,-5,1] till you hill always your tail rotor [0,-5,1] means 5m behind and 1m above chopper's center
   _pos = _heli modelToWorld [0,-5,1];
   _bullet = "B_127x99_Ball_noTracer" createvehicle _pos;
   Sleep 0.1;
};

Offline Goullou

  • Members
  • *
Re: relative position 3D
« Reply #8 on: 23 Feb 2008, 20:30:23 »
That's what i did first but it causes a lot of damage. The best is to hit the tail rotor from the side. From this position i just need 2 or 3 bullets to stop it. That's what my script do actualy. My modelToWorld [1,-7.9,0.95] is very close to the centre of the rotor. The result is almost pretty good but if the chopper is really rolling on a side, the shots miss the rotor. Try my script in situation and you'll see what's happening. Bullets are supposed to be "no tracer" but can be seen anyway. :D Otherwize you'll see the impacts.
Si vis pacem, para bellum.

Offline Tajin

  • Members
  • *
Re: relative position 3D
« Reply #9 on: 25 Feb 2008, 15:04:22 »
I guess adding the current velocity of the chopper to the velocity of the bullet would make it a little more accurate.

And for the velocityvector toward the rotor ....
lets try the following:

Code: [Select]
_pos1 = _heli modelToWorld [1,-7.9,0.95];
_pos2 = _heli modelToWorld [-1,-7.9,0.95];
_vx = (_pos2 select 0) - (_pos1 select 0);
_vy = (_pos2 select 1) - (_pos1 select 1);
_vz = (_pos2 select 2) - (_pos1 select 2);
_bullet = "B_127x99_Ball_noTracer" createvehicle _pos1;
_bullet setpos _pos1;

_cvel = velocity _heli;
_fact = 50;

_bullet setvelocity [(_cvel select 0) + _vx * _fact,(_cvel select 1) + _vy * _fact,(_cvel select 2) + _vz * _fact];

not tested, just using your values for the position of the rotor... so we spawn a bullet at a position on the right side of the chopper and accalerate it to the same position mirrored to the left side. And we also add the current velocity of the helo, just to be sure.

try it out and tell me if it works  :D
ps.: with a bit of creativity there is no need for a high math level :)