Jump to content

Method of using if statements as loops


fl0w

Recommended Posts

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

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

Link to comment
Share on other sites

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 by Sirius
Link to comment
Share on other sites

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 by BR=55=Sevas
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...