Point is a struct with x and a y. This function is running every frame and time_ms is the time between the frames. x0 and y0 are the player x and y and are dynamic. It should arc between the two points. If x0>x1 it works great, otherwise the arc goes down instead of up, they both reach the correct ending point. Excuse my terrible code please.
I already tried reversing y and a and stuff but then it gets very unstable near the target and shoots off the screen.
Source code
https://pastebin.com/REWDruz5
Code:
I already tried reversing y and a and stuff but then it gets very unstable near the target and shoots off the screen.
Source code
https://pastebin.com/REWDruz5
Code:
Point calculateTrajectory(double x0, double y0, double x1, double y1, double time_ms, int originalX0, int originalY0) {
double t_travel = (fabs(originalX0 - x1) / 70.0) + .1; //seconds // completely arbitrary
double v0 = (x1 - x0) / t_travel;
double a = 2 * (y1 - y0 - v0 * t_travel) / (t_travel * t_travel);
double vx = v0;
double t = time_ms / 1000.0; // Convert time_passed to seconds
// Calculate the y-coordinate of the control point
double control_point_y = y0 + 1 * (y1 - y0);
Point point;
point.x = x0 + vx * t;
point.y = y0 + v0 * t + 0.5 * a * t * t;
// Adjust the y-coordinate based on the height_factor
point.y += (control_point_y - point.y) * (2 * t / t_travel) * (1 - t / t_travel);
//snap
if(point.y >= y1 - 1 && point.y <= y1 + 1){
point.y = y1;
}
if(point.x >= x1 - 1 && point.x <= x1 + 1){
point.x = x1;
}
return point;
}