Home   Help Search Login Register  

Author Topic: Slopes and Vectors  (Read 1794 times)

0 Members and 1 Guest are viewing this topic.

Offline drbobcat

  • Members
  • *
  • Rawr
Slopes and Vectors
« on: 14 May 2008, 10:33:47 »
While I have not had many problems with scripting up till now, I knew my lack of knowledge in advanced mathematics would eventually cause issues to arise. The following script spawns a missile of a chosen type at a relative x-y-z coordinate given by the mission designer. It works almost perfectly, but there are a few issues.

1) With the basic formula I have applied to the missile's Z velocity, it loves to head towards the ground at an amazing rate, only to stop at whatever height the target is at. It makes an almost perfect curve. Unfortunately, it looks too clean to seem realistic and does not fly like a missile really should. Is there a better formula I could use?

2) Although I have read of the "vectorUp" and "setVectorUp" commands introduced in ArmA, I have not a clue what equations I'd need to use in order to have the missile aiming up/down at the target it is heading towards. Any help here would be equally appreciated.

3) Is there a way I could guarantee a hit each and every time so I do not need to use a crude "net" to insure damage as seen in the "distance < 10" condition?

The script...
Code: [Select]
// "Missile Generator" by DrBobcat (with much help from the ArmA/OFP editing community)
// [target,"type",x,y,z] execvm "DRB_Missile.sqf";

_target = _this select 0;
_type = _this select 1;
_xPos = _this select 2;
_yPos = _this select 3;
_zPos = _this select 4;

_round = _type createvehicle [0,0,1000];
_burst = "Bomb" createVehicle [0,0,1005];
_round setPos [(getposASL _target select 0) + _xPos, (getposASL _target select 1) + _yPos, (getposASL _target select 2) + _zPos];

while {!(isNull _round)} do
{
_ang = (((getposASL _target select 0) - (getposASL _round select 0)) atan2 ((getposASL _target select 1) - (getposASL _round select 1)));
_round setDir _ang;

_zAdjust = (speed _round) * ((((getPosASL _target select 2) + 1.5) - (getPosASL _round select 2)) / (_round distance _target));
_round setVelocity [velocity _round select 0, velocity _round select 1, _zAdjust];

if ((_round distance _target) < 10) exitWith
{
_round setVelocity [0,0,0];
{_x setPos (getPos _target)} forEach [_burst,_round];
{_x setDammage 1} forEach [_burst,_target];
{_x setDammage 1} forEach (crew _target);
};

sleep 0.01;
};

sleep 0.1;

deleteVehicle _round;
deleteVehicle _burst;

NOTE: I do realize there are other snippets and suites available that likely do exactly the same sort of thing, but I am curious and am trying to figure it all out for the sake of learning something new.

Much appreciated,
- dRb  ;)



EDIT: It should also be noted that this script does not influence the missile's X or Y velocities simply because I thought it would be easier to "steer" the missile to the target than forcing it to obey a strict route.  :)
« Last Edit: 14 May 2008, 12:05:43 by drbobcat »

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Slopes and Vectors
« Reply #1 on: 14 May 2008, 13:22:44 »
The basic rules are to use setVectorDir, setVectorUp and setVelocity and not setDir. The vectorUP is one that points up (lets say to the sun) and is relative to the object (this one affects the orientation of the object relative to its axes), so this is a bit different than vectorDir which points to where the plane is moving at. VectorUp and VectorDir use a vector magnitude of 1 and you need to adjust both accurately and accordingly with the velocity vector to ensure smooth movement of the object in the space.

For example, a plane going vertically up, in world coords, its vectorDir might be close to [0,0,1], while the up vector in model coors would be something close to [0,1,0]. If the plane is stalling and moving backwards, [0,0,-1] for dirVector and still [0,1,0] for up vector. And so on.

Offline drbobcat

  • Members
  • *
  • Rawr
Re: Slopes and Vectors
« Reply #2 on: 14 May 2008, 15:27:00 »
Thank you for the reply, Mandoble. If anyone could have helped with this, you'd be that person. Now, after a little more research into the vector commands, I have managed to get the script to operate directionally using them alone. However, I still am completely stumped with configuring the up vectors. Which math commands would I use and how? I do not wish to inconvenience you in any way, but I am just so interested in obtaining a solution to this bothersome problem.

Code: [Select]
while {!(isNull _round)} do
{
_ang = (((getposASL _target select 0) - (getposASL _round select 0)) atan2 ((getposASL _target select 1) - (getposASL _round select 1)));
//_round setDir _ang;

_round setVectorDir [sin _ang, cos _ang, vectorDir _round select 2];
_round setVectorUp [..., ..., ...];

// _zAdjust = (speed _round) * ((((getPosASL _target select 2) + 1.5) - (getPosASL _round select 2)) / (_round distance _target));
//_round setVelocity [velocity _round select 0, velocity _round select 1, _zAdjust];

if ((_round distance _target) < 10) exitWith
{
_round setVelocity [0,0,0];
{_x setPos (getPos _target)} forEach [_burst,_round];
{_x setDammage 1} forEach [_burst,_target];
{_x setDammage 1} forEach (crew _target);
};

sleep 0.01;
};

