Bietet folgende Kollisionsüberprüfungen:
- Rechteck - Rechteck (nur für Rotation=0)
- Linie - Punkt
- Rechteck - Punkt
- Linie - Rechteck
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN] Type CollisionUtil Abstract
Function rectInRect:Byte(rect1X:Int, rect1Y:Int, rect1Width:Int, rect1Height:Int, rect2X:Int, rect2Y:Int, rect2Width:Int, rect2Height:Int) If rect1X + rect1Width > rect2X And rect1X < rect2X + rect2Width Then If rect1Y + rect1Height > rect2Y And rect1Y < rect2Y + rect2Height Then Return True End If End If Return False End Function
Function pointOnLine:Byte(lineX1:Int, lineY1:Int, lineX2:Int, lineY2:Int, pointX:Int, pointY:Int) Local result:Int = lineX1 * lineY2 + lineY1 * pointX + lineX2 * pointY - lineX1 * pointY - lineY1 * lineX2 - lineY2 * pointX If result = 0 Then Return True Else Return False End If End Function
Function pointInRect:Byte(x:Int, y:Int, rectX:Int, rectY:Int, rectWidth:Int, rectHeight:Int) If x >= rectX And x <= rectX + rectWidth Then If y >= rectY And y <= rectY + rectHeight Then Return True Else Return False End If Else Return False End If End Function
Function lineInRect:String(lineX1:Int, lineY1:Int, lineX2:Int, lineY2:Int, rectX:Int, rectY:Int, rectWidth:Int, rectHeight:Int) Local m:Double = 0 If (lineX2 - lineX1) <> 0 Then m = Double((lineY2 - lineY1)) / Double((lineX2 - lineX1)) Local c:Double = lineY2 - (m * lineX2) If lineX1 < lineX2 Then For Local x:Int = lineX1 To lineX2 Step 1 Local y:Int = Int(((m * Double(x)) + c)) If CollisionUtil.pointInRect(x, y, rectX, rectY, rectWidth, rectHeight) Then Return String(x) + ";" + String(y) Next End If If lineX1 > lineX2 Then For Local x:Int = lineX1 To lineX2 Step (-1) Local y:Int = Int((m * Double(x)) + c) If CollisionUtil.pointInRect(x, y, rectX, rectY, rectWidth, rectHeight) Then Return String(x) + ";" + String(y) Next End If Return Null End Function End Type
Falls jemandem Fehler auffallen, bitte melden. Ich werde den Beitrag dann editieren.
|