-
Notifications
You must be signed in to change notification settings - Fork 8
pow()
“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.