Thanks,
- dRb
« Last Edit: 14 May 2008, 15:31:01 by drbobcat »

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Slopes and Vectors
« Reply #3 on: 14 May 2008, 16:28:27 »
You will enjoy checking this. Else just download mando missile, open mando_missile.sqf and see there how missile's direction, up and velocity vectors are computed.

Offline drbobcat

  • Members
  • *
  • Rawr
Re: Slopes and Vectors
« Reply #4 on: 15 May 2008, 01:50:12 »
Although still behaving like the rebel I am, I have finally gotten the script to do what I had originally wanted it to. Rather than applying any exterior velocities to the missile, I allow it to operate within its preset parameters instead. Two new questions come to mind, however.

1) Is there any advantage or noticible difference in using setVectorDir over setDir in this particular case?

2) What kind of commands would I use to calculate "lead?" The current system has a tendancy to miss aircraft with a recessed center point, such as the Mi17.

The updated script portion...

Code: [Select]
while {!(isNull _round)} do
{
_ang = (((getposASL _target select 0) - (getposASL _round select 0)) atan2 ((getposASL _target select 1) - (getposASL _round select 1)));
_round setDir _ang;
//_round setVectorDir [sin _ang, cos _ang, vectorDir _round select 2];

if (_xPos != 0) then
{
_zAng = (((getposASL _target select 0) - (getposASL _round select 0)) atan2 (((getposASL _target select 2) + _h) - (getposASL _round select 2)));
_round setVectorUp [cos _zAng, 0, -(sin _zAng)];
} else
{
_zAng = (((getposASL _target select 1) - (getposASL _round select 1)) atan2 (((getposASL _target select 2) + _h) - (getposASL _round select 2)));
_round setVectorUp [0, cos _zAng, -(sin _zAng)];
};

sleep 0.01;
};

- dRb

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Slopes and Vectors
« Reply #5 on: 15 May 2008, 09:03:26 »
setDir resets both vectorUp and vectorDir, so you might notice a non smooth flight of the object.
There are no commands to provide you the lead, you need to use complex maths or in a much simpliflied way simply point to where the object will be in the next 2 seconds (use its velocity vector to calculate that position).

Offline drbobcat

  • Members
  • *
  • Rawr
Re: Slopes and Vectors
« Reply #6 on: 15 May 2008, 19:22:38 »
The script, for now, is finished. I did not implement any advanced methods of calculating lead, and rather allowed the user to specify lateral and height offsets. I believe this is enough and only a minor inconvenience. Soon, I will make a similar version that uses a specific coordinate starting position instead of relative coordinates.

Here we are...
Code: [Select]
// "Missile Generator (Relative)" by DrBobcat
// [target,"type",x,y,z,lateralOffset,heightOffset,proximityBool,proximityDistance] execvm "DRB_MissileR.sqf";
// Much thanks goes out to the ArmA/OFP editing community!

_target = _this select 0;
_type = _this select 1;
_xPos = _this select 2;
_yPos = _this select 3;
_zPos = _this select 4;
_lOff = _this select 5;
_hOff = _this select 6;
_prox = _this select 7;
_pDis = _this select 8;

_burst = objNull;

_round = _type createvehicle [0,0,1000];
if (_prox) then {_burst = "Bomb" createVehicle [0,0,1005];};
_round setPos [(getposASL _target select 0) + _xPos, (getposASL _target select 1) + _yPos, (getposASL _target select 2) + _zPos];

while {!(isNull _round)} do
{
if ((_round distance _target) <= _pDis) exitWith
{
_round setVelocity [0,0,0];
{_x setPos (getpos _round)} forEach [_burst,_round];
_burst setDammage 1;
sleep 0.1;
{deleteVehicle _x} forEach [_burst,_round];
};

_lPosX = ((getPosASL _target select 0) + (_lOff * (sin (getDir _target))));
_lPosY = ((getPosASL _target select 1) + (_lOff * (cos (getDir _target))));
_lAng = ((_lPosX - (getposASL _round select 0)) atan2 (_lPosY - (getposASL _round select 1)));
_round setVectorDir [sin _lAng, cos _lAng, vectorDir _round select 2];

if (_xPos != 0) then
{
_zAng = (((_lPosX) - (getposASL _round select 0)) atan2 (((getposASL _target select 2) + _hOff) - (getposASL _round select 2)));
_round setVectorUp [cos _zAng, 0, -(sin _zAng)];
} else
{
_zAng = (((_lPosY) - (getposASL _round select 1)) atan2 (((getposASL _target select 2) + _hOff) - (getposASL _round select 2)));
_round setVectorUp [0, cos _zAng, -(sin _zAng)];
};

sleep 0.01;
};

Again, thanks for all the help. If anyone notices any glaring flaws that I may have overlooked, please do speak up!

- dRb