double g = 9.8d; //gravity
double m = 1.0d; //mass
double fMax = 15; //maximum upwards force
double idealHeight = 100; //ideal height the craft should be off the ground
double v; //velocity
void Update() {
double h = Math.Abs((craft.Y + craft.Height) - ground.Y); //determin the craft's height from the ground.
double a = (g - f(h)) * m; //acceleration
v += a; //velocity
v = MathHelper.Clamp((float)v, -15.0f, 15.0f); //make sure the acceleration doesn't get out of hand
craft.Y += (int)v; //assign the velocity
}
double f(double h) { //this function return the amount of upwards force
return Math.Max(0, h * (g - fMax) / idealHeight + fMax);
}
One cunfusing thing to note is that my game is 2D with (0, 0) being the top left corner of the window and (100, 100) being accrosed and down 100 pixels. This is why I have the Math.Abs(craft.Y + craft.Height) - ground.Y whole thing.
Now the problem:
This code works but the craft just goes up and down up and down up and down enlessly. I would like the craft's velocity to diminish over time to equal out with the idealHeight. Like this picture shows.
How would I do that?
Thanks.