Collision SwiftUI rotated shapes

64 views Asked by At

I'm using CGRect and its intersects(_:) function to see if 2 rectangles collide.

The problem is both rectangles have an angle of var rotationAngle: Angle and that the intersects function doesn't take rotation into account.

So I figured that since both rectangles have the same angle we could make them both paralel to the x axis. That way we didn't rotate anything, but I don't know how to.

This is my code that doesn't take rotation into account:

private func checkCollisions() {
    let airplaneRect = CGRect(
        x: airplane.x - 10,
        y: airplane.y - 60,
        width: 90,
        height: 30
    )
    
    for index in 0..<raindrops.count {
        if raindrops[index].hasCollided {
            continue
        }
        
        let raindropRect = CGRect(
            x: raindrops[index].xPosition - raindrops[index].size / 2,
            y: raindrops[index].yPosition - raindrops[index].size / 2,
            width: raindrops[index].size,
            height: raindrops[index].size
        )
        
        if airplaneRect.intersects(raindropRect) {
            collisionDetected = true
            raindrops[index].hasCollided = true
        }
    }
}
0

There are 0 answers