Jump to content
Sign in to follow this  
citazenman

Distance command without Z

Recommended Posts

Hello and thank you for reading,

I'm trying to create a cargo drop script and I'm a little stuck. Everything is working fine except the part where the cargo plane actually drops the cargo box. Here is what I have now.

Once the C-130 is in the air and on the way to the player, the script waits with the following:

waitUntil {_c130pilot distance player < 350};

This of course works fine and as soon as the pilot gets close enough the rest of the script continues and the cargo box drops. However, I have the aircraft flying at 750 meters. So it never actually is within 350 meters of the player.

Is there anyway I can get the code above to stop using the Z axis and thus not factor in the height of the aircraft?

Thanks

Share this post


Link to post
Share on other sites
[color="#FF8040"][color="#191970"][b]waitUntil[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#1874CD"]_player[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]getPosASL[/b][/color] [color="#000000"]player[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_pilot[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#191970"][b]getPosASL[/b][/color] [color="#1874CD"]_c130pilot[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_pilot[/color] [color="#191970"][b]set[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#FF0000"]2[/color][color="#8B3E2F"][b],[/b][/color] [color="#1874CD"]_player[/color] [color="#191970"][b]select[/b][/color] [color="#FF0000"]2[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_pilot[/color] [color="#191970"][b]vectorDistance[/b][/color] [color="#1874CD"]_player[/color] [color="#8B3E2F"][b]<[/b][/color] [color="#FF0000"]350[/color] 
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

Share this post


Link to post
Share on other sites

Alternatively, you could use the function that BIS made for this already:

waitUntil {([_c130pilot,player] call BIS_fnc_distance2D) < 350};

Share this post


Link to post
Share on other sites
Alternatively, you could use the function that BIS made for this already:

waitUntil {([_c130pilot,player] call BIS_fnc_distance2D) < 350};

You method is 4.5 times slower, and considering waitunitil will run code almost every frame, is not very productive.

Share this post


Link to post
Share on other sites
You method is 4.5 times slower, and considering waitunitil will run code almost every frame, is not very productive.

This was interesting. :confused:

BIS_fnc_distance2D is essentially:

pos1 = getpos player;
pos2 = getpos c130pilot;
sqrt(
((pos1 select 0) - (pos2 select 0))^2 +
((pos1 select 1) - (pos2 select 1))^2
)

and it executes in 0.0203 ms.

While your code:

pos1 = getPosASL player;
pos2 = getPosASL c130pilot;
pos2 set [2, pos1 select 2];
pos2 vectorDistance pos1;

runs in 0.0118 ms (!)

To my untrained eye, the distance2d seems more simple and should be faster. What am I missing here?

Edited by Greenfist

Share this post


Link to post
Share on other sites

getPosASL is faster than getPos (presumably as it is really getPosAGL and has to do more work than just calculate z above water).

edit: If you need to know AGL, it's still faster to make your own getPosAGL function rather than use getPos apparently.

https://community.bistudio.com/wiki/Code_Optimisation#Position_ASL_is_the_fastest

Edited by Das Attorney

Share this post


Link to post
Share on other sites

That's the difference then. I'll have to keep that in mind. Thanks, DA!

Share this post


Link to post
Share on other sites
That's the difference then. I'll have to keep that in mind. Thanks, DA!

There is not much difference between getPos and getPosASL, even though the latter is a little bit faster. Try it yourself. vectorDistance is the engine command which does euclidean distance calculation so it will always be faster, even more so if you need 3D distance.

Share this post


Link to post
Share on other sites
You method is 4.5 times slower, and considering waitunitil will run code almost every frame, is not very productive.

This is misleading. Yes, it's technically slower, but the difference is completely negligible; we're talking about fractions of a millisecond here. Unless performance is really an issue, I always recommend simplicity.

Share this post


Link to post
Share on other sites
This is misleading. Yes, it's technically slower, but the difference is completely negligible; we're talking about fractions of a millisecond here. Unless performance is really an issue, I always recommend simplicity.

Yes, if you have a single script running, of course, this is a non issue. However at 50FPS you have just 20ms to complete all your scripts per frame, that includes all scheduled and event scripts. With unoptimised scripts it is not going to take long to approach this limit, and once you go over, the FPS will fall to stretch the frame to get more time. Scripts running each frame like this one are more than any other scripts will eat into the allowed time. So simplifying might not be the best approach. Also don't forget that the speed of scripts is different on different PCs. I don't have high end PC and it takes 0.09ms for BIS function to complete while plain code takes 0.02ms. And if you are planning on sharing your mission, you should make it with the lowest spec in mind which means even slower script execution.

Share this post


Link to post
Share on other sites
vectorDistance is the engine command which does euclidean distance calculation so it will always be faster, even more so if you need 3D distance.

I can see that this would be faster than a manually calculated distance but wasn't sure how it stacked up to "distance" as they are similes for each other last time I checked. Also, I thought it might be faster to select only the elements we need rather than be hamstrung to work with 3 elements as dictated by use of vectorDistance and set one z to the other as you do in your example above.

Here are some results:

(getPosASL man1 select [0,2]) distance (getPosASL man2 select [0,2])

// 0.00689697 ms

pos1 = getPosASL man1;
pos2 = getPosASL man2;
pos2 set [2, pos1 select 2];
pos2 vectorDistance pos1

// 0.00909424 ms

And for comparison BIS function:

pos1 = getpos man1;
pos2 = getpos man2;
sqrt(
((pos1 select 0) - (pos2 select 0))^2 +
((pos1 select 1) - (pos2 select 1))^2
)

// 0.0147034 ms

So yeah I'd say in terms of speed, distance is faster for this problem as you don't have to work with the 3rd element.

Share this post


Link to post
Share on other sites

(getPosASL man1 select [0,2]) distance (getPosASL man2 select [0,2])  

0.0100998 ms

(getPosWorld man1 select [0,2]) distance (getPosWorld man2 select [0,2])  

0.00700073 ms

Did I win? :D

Share this post


Link to post
Share on other sites

Wow. I just checked back in on this. I appreciate all the additional suggestions, but I've been using Killzone's recommendation and even in a script heavy environment it is fast enough.

Share this post


Link to post
Share on other sites

Of course it is, we're all just bollocking on here being efficiency twats! xD

Share this post


Link to post
Share on other sites

Did I win? :D

Nope and neither your friend. [x,y] distance [x,y] will pick both points on the surface and [x,y,z] distance [x,y,z] will pick both points in relation to the surface. This is why you need to use vectorDistance, which will pick up absolute points in 3d.

((getPosWorld player select [0,2]) + [0]) vectorDistance ((getPosWorld pilot select [0,2]) + [0])

Seems to be a tiny fraction faster than the solution I offered initially.

Share this post


Link to post
Share on other sites
Nope and neither your friend.

Thanks Mr Personality.

Try making a post without being snidey for a change.

Share this post


Link to post
Share on other sites
Nope and neither your friend. [x,y] distance [x,y] will pick both points on the surface and [x,y,z] distance [x,y,z] will pick both points in relation to the surface.

This would be a problem how exactly? The OP's goal would be achieved just the same.

I actually didn't know about that alternate syntax for the select command. Thanks for that, Das Attorney!

Share this post


Link to post
Share on other sites
This would be a problem how exactly? The OP's goal would be achieved just the same.

I actually didn't know about that alternate syntax for the select command. Thanks for that, Das Attorney!

Because OP asked for 2D distance and using distance command would still produce some kind of 3D distance unless the ground is perfectly flat and horizontal between the plane and the player.

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  

×