I don’t know if you are looking for something like this, but here:
This is a collision detection for rotating rectangle collision.
public class RotatingRegion {
private Line2D.Float[] lines = new Line2D.Float[4];
public float cx, cy;
private float angle;
private float x, y, width, height;
@@ public RotatingRegion(float x, float y, float width, float height){
this(x, y, width, height, 0);
}
@@ public RotatingRegion(float x, float y, float width, float height, float angle){
cx = x + (width / 2);
cy = y + (height / 2);
this.angle = angle;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
build();
}
public float angle(){
return (float)Math.toDegrees(angle);
}
public void setCenter(float cx, float cy){
this.cx = cx;
this.cy = cy;
x = cx - (width / 2);
y = cy - (height / 2);
build();
}
@@ public void angle(float angle){
this.angle = angle;
build();
}
public void build(){
float[] point1 = rotateAroundCenter(x, y, angle);
float[] point2 = rotateAroundCenter(x + width, y, angle);
float[] point3 = rotateAroundCenter(x + width, y + height, angle);
float[] point4 = rotateAroundCenter(x, y + height, angle);
lines[0] = new Line2D.Float(point1[0], point1[1], point2[0], point2[1]);
lines[1] = new Line2D.Float(point2[0], point2[1], point3[0], point3[1]);
lines[2] = new Line2D.Float(point3[0], point3[1], point4[0], point4[1]);
lines[3] = new Line2D.Float(point4[0], point4[1], point1[0], point1[1]);
}
public float[] rotateAroundCenter(float x, float y, float angle){
float dx = x - cx;
float dy = y - cy;
float distance = (float) Math.sqrt(dx * dx + dy * dy);
float originalAngle = getAngle(cx, cy, x, y);
float newangle = originalAngle + angle;
float ndx = distance * (float)Math.sin(newangle);
float ndy = distance * (float)Math.cos(newangle);
float nx = cx + ndx;
float ny = cy + ndy;
return new float[]{nx, ny};
}
public boolean intersects(float x, float y){
for(int index = 0; index < lines.length; index++){
if(lines[index].contains(x, y)){
return true;
}
}
return false;
}
public boolean contains(float x, float y){
int intersections = 0;
Line2D.Float test = new Line2D.Float(x - 1000, y - 1000, x, y);
for(int index = 0; index < lines.length; index++){
if(lines[index].intersectsLine(test)){
intersections++;
}
}
return intersections > 0 && intersections % 2 == 1;
}
@@ public boolean contains(RotatingRegion region){
for(int index1 = 0; index1 < region.lines.length; index1++){
for(int index = 0; index < lines.length; index++){
if(lines[index].intersectsLine(region.lines[index1])){
return true;
}
}
}
return false;
}
public static float getAngle(float mousex, float mousey, float playerx, float playery){
float deltax = mousex - playerx;
float deltay = mousey - playery;
double rawangle = Math.atan2(deltay, deltax);
float degrees = (float) Math.toDegrees(rawangle);
degrees += 90.0f; // add 90 so opengl can use it
if(degrees < 0){
degrees += 360.0f; // fix all angles less than 0
}
if(degrees > 360.0f){
degrees -= 360.0f; // fix all angles more than 360
}
degrees %= 360.0f;
return (float) Math.toRadians(degrees);
}
}
CopyableCougar4