Home   Help Search Login Register  

Author Topic: inaccurate sleep  (Read 2126 times)

0 Members and 1 Guest are viewing this topic.

Offline Tobbs

  • Members
  • *
inaccurate sleep
« on: 22 Mar 2010, 19:52:51 »
I'm having some trouble with the sleep function giving me consistently inconsistent results. :dunno:

The projectile in the case below is set to detonate 400m from its shooter. It ends up detonating at between 317m and 330m 90% of my tests.  At 700m range it ends up about 200m short.

I'm guessing that the sleep-function isn't quite as precise as I'd like it to be, or to dependent on fps or somesuch. Anyone know? Is there a better way to do it?



Code has been edited to remove irrelevant clutter...
Code: [Select]
//Timed Fuse (sqf), called by eventhandler "fired"
//*snipp*
_V0 = speed (vehicle _projectile) / 3,6 ; //Actual speed is 1560m/s
_range = 400;
_timer = _range / _V0;

sleep _timer;

//Detonation code
_pos = getpos _projectile;
deleteVehicle _projectile;
_pos2 = [_pos select 0, _pos select 1, _pos select 2];
_b2 = "roadcone" createVehicle _pos2;
_b2 setPos _pos2;
_b1 = "b_30mm_he" createVehicle (_b2 modelToworld [0,0,0]);
_b2pos = _b2 modelToWorld [0,0,0];
sleep 0.05;
_conerange = _veh distance _b2; //used to print out the actual distance between player and spawned roadcone

//deletevehicle and other effects follows...
« Last Edit: 22 Mar 2010, 20:02:00 by Tobbs »

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Re: inaccurate sleep
« Reply #1 on: 22 Mar 2010, 21:21:40 »
The sleep function is the sqf-equivalent of the ~ in the sqs lscripting language. In sqs ~4 told the script to "sleep" for 4 seconds. But as you suspect, everything depended on how fast your system was. And in a MP game, everything runs slower on the host. I've always assumed that sleep works the same way nowdays.
OFPEC | Intel Depot
RETARDED Ooops... Retired!

Offline ModestNovice

  • Members
  • *
Re: inaccurate sleep
« Reply #2 on: 23 Mar 2010, 00:20:08 »
Looking at the code, it would not wait 400 seconds because you are dividing 400 by _V0?

Why not just use a waitUntil?
Code: [Select]
waitUntil {_veh distance _b2 >= _range};
Something like that as I wasn't sure which variable name is the one you want to be _range distance from.
« Last Edit: 23 Mar 2010, 00:23:04 by DaChevs »
"The road became empty and the people disappeared. The clouds ran away; opened up the sky, and one by one I watched every constellation die."
- Sean "Slug" Daley

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: inaccurate sleep
« Reply #3 on: 23 Mar 2010, 07:55:10 »
waitUntil is indeed a solution, since it runs every frame (=as fast as the engine/your system can manage). This still leaves room for inaccuracies - sorry, absolute precision just isn't going to happen, especially not in battlefield conditions (you might get an exact 400m on-the-mark explosion in 'lab' conditions on Utes with nothing else but the script and the bombs, but not in a laggy battle). I hope that's not -that- important :)

The only problem with waitUntil is that a very large number of them can start to bog down a system, since as mentioned they check every frame. But if you're not expecting to have a lot of these babies in the air at once, and considering they probably have a lifetime of only a few seconds anyway, I think waitUntil should be fine.

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline kju

  • Members
  • *
    • PvPScene - The ArmA II multiplayer community
Re: inaccurate sleep
« Reply #4 on: 23 Mar 2010, 09:12:12 »
You can tweak the cycle time this way:

Code: [Select]
waitUntil {sleep 0.001; ((_veh distance _b2) >= _range)};

Offline Loyalguard

  • Former Staff
  • ****
Re: inaccurate sleep
« Reply #5 on: 23 Mar 2010, 09:20:56 »
Just in case it's applicable, according to the Biki and the OFPEC comref, distance measures above ground level and only map distances, not vector distance. So, if the projectile is not traveling parallel to the ground, distance will return an inacurrate result I think.  

Offline Worldeater

  • Former Staff
  • ****
  • Suum cuique
Re: inaccurate sleep
« Reply #6 on: 24 Mar 2010, 09:14:36 »
The real problem here is the false assumption that the projectile keeps its muzzle velocity.
try { return true; } finally { return false; }

Offline CH

  • Members
  • *
Re: inaccurate sleep
« Reply #7 on: 24 Mar 2010, 18:08:17 »
I'm guessing that the sleep-function isn't quite as precise as I'd like it to be, or to dependent on fps or somesuch. Anyone know? Is there a better way to do it?

As I understand it, this code

Code: [Select]
_t = Time;

waitUntil {Time > (_t + 3)};

does the same as this

Code: [Select]
sleep 3;

but without being FPS dependant (in most cases).

Offline kju

  • Members
  • *
    • PvPScene - The ArmA II multiplayer community
Re: inaccurate sleep
« Reply #8 on: 24 Mar 2010, 18:32:28 »
Code: [Select]
_time = time + 3;

waitUntil {(time > _time)};

Variable naming + design.

Offline Tobbs

  • Members
  • *
Re: inaccurate sleep
« Reply #9 on: 27 Mar 2010, 09:31:24 »
Thanks for all the good answers!

The waitUntil solution should work fine. I do not require pinpoint accuaracy but 200m on a 700m range is a tad to inaccurate for my taste. Something in the line of +- 1m would be great but if it's withing 10m that would be satisfactory.

The real problem here is the false assumption that the projectile keeps its muzzle velocity.
I measured the projectiles velocity at it's "endpoint" and it would seem that there is no decrease in the projectiles speed. At least not on distances up to 1km.