I am making a game where the character speeds up in the direction of the mouse when it is press. Along the +x +y to -x -y axis it works, but along the +x -y to -x +y axis it behaves erratically and then crashes with a floating point exception, like this gif: shows. I am using raylib, here is my code:
Vector2 goToPosition = GetMousePosition();
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
Vector2 relative = {goToPosition.x - ballPosition.x, goToPosition.y - ballPosition.y};
if (abs(relative.x) > abs(relative.y)) {
velocity.x += relative.x > 0 ? 5 : -5;
velocity.y += relative.y > 0 ? 5 * (abs(relative.x) / abs(relative.y)) : 5 * -(abs(relative.x) / abs(relative.y));
}
else if (abs(relative.x) < abs(relative.y)) {
velocity.x += relative.x > 0 ? 5 * (abs(relative.y) / abs(relative.x)) : 5 * -(abs(relative.y) / abs(relative.x));
velocity.y += relative.y > 0 ? 5 : -5;
}
else if (abs(relative.x) > 0) {
velocity.x += relative.x > 0 ? 5 : -5;
velocity.y += relative.y > 0 ? 5 : -5;
}
printf("%f, %f \n", velocity.x, velocity.y);
}
velocity.x = Lerp(velocity.x, 0, 0.1);
velocity.y = Lerp(velocity.y, 0, 0.1);
velocity.x = Clamp(velocity.x, -50, 50);
velocity.y = Clamp(velocity.y, -50, 50);
ballPosition.x += velocity.x;
ballPosition.y += velocity.y;