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:


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;
}
 
Why are you trying to avoid tracking the x and y velocity? The canonical way to do this would be to track the instantaneous x, y, vx, and vy of the object, and at each frame update vx and vy based on the x acceleration (presumably 0) and the y acceleration (presumably g), then update x and y (x' = x + vx * time_ms, y' = y + vy * time_ms).
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement