pow() - leiget/Godot_FPC_Base GitHub Wiki
โpow()โ is simply the exponential function. It works like this:
pow(4 , 2) = 16
So in this example we take โ4โ and multiply it by itself twice, so that it equals โ16โ, as โ4*4=16โ. You can also do this:
pow(4 , 2.5) = 32
So what we are doing here is multiplying โ4โ by itself twice, and then we multiply that result by take half of โ4โ, which is, of course, โ2โ. It looks like this:
pow(4 , 2.5) = 4*4*2 = 32
The second argument, the exponent, can be anything you want. Letโs say you do this:
pow( 4, 0.5 ) = 2
Did you see what happened? We were able to be the square root of โ4โ by using an exponent, which in this case is โ0.5โ. Another example:
pow(4 , 0.25) = 1.414213562
Okay, this is useful. But what can it be used for? It can be used for many things, but in this script we use it to make a linear falloff into a smooth falloff. Letโs take a look at some examples. Letโs say we have a car. And whenever the player presses on the accelerate button, it takes some time to get to max speed. Letโs look at some variables:
CarSpeed = 0.0
MaxSpeed = 120
TimeToMax = 25
CurrentTime = 0.0
They should be self-explanatory. The last variable, โCurrentTimeโ, is how long the player has been pressing the accelerator button. Here would be the code that we are using right now:
CarSpeed = (CurrentTime / TimeToMax) * MaxSpeed
So letโs look at a real-number example:
CarSpeed = (12.5 / 25) * 120 = 60
This code can be visualized as a graph which looks like this:
As you can see, the acceleration of the carโs speed is a straight shot from 0 to 120. Thatโs okay, but letโs make it better like this:
CarSpeed = pow( ( CurrentTime / TimeToMax ) , 0.5 ) * MaxSpeed
And in a real number example:
CarSpeed = pow( (12.5 / 25) , 0.5 ) * 120 = 84.85
Which would look like this visualized:
As you can see, the acceleration is quite different now, with the car accelerating quickly in the first few seconds, but then taking much longer to get to max speed after that. This is more how cars accelerate, and you can simply adjust the exponent to whatever you want to edit the acceleration of the car.
Letโs look at another example, this time using a exponent larger then 1:
result = pow( x , 2 )
Which would look like this, visualized:
As you can see, the curve is now in the opposite direction. The script uses this kind of falloff for when the player jump. When the player presses the jump button, the velocity starts out at full velocity, according to whatever โJump_Velโ is. Then over time the curve above is subtracted from the max jump velocity. It looks like this in the script:
Jump_CurrentVel = Jump_Vel - (Jump_Vel * ( pow(Jump_CurrentTime / Jump_Length, 2) ))
So a real-number example would look like this:
Jump_CurrentVel = 15.19 - (15.19 * ( pow(0.5 / 1.0 , 2) ))
#Simplified.
Jump_CurrentVel = 15.19 - ( 15.19 * ( 0.25 ) )
#Simplified.
Jump_CurrentVel = 15.19 โ 3.7975
#Simplified.
Jump_CurrentVel = 11.3925
So, in the above example, the current jump time is โ0.5โ, or half way through the jump. So, we take that and multiply it to the second power. That gives us โ0.25โ. Then we multiply that by the max jump velocity, which gives us โ3.7975โ. Then, lastly, we take the max jump velocity and subtract โ3.7975โ from it to get the current jump velocity.
Over time, the jump velocity tapers off, so that as the player reaches the peak of the jump, he comes to a gradual stop in the air instead of going up and then sharply start falling down.
So, as with our example, it would look like this:
And thatโs about it.