The truncation of the val for the throttle curve has no effect because the absolute of it (val_a) is defined previously. So the truncation should be done before val_a is created.
float throttle_curve(float val, float curve_acc, float curve_brake, int mode) {
float ret = 0.0;
float val_a = fabsf(val); <-WRONG HERE
if (val < -1.0) {
val = -1.0;
}
if (val > 1.0) {
val = 1.0;
}
<-BETTER HERE
float curve;
if (val >= 0.0) {
curve = curve_acc;
} else {
curve = curve_brake;
}
// See
// http://math.stackexchange.com/questions/297768/how-would-i-create-a-expo...
if (mode == 0) { // Power
if (curve >= 0.0) {
ret = 1.0 - powf(1.0 - val_a, 1.0 + curve);
} else {
ret = powf(val_a, 1.0 - curve);
}
} else if (mode == 1) { // Exponential
if (fabsf(curve) < 1e-10) {
ret = val_a;
} else {
if (curve >= 0.0) {
ret = 1.0 - ((expf(curve * (1.0 - val_a)) - 1.0) / (expf(curve) - 1.0));
} else {
ret = (expf(-curve * val_a) - 1.0) / (expf(-curve) - 1.0);
}
}
} else if (mode == 2) { // Polynomial
if (curve >= 0.0) {
ret = 1.0 - ((1.0 - val_a) / (1.0 + curve * val_a));
} else {
ret = val_a / (1.0 - curve * (1.0 - val_a));
}
} else { // Linear
ret = val_a;
}
if (val < 0.0) {
ret = -ret;
}
return ret;
}