fl0w Posted February 2, 2019 Share Posted February 2, 2019 Hello, everyone. Lately, I have been messing around a bit with some Lua code for DCS aircraft systems in order to get my desired results of incrementing/decrementing values. In this case, I am using 2 if statements acting as loops shown in order to set a "fluid" transition animation of my gear lever. Method 1: -- If the gear is supposed to be up and is down if placement == 0 and lever_pos > 0 then lever_pos = lever_pos - 0.002 end -- If the gear is supposed to be down and is up if placement == 1 and lever_pos < 1 then lever_pos = lever_pos + 0.002 end Method 2: -- If the gear is supposed to be up and is down if placement == 0 and lever_pos ~= 0 then lever_pos = lever_pos - 0.002 end -- If the gear is supposed to be down and is up if placement == 1 and lever_pos ~= 1 then lever_pos = lever_pos + 0.002 end The issue is what type of way to check lever_pos is better, the ~= (or !== in other languages as 'not equal to') method of using the greater/less than operators. I have found that the "than" operators give you a range that works but it can either go backward or too much forward in order to be true. And with ~=, it does not fully "recognize" it (the lever_pos) at 0 or 1. In order to check, we can do simple math. We can assume lever_pos is going to start at 0 or 1. 0.002 is my time update rate (in this case, 0.01 or 10 ms/s) divided by 5, to represent 5 seconds of movement. Doing 1 - (0.002 * 5) will return 0.99, meaning still .1 is missing. Does anyone know a good method besides having a non-perfect value to level it out to 1 or 0? Any help is appreciated. Link to comment Share on other sites More sharing options...
BR55Sevas Posted February 2, 2019 Share Posted February 2, 2019 Not sure about correct/incorrect solution, just some thoughts add another if statement for force round var value if (lever_pos > 0.9) then lever_pos = 1.0 end if (lever_pos < 0.1) then lever_pos = 0.0 end МиГ-29 Fly by wire СДУ Su-27SM second display panel https://www.youtube.com/embed/videoseries?list=PL_2GGwNpWNp_fKXfRtDhIk8s5Jf4a9XHS http://berkuts.ru Пилотажный сервер с роботом | Aerobatic server with PhantomControl Link to comment Share on other sites More sharing options...
fl0w Posted February 2, 2019 Author Share Posted February 2, 2019 (edited) Not sure about correct/incorrect solution, just some thoughts add another if statement for force round var value if (lever_pos > 0.9) then lever_pos = 1.0 end if (lever_pos < 0.1) then lever_pos = 0.0 end Thanks. I did consider this in the past but I wanted to verify for any way to mathematically have it done to increment/decrement perfectly to 0 or 1 while using the time update rate/seconds wanted. It seems you can't do it unless you use an even value of division. Since I divided 0.01 by 5, I used an odd number to rep. my time transition which may explain the issue. Edited February 2, 2019 by Sirius Link to comment Share on other sites More sharing options...
BR55Sevas Posted February 2, 2019 Share Posted February 2, 2019 (edited) Another way I see is add some kind of counter. If you know your dt, then you know tick number for full transition from 0 to 1. So if you know there are shoud be 20 ticks, then just check this ticks if (placement == 0) and (lever_pos > 0) and (tickCounter <21) then lever_pos = lever_pos - 0.002 tickCounter = tickCounterPrev + 1 tickCounterPrev = tickCounter end also need to add extra statement for nulling the counter after we have reached the limits. Of-course bad thing could happens in this case that we can over count 1.1.. or -0.1 which give unpredictable results with animation arguments. And finally as we work with float/double vars so obvious better to check some range and manually round value. So I think simple checking small deadzone in my 1st post is better solution. Edited February 2, 2019 by BR=55=Sevas МиГ-29 Fly by wire СДУ Su-27SM second display panel https://www.youtube.com/embed/videoseries?list=PL_2GGwNpWNp_fKXfRtDhIk8s5Jf4a9XHS http://berkuts.ru Пилотажный сервер с роботом | Aerobatic server with PhantomControl Link to comment Share on other sites More sharing options...
Recommended Posts