Skip to content

Instantly share code, notes, and snippets.

@BonsaiDen
Created November 17, 2011 22:23
Show Gist options
  • Save BonsaiDen/1374744 to your computer and use it in GitHub Desktop.
Save BonsaiDen/1374744 to your computer and use it in GitHub Desktop.
Util code for a platformer
#include <stdio.h>
#define max(A, B) (A > B ? A : B)
// Calculate required initial force and downward acceleration for a jump of "height" in "time"
void jump_data(const float time, const int fps, const float height, float *force, float *acc) {
const float steps = time / (1.0f / fps);
*force = (height * (1.0f / steps)) * 2.0f;
*acc = *force / max(steps - 1.0f, 1.0f);
}
// Calculate the downward acceleration needed to reach "max" in "time"
float fall_data(const float time, const int fps, const float max) {
const float steps = time / (1.0f / fps);
return max / (max(steps - 1, 0.1f));
}
int main() {
float jump_force;
float jump_acc;
float fall_acc = fall_data(3.666f, 30, 4);
jump_data(0.3, 30, 30, &jump_force, &jump_acc);
printf("%.4f %.4f\n", jump_force, jump_acc);
printf("%.4f\n", fall_acc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment