Rotated Rectangle collision

So I have a custom Shape object with intersection for points, but I want to test the shapes for collision with a rotated rectangle. I have looked all over, including AffineTransform, Path2D, etc. but I couldn’t find a solution to my problem.

Here is the Shape class. create() finalizes the shape:


package game.util;

import java.awt.geom.Line2D;
import java.util.ArrayList;

import static org.lwjgl.opengl.GL11.*;

public class Shape {
	
	private ArrayList<Coordinate> points = new ArrayList<Coordinate>();
	private ArrayList<Line2D.Float> intersectionTest = new ArrayList<Line2D.Float>(); 
	
	public Shape(){
	}
	
	public void addPoint(float x, float y){
		points.add(new Coordinate(x, y));
		if(points.size() >= 2){
			Coordinate last = points.get(points.size() - 2);
			intersectionTest.add(new Line2D.Float(last.x, last.y, x, y));
		}
	}
	public void create(){
		// last point
		Coordinate last = points.get(points.size() - 1);
		Coordinate first = points.get(0);
		intersectionTest.add(new Line2D.Float(last.x, last.y, first.x, first.y));
	}
	
	public boolean intersects(float x, float y){
		int intersections = 0;
		Line2D.Float original = new Line2D.Float(x - 1000, y - 1000, x, y);
		for(Line2D.Float intersectionTestLine : intersectionTest){
			if(original.intersectsLine(intersectionTestLine)){
				intersections++;
			}
		}
		return intersections > 0 && intersections % 2 == 1;
	}
	public boolean intersects(Shape shape){
		for(Line2D.Float intersectionTestLine : intersectionTest){
			for(Line2D.Float intersectionOtherLine : shape.intersectionTest){
				if(intersectionOtherLine.intersectsLine(intersectionTestLine)){
					return true;
				}
			}
		}
		return false;
	}
	
	private static class Coordinate {
		private float x, y;
		public Coordinate(float x, float y){
			this.x = x;
			this.y = y;
		}
	}
	
	public void debug(){
		glColor3f(0.0f, 0.0f, 1.0f);
		glBegin(GL_LINE_STRIP);
		for(int index = 0; index < intersectionTest.size(); index++){
			Line2D.Float line = intersectionTest.get(index);
			drawLine(line);
		}
		glEnd();
		glColor3f(1.0f, 1.0f, 1.0f);
	}
	private void drawLine(Line2D.Float line){
		glVertex2f(line.x1, line.y1);
		glVertex2f(line.x2, line.y2);
	}

}

And here is what I have so far for my rotated rectangle, however it doesn’t seem to work…


package game.util;

import java.awt.geom.Line2D;

import static org.lwjgl.opengl.GL11.*;

public class RotatingRectangle {
	
	private float x1, x2, x3, x4, y1, y2, y3, y4, x, y, width, height;
	private Line2D.Float line1, line2, line3, line4;
	private Shape shape;
	
	public RotatingRectangle(float x, float y, float width, float height){
		x1 = x;
		x2 = x + width;
		x3 = x + width;
		x4 = x;
		y1 = y;
		y2 = y;
		y3 = y + height;
		y4 = y + height;
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public void setCenterX(float cx){
		x = cx - (width / 2);
	}
	public void setCenterY(float cy){
		y = cy - (height / 2);
	}
	
	public void debug(){
		glColor3f(1.0f, 0.0f, 0.0f);
		glBegin(GL_LINE_STRIP);
		glVertex2f(x1, y1);
		glVertex2f(x2, y2);
		glVertex2f(x3, y3);
		glVertex2f(x4, y4);
		glVertex2f(x1, y1);
		glEnd();
		glColor3f(1.0f, 1.0f, 1.0f);
	}
	
	public RotatingRectangle rotate(float radians){
		// rotate each point
		Point p1 = rotate(radians, x, y);
		x1 = p1.x;
		y1 = p1.y;
		Point p2 = rotate(radians, x + width, y);
		x2 = p2.x;
		y2 = p2.y;
		Point p3 = rotate(radians, x + width, y + height);
		x3 = p3.x;
		y3 = p3.y;
		Point p4 = rotate(radians, x, y + height);
		x4 = p4.x;
		y4 = p4.y;	
		createShape();
		return this;
	}
	
	public void createShape(){
		shape = new Shape();
		shape.addPoint(x1, y1);
		shape.addPoint(x2, y2);
		shape.addPoint(x3, y3);
		shape.addPoint(x4, y4);
		shape.create();
	}
	
	public boolean intersects(Shape shape_){
		return shape.intersects(shape_);
	}
	
	// You can only rotate a shape once
	public Point rotate(float radians, float x, float y){
		float cx = this.x + (width / 2);
		float cy = this.y + (height / 2);
		float dx = cx - x;
		float dy = cy - y;
		float hypotenuse = (float) StrictMath.sqrt(dx * dx + dy * dy);
		float oldAngle = (float) Math.tan(dx / dy);
		float ndx = hypotenuse * (float) Math.sin(oldAngle + radians);
		float ndy = hypotenuse * (float) Math.cos(oldAngle + radians);
		float nx = ndx + cx;
		float ny = ndy + cy;
		return new Point(nx, ny);
	}
	
	private static class Point {
		
		private float x, y;
		public Point(float x, float y){
			this.x = x;
			this.y = y;
		}
		
	}

}

Sorry that the code is so all over the place. Any help with the collision would be appreciated. (Also, the Shape object collision seems to work 100% correctly, rotated rectangle seems to be where the problem lies)

CopyableCougar4