How to avoid the intersection of 2 objects

Asked

Viewed 56 times

0

I have a game problem I’m developing. The problem is, I’ve got two planes that can stand on top of each other and I don’t know what to do, any suggestions?

Plane code1

int vBola=550;
int CBola=600;
Rectangle aviao11 = new Rectangle(vBola,CBola,x2,y2);

Plane code2

int xBola=750;
int yBola=600;
Rectangle aviao22 = new Rectangle(xBola,yBola,x2,y2);

A little code so the plane can’t get off the screen

       if(!TeclasPressionadas[68].equals("")) 
       if (vBola>=1278)
       vBola = vBola +0;
               else
           vBola=vBola+7;

My problem is how to implement for both planes

1 answer

2

Create specific methods for this:

public static boolean colidiu(double a1, double w1, double a2, double w2) {
     return (a1 <= a2 && a2 <= a1 + w1) || (a2 <= a1 && a1 <= a2 + w2);
}

public static boolean colidiu(Rectangle a, Rectangle b) {
     return colidiu(a.getX(), a.getWidth(), b.getX(), b.getWidth())
             && colidiu(a.getY(), a.getHeight(), b.getY(), b.getHeight());
}

The first method checks whether two line segments in the position+length form collide.

The second method is to check if two rectangles collide. They collide if their dimensions in X and Y collide simultaneously.

Furthermore, I must warn you that your approach does not seem to be a good object-oriented approach, but that is content for another issue and would require knowing more of your code so I can at least have a better opinion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.