A
Abs
Hi!
I have two draggable Rectangle shapes in a JPanel and I'd like
to perform collision detection between them. Right now I'm using
the following algorithm to detect their intersection:
public boolean intersects(RectangleShape r) {
int tw = this._w;
int th = this._h;
int rw = r._w;
int rh = r._h;
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return false;
}
int tx = this._x;
int ty = this._y;
int rx = r._x;
int ry = r._y;
rw = rw+rx;
rh = rh+ry;
tw = tw+tx;
th = th+ty;
if ((rw < rx || rw > tx) &&
(rh < ry || rh > ty) &&
(tw < tx || tw > rx) &&
(th < ty || th > ry)) {
return true;
} else {
return false;
}
}
This algorithm detects the intersection but does nothing to avoid
the overlapping between the two shapes. How can I reposition the
moving shape ("this") to prevent it from overlapping the other one ? Can
anyone help me, please ?
Thanks
I have two draggable Rectangle shapes in a JPanel and I'd like
to perform collision detection between them. Right now I'm using
the following algorithm to detect their intersection:
public boolean intersects(RectangleShape r) {
int tw = this._w;
int th = this._h;
int rw = r._w;
int rh = r._h;
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return false;
}
int tx = this._x;
int ty = this._y;
int rx = r._x;
int ry = r._y;
rw = rw+rx;
rh = rh+ry;
tw = tw+tx;
th = th+ty;
if ((rw < rx || rw > tx) &&
(rh < ry || rh > ty) &&
(tw < tx || tw > rx) &&
(th < ty || th > ry)) {
return true;
} else {
return false;
}
}
This algorithm detects the intersection but does nothing to avoid
the overlapping between the two shapes. How can I reposition the
moving shape ("this") to prevent it from overlapping the other one ? Can
anyone help me, please ?
Thanks