Search code examples
javamathpoints

How to calculate points on a line at intervals


I am coding a game and want a projectile to go from one location to the next moving at intervals every frame.

I've been playing around with the slope-intercept method of determining things and I'm getting close, but I am stuck.

Here is my code so far:

animationFrame = refresh;
    double x, y, xPerF; //Values for drawing
    double m, b; //Value for slope and y-intercept
    double x1, x2, y1, y2; //Values for the targets
    x1 = getCenterX();
    x2 = Canvas.target[shotTarget].getCenterX();
    y1 = getCenterY();
    y2 = Canvas.target[shotTarget].getCenterY();
    xPerF = Point2D.distance(x1, y1, x2, y2)/animationSpeed;
    //Calculate slope
    if(x2>x1) m = (y2-y1)/(x2-x1);
    else if(x2<x1) m = (y1-y2)/(x1-x2);
    else m = 0;
    //Calculate the y-intercept
    b = m * x1 - y1;
    if(b<0) b = -b + Canvas.myHeight;
    else {
        b -= Canvas.myHeight;
        if(b<0) b = -b;
    }
    //Calculate the x value
    if(x1>x2) x = x1 - (xPerF * animationFrame);
    else if(x1<x2) x = x1 + (xPerF * animationFrame);
    else x = x1;
    //Calculate the y value
    if(m!=0) y = (m * x + b) - Canvas.myHeight;
    else {
        if(y1>y2) y = y1 - (xPerF * animationFrame);
    else y = y1 + (xPerF * animationFrame);
    }
    g.fillOval((int) x - 15, (int) y - 15, 30, 30);
    //Debugging
    System.out.println("Frame " + animationFrame + " of " + animationSpeed + " | " + y + " = " + m + " * " + x + " + " + b + " | at speed of " + xPerF);

Updated

I expect the animation to end at the target location, but it always either overshoots or is right on target. It mainly overshoots when the target is pretty kind of straight above the tower, give or take a few x co-ordinates. I have worked this out to be a quadrant 1 x-y plane and I believe the problem I have now lies with how I am calculating my slope. Thanks!

Outdated

Here is a mini applet to demonstrate: https://drive.google.com/file/d/1fCTFJzulY1fcBUmdV6AXOd7Ol1g9B3lo/view?usp=sharing

Click on each target to target it


Solution

  • Answer

    I figured it out. Instead of calculating the coordinates with slope-intercept method, I simply calculated the intervals I would have to increment y and x per frame and incremented them based on the frame of the animation.

    double x, y, xPerF, yPerF; //Values for drawing
    double x1, x2, y1, y2; //Values for the targets
    x1 = getCenterX();
    x2 = Canvas.target[shotTarget].getCenterX();
    y1 = getCenterY();
    y2 = Canvas.target[shotTarget].getCenterY();
    xPerF = (Math.max(x1, x2) - Math.min(x1, x2))/animationSpeed;
    yPerF = (Math.max(y1, y2) - Math.min(y1, y2))/animationSpeed;
    if(x1>x2) x = x1 - xPerF * animationFrame;
    else if(x1<x2) x = x1 + xPerF * animationFrame;
    else x = x1;
    if(y1>y2) y = y1 - yPerF * animationFrame;
    else if(y1<y2) y = y1 + yPerF * animationFrame;
    else y = y1;
    g.fillOval((int) x - 15, (int) y - 15, 30, 30);