Damian Galli
Damian Galli

Reputation: 35

Prevent overlapping from rectangles

I'm currently trying to make a small Entity-Relationship-Modell creating tool for Databases. To avoid errors I try to don't allow the rectangles to overlap with a other rectangle.

I got most of my code working and it detects almost every collusion but in one case I can still overlap them and I don't know how to fix this error.

The problem is that it will look on only on rectangle not on both. But I really don't know how to intercept that case.

The strange thing to me is that my Debug line

System.out.println("still intersects?");

never triggers. Can somebody help me out in this error?

Image to the error in my Colision

This Function gets triggered every time I move a Rectangle. The attribute Selected is a Boolean which says me which Rectangle I'm moving. Then I check on which side its overlapping and revert back to the old position.

    boolean Move(Point point) {
    boolean moved = false;
    boolean foundone = false;
    for (ERMRectangle rectangle : Rectangles) {
        if (rectangle.selected) {
            foundone = true;
            moved = true;
            //calculated new Rectangle after Move
            Rectangle Temp = new Rectangle();
            Temp.x = point.x + rectangle.click.x;
            Temp.width = rectangle.position.width;
            Temp.height = rectangle.position.height;
            Temp.y = point.y + rectangle.click.y;
            for (ERMRectangle rectangle2 : Rectangles) {
                if (rectangle != rectangle2 && rectangle2.position.intersects(Temp))//prevent overlapping
                {
                    Rectangle intersection = rectangle2.position.intersection(Temp);

                    if (intersection.height > intersection.width) {

                        Temp.x = rectangle2.position.x - rectangle2.position.width;
                        if (intersection.x != rectangle2.position.x) {
                            Temp.x = rectangle2.position.x + rectangle2.position.width;
                        }

                    } else {
                        Temp.y = rectangle2.position.y - rectangle2.position.height;
                        if (intersection.y != rectangle2.position.y) {
                            Temp.y = rectangle2.position.y + rectangle2.position.height;
                        }

                    }

                }
                if(rectangle != rectangle2 && rectangle2.position.intersects(Temp))
                {
                    System.out.println("still intersects?");
                }
            }

            rectangle.position = Temp;
        }
    }

    return moved;
}

ERMRectangle class:

import java.awt.Rectangle; 

public class ERMRectangle {
    public Rectangle position;
    public boolean selected;
    public Point click;
}

Edit: i resolved it by adding one more for loop into my methode which checks if it still interselects the rectangle and reverts both x and y cordinnates.

Upvotes: 2

Views: 1149

Answers (1)

user3437460
user3437460

Reputation: 17454

If at any moment, there will only be one rectangle moving (dragged by the user) there is no need to use nested loops to check for collision n-square times.

All you need is a method like this in your ERMRectangle class:

These codes assume your ERMRectangle class extends Rectangle class from Java

public boolean isColliding(ArrayList<ERMRectangle> rects)
{
    for(ERMRectangle r : rects)
        if(this != r && this.intersects(r))
            return true;
    return false;
}

You can then invoke isColliding() in the mouseReleased(MouseEvent e) in the mouse listener for your rectangles, and trigger the necessary actions when collision is detected. So, upon every mouse release after dragging, it will check for collisions on the rectangle which you have just moved.

To check for collision as you drag, place the similar codes as you would in mouseReleased(MouseEvent e) into mouseDragged(MouseEvent e). This will do a real-time collision detection check as you drag the rectangle.

Upvotes: 1

Related Questions