Search code examples
java2dintersectionrectangles

java find intersection of Line and Rectangle


So, i am programming a small game. As the player, you fly a spaceship in 2d space. I want to have a marker that points at a selected Object like a sun or a planet, if selected in the scanner. Everything of that works fine except the drawing of the Marker at the screen border.

Here is my programm so far:

i first painted a Marker Marker

In order to rotate the Marker, to let it point in direction of the object which is out of the screen, i use this Algorythm.

double Marker_vec = Math.toDegrees(Vector2F.getAngle( SolarSystem.object_vectors[marker],center,player.pos));

public static double getAngle(Vector2F v1, Vector2F v2, Vector2F fixed)
{

    double angle1 = Math.atan2(v1.ypos - fixed.ypos, v1.xpos - fixed.xpos);
    double angle2 = Math.atan2(v2.ypos - fixed.ypos, v2.xpos - fixed.xpos);
    return angle1 - angle2;
}

Basically what i do is this, take a fixed point on the left side of the screen, the fixed point of the Players ship and the Object and get the Angle inbetween those two lines. The resulting value is the degrees i have to turn my marker in. And that works fine

Get angle

My problem is to display it on the edge of the screen.

My attempt to do so is that i create a Rectangle that fits the Screen:

screen_rec = new Rectangle(0,0,Main.width-1,Main.height-1);

Then i cut used the rectangles values of X,Y, width and height to create 4 Lines which together form the Rectangle around the screen. Now i want to see if a line, drawn between the player and the selected Object intersects with any of the lines of the rectangle and get that point. And finally display the rotated markerimage at those coordinates.

Here is my Code for that

marker_vec = Vector2F.getIntersectionPoint(line, Player.screen_rec);

public static Point intersection(Line2D lineA, Line2D lineB) 
 {
     double x1 = lineA.getX1();
     double y1 = lineA.getY1();

     double x2 = lineA.getX2();
     double y2 = lineA.getY2();

     double x3 = lineB.getX1();
     double y3 = lineB.getY1();

     double x4 = lineB.getX2();
     double y4 = lineB.getY2();

    double d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
    if (d == 0) return null;

    double xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d;
    double yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d;
    return new Point((int)xi,(int)yi);
 }

 public static Point[] getIntersectionPoint(Line2D line, Rectangle2D rectangle) {

    Point[] p1 = new Point[4];



     // Top line
     p1[0] = intersection(line,
                     new Line2D.Double(
                     rectangle.getX(),
                     rectangle.getY(),
                     rectangle.getX() + rectangle.getWidth(),
                     rectangle.getY()));
     // Bottom line
     p1[1] = intersection(line,
                     new Line2D.Double(
                     rectangle.getX(),
                     rectangle.getY() + rectangle.getHeight(),
                     rectangle.getX() + rectangle.getWidth(),
                     rectangle.getY() + rectangle.getHeight()));
     // Left side...
     p1[2] = intersection(line,
                     new Line2D.Double(
                     rectangle.getX(),
                     rectangle.getY(),
                     rectangle.getX(),
                     rectangle.getY() + rectangle.getHeight()));
     // Right side
     p1[3] = intersection(line,
                     new Line2D.Double(
                     rectangle.getX() + rectangle.getWidth(),
                     rectangle.getY(),
                     rectangle.getX() + rectangle.getWidth(),
                     rectangle.getY() + rectangle.getHeight()));




     return p1;

 }

But somehow the Marker is displayed only at the upper and left sceenside. I tried to solve it by subtracting the width in the xaxis in the drawing function, but that didnt work. And those lines shouldnt have any intersectionpoints anyways, because the line between the player and the object does not intersect that rectangleline. I will post images of how it looks in the comments I just tried for serveral days and cant find any solution. Thanks in advance

Spytrycer


Solution

  • I solved it. I got rid of the line:

    if (d == 0) return null;
    

    And the While where i drew the Markers was not going long enough.

    Plus, i surrounded the draw functions with if and made sure the markers are only shown when the Angle is a specific value.