Right now, I can compare the X and Y to check for collision, but that\'s only if the two objects pass right through each other, on exactly the same X & Y pos. I need to chec
Elist was/is a huge help! However I found this created a large square around my first square and messed up the collisions. Here is my implementation based off of Elist's post.
public boolean colliding(Square os)
{
// the lesser value is the distance of the square's width and that of the lenght of the
// other square
return Math.abs(center.x - os.center.x) < width / 2 + os.width / 2
&& Math.abs(center.y - os.center.y) < height / 2 + os.width / 2;
}
All due respect to Elist, I would not have come to this conclusion without their post.
Additional help for those interested would be a handler that can stop the incoming square, and one that reflects it based on the other squares velocity.
// stops the square from intersecting
public void push_collided_basic(Square os, float refelct_amnt)
{
if(os.center.x < center.x)
os.center.x -= refelct_amnt;
else if(os.center.x > center.x)
os.center.x += refelct_amnt;
if(os.center.y < center.y)
os.center.y -= refelct_amnt;
else if(os.center.y > center.y)
os.center.y += refelct_amnt;
}
// and now one that reflects the ball -> untested!!!
public void push_collided_velocity(Square os)
{
// flip the velocitys directions for x
if(os.center.x < center.x
|| os.center.x > center.x)
os.vel_x *= -1;
// flip the velocity direction for y
if(os.center.y < center.y
|| os.center.y > center.y)
os.vel_y *= -1;
// update the coordiantes
os.center.x += vel_x;
os.center.y += vel_y;
}
If your squares can't rotate, it's easy: say double r
is the length of every edge, Point p1
is the center of one square, and p2
is of the other. then:
if (Math.abs(p1.x - p2.x) < r && Math.abs(p1.y - p2.y) < r) {
// Collision
}
The more complex case is if a square might be rotated. In such case: treat each edge of the objects as a geometric line (you can easily compute each line's equation if you know the coordinates of the corners).
Next, find the meeting point of every couple of lines (each from one square against each from the other one), and test if this point is inside one of the squares. If one of those comparisons return true - a collision occurred.
If the rectangles can rotate, all you have to do is change your coordinate system so one of them is axis-aligned, and then check each of the vertices of the other one to see if it (the vertex) is inside the axis-aligned rectangle. You can change your coordinate system by rotating all the vertices of both rectangles around the same point. The angle of this rotation should be the negation of the angle of rotation of the rotated rectangle. E.g. if one rectangle is tilted by 13°, you would rotate the vertices of both rectangles by -13° around the same point.