Skip to content

Instantly share code, notes, and snippets.

@Orpheon
Created March 29, 2014 14:25
Show Gist options
  • Select an option

  • Save Orpheon/9855406 to your computer and use it in GitHub Desktop.

Select an option

Save Orpheon/9855406 to your computer and use it in GitHub Desktop.
// If we're not inside our current/last rectangle, then we are in the air flying towards our destination, so we should just continue to do that
if (character->x < current_rect->bottomleft.x || character->x > current_rect->bottomright.x || character->y < current_rect->topleft.y || character->y > current_rect->bottomleft.y)
{
// Get the equation of the parabola of our current falling (in the form y = ax^2 + bx + c)
double a, b, c;
a = GRAVITY/2;
b = sqrt(character->hs*character->hs + character->vs*character->vs) - 2*a*character->x;
c = character->y - a*character->x*character->x - b*character->x;
// Test whether we'll land within the right boundaries with the current path
double landing_x;
// The determinant should always be positive since we will always be above the target (and gravity always points down)
landing_x = (-b + sqrt(b*b - 4*a*(c-next_rect->bottomleft.y)))/(2*a);
if (landing_x < next_rect->bottomleft.x)
{
// We're overshooting on the left side
output[DIRECTION] = DIR_LEFT;
}
else if (landing_x > next_rect->bottomright.x)
{
// We're overshooting on the right side
output[DIRECTION] = DIR_RIGHT;
}
else
{
// We're good, but we might get to where-ever faster if we continued moving in the direction we were going until now
if (character->hs < 0)
{
output[DIRECTION] = DIR_LEFT;
}
else if (character->hs > 0)
{
output[DIRECTION] = DIR_RIGHT;
}
else
{
output[DIRECTION] = STANDSTILL;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment