Jump to content
Sign in to follow this  
DrBobcat

Slopes and Vectors

Recommended Posts

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...

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">// "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.  smile_o.gif

Share this post


Link to post
Share on other sites
Quote[/b] ]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.

Don't waste any more time with those commands, they are a nightmare! Grab my set pitch/bank function here. They are easier for humans to work with.

So then, all you would need to do, is find the angle between the missile and its target, and then set the missile's pitch to that angle. So, if the target is at height of 5m (ASL), and the missile is at height of 20m, and the missile is 50m away, then the pitch required by the missile would be about 16.5 degrees.

You can find this using some simple trigonometry, which isn't really that hard because all you are doing is dealing with triangles sin/cos/tan.

I'm hoping you already know all of this, because then I don't have to draw a diagram, which is the only real way to explain how to do this stuff. smile_o.gif

Share this post


Link to post
Share on other sites

Update! After many hours toiling away at this, working with countless formulas and ideas, I finally got the script to function as I had originally wanted. Here is the portion of the script that has been changed...

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">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)];

};

// _round setVelocity [((sin _ang) * ((cos _zAng) * speed _round)), ((cos _ang) * ((cos _zAng) * speed _round)), ((sin _zAng) * (speed _round))];

sleep 0.01;

};

As I had stated in a sister post on OFPEC, I now need a method to calculate lead. The missile consistently misses targets with recessed central points, such as a Mi17. Also, I likely will still need to apply some added velocity to the missile or it will continue to fall short of the target at long ranges. (The reason why this is happening is because the missile is no longer receiving the thrust outlined in ArmA's config.) If I could receive some additional support on these fronts, I'd be very thankful.

- dRb

EDIT: No offence meant towards you, General Barron. I had not seen your post until after the changes already had been made. That is a brilliant, practical function...

EDIT 2:

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...

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">// "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!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×