@@ -9,25 +9,24 @@ import (
99// BoxCircleSweep2 checks for collision between a moving AABB and a moving Circle.
1010//
1111// Returns true if collision occurs during movement, false otherwise.
12- func BoxCircleSweep2 (box * AABB , circle * Circle , boxVel , circleVel v.Vec , h * Hit ) bool {
13- // Relative velocity (circle relative to box)
14- relVelX := circleVel .X - boxVel .X
15- relVelY := circleVel .Y - boxVel .Y
12+ func BoxCircleSweep2 (a * AABB , b * Circle , deltaA , deltaB v.Vec , h * Hit ) bool {
13+ relDeltaX := deltaB .X - deltaA .X
14+ relDeltaY := deltaB .Y - deltaA .Y
1615
1716 // If no relative movement, check overlap directly
18- relVelMagSq := relVelX * relVelX + relVelY * relVelY
19- if relVelMagSq < 1e-8 {
17+ relDeltaMagSq := relDeltaX * relDeltaX + relDeltaY * relDeltaY
18+ if relDeltaMagSq < 1e-8 {
2019 // Find closest point on AABB to circle center
21- closestX := math .Max (box .Pos .X - box .Half .X , math .Min (circle .Pos .X , box .Pos .X + box .Half .X ))
22- closestY := math .Max (box .Pos .Y - box .Half .Y , math .Min (circle .Pos .Y , box .Pos .Y + box .Half .Y ))
20+ closestX := math .Max (a .Pos .X - a .Half .X , math .Min (b .Pos .X , a .Pos .X + a .Half .X ))
21+ closestY := math .Max (a .Pos .Y - a .Half .Y , math .Min (b .Pos .Y , a .Pos .Y + a .Half .Y ))
2322
2423 // Vector from closest point to circle center
25- distX := circle .Pos .X - closestX
26- distY := circle .Pos .Y - closestY
24+ distX := b .Pos .X - closestX
25+ distY := b .Pos .Y - closestY
2726 distSq := distX * distX + distY * distY
2827
2928 // Check if overlapping
30- if distSq >= circle .Radius * circle .Radius {
29+ if distSq >= b .Radius * b .Radius {
3130 return false
3231 }
3332
@@ -37,10 +36,10 @@ func BoxCircleSweep2(box *AABB, circle *Circle, boxVel, circleVel v.Vec, h *Hit)
3736 // If circle center is inside box
3837 if distSq < 1e-8 {
3938 // Calculate penetration depths for each side
40- leftDist := (circle .Pos .X - (box .Pos .X - box .Half .X )) + circle .Radius
41- rightDist := ((box .Pos .X + box .Half .X ) - circle .Pos .X ) + circle .Radius
42- topDist := (circle .Pos .Y - (box .Pos .Y - box .Half .Y )) + circle .Radius
43- bottomDist := ((box .Pos .Y + box .Half .Y ) - circle .Pos .Y ) + circle .Radius
39+ leftDist := (b .Pos .X - (a .Pos .X - a .Half .X )) + b .Radius
40+ rightDist := ((a .Pos .X + a .Half .X ) - b .Pos .X ) + b .Radius
41+ topDist := (b .Pos .Y - (a .Pos .Y - a .Half .Y )) + b .Radius
42+ bottomDist := ((a .Pos .Y + a .Half .Y ) - b .Pos .Y ) + b .Radius
4443
4544 // Find minimum penetration
4645 minDist := math .Min (math .Min (leftDist , rightDist ), math .Min (topDist , bottomDist ))
@@ -71,42 +70,42 @@ func BoxCircleSweep2(box *AABB, circle *Circle, boxVel, circleVel v.Vec, h *Hit)
7170 }
7271
7372 // Expanded box bounds (box + circle radius)
74- halfX := box .Half .X + circle .Radius
75- halfY := box .Half .Y + circle .Radius
76- boxMinX := box .Pos .X - halfX
77- boxMaxX := box .Pos .X + halfX
78- boxMinY := box .Pos .Y - halfY
79- boxMaxY := box .Pos .Y + halfY
73+ halfX := a .Half .X + b .Radius
74+ halfY := a .Half .Y + b .Radius
75+ boxMinX := a .Pos .X - halfX
76+ boxMaxX := a .Pos .X + halfX
77+ boxMinY := a .Pos .Y - halfY
78+ boxMaxY := a .Pos .Y + halfY
8079
8180 // AABB ray intersection (slab method)
8281 var tminX , tmaxX , tminY , tmaxY float64
8382 var hitX , hitY bool
8483
85- if math .Abs (relVelX ) > 1e-8 {
86- invDirX := 1.0 / relVelX
87- t1 := (boxMinX - circle .Pos .X ) * invDirX
88- t2 := (boxMaxX - circle .Pos .X ) * invDirX
84+ if math .Abs (relDeltaX ) > 1e-8 {
85+ invDirX := 1.0 / relDeltaX
86+ t1 := (boxMinX - b .Pos .X ) * invDirX
87+ t2 := (boxMaxX - b .Pos .X ) * invDirX
8988 tminX = min (t1 , t2 )
9089 tmaxX = max (t1 , t2 )
9190 hitX = true
9291 } else {
93- if circle .Pos .X < boxMinX || circle .Pos .X > boxMaxX {
92+ if b .Pos .X < boxMinX || b .Pos .X > boxMaxX {
9493 return false
9594 }
9695 tminX = - math .MaxFloat64
9796 tmaxX = math .MaxFloat64
9897 hitX = false
9998 }
10099
101- if math .Abs (relVelY ) > 1e-8 {
102- invDirY := 1.0 / relVelY
103- t3 := (boxMinY - circle .Pos .Y ) * invDirY
104- t4 := (boxMaxY - circle .Pos .Y ) * invDirY
100+ if math .Abs (relDeltaY ) > 1e-8 {
101+ invDirY := 1.0 / relDeltaY
102+ t3 := (boxMinY - b .Pos .Y ) * invDirY
103+ t4 := (boxMaxY - b .Pos .Y ) * invDirY
105104 tminY = min (t3 , t4 )
106105 tmaxY = max (t3 , t4 )
107106 hitY = true
108107 } else {
109- if circle .Pos .Y < boxMinY || circle .Pos .Y > boxMaxY {
108+ if b .Pos .Y < boxMinY || b .Pos .Y > boxMaxY {
110109 return false
111110 }
112111 tminY = - math .MaxFloat64
@@ -125,16 +124,16 @@ func BoxCircleSweep2(box *AABB, circle *Circle, boxVel, circleVel v.Vec, h *Hit)
125124 // Already overlapping at t=0
126125 if tmin < 0 {
127126 // Find closest point on AABB to circle center
128- closestX := math .Max (box .Pos .X - box .Half .X , math .Min (circle .Pos .X , box .Pos .X + box .Half .X ))
129- closestY := math .Max (box .Pos .Y - box .Half .Y , math .Min (circle .Pos .Y , box .Pos .Y + box .Half .Y ))
127+ closestX := math .Max (a .Pos .X - a .Half .X , math .Min (b .Pos .X , a .Pos .X + a .Half .X ))
128+ closestY := math .Max (a .Pos .Y - a .Half .Y , math .Min (b .Pos .Y , a .Pos .Y + a .Half .Y ))
130129
131130 // Vector from closest point to circle center
132- distX := circle .Pos .X - closestX
133- distY := circle .Pos .Y - closestY
131+ distX := b .Pos .X - closestX
132+ distY := b .Pos .Y - closestY
134133 distSq := distX * distX + distY * distY
135134
136135 // Check if overlapping
137- if distSq >= circle .Radius * circle .Radius {
136+ if distSq >= b .Radius * b .Radius {
138137 return false
139138 }
140139
@@ -144,10 +143,10 @@ func BoxCircleSweep2(box *AABB, circle *Circle, boxVel, circleVel v.Vec, h *Hit)
144143 // If circle center is inside box
145144 if distSq < 1e-8 {
146145 // Calculate penetration depths for each side
147- leftDist := (circle .Pos .X - (box .Pos .X - box .Half .X )) + circle .Radius
148- rightDist := ((box .Pos .X + box .Half .X ) - circle .Pos .X ) + circle .Radius
149- topDist := (circle .Pos .Y - (box .Pos .Y - box .Half .Y )) + circle .Radius
150- bottomDist := ((box .Pos .Y + box .Half .Y ) - circle .Pos .Y ) + circle .Radius
146+ leftDist := (b .Pos .X - (a .Pos .X - a .Half .X )) + b .Radius
147+ rightDist := ((a .Pos .X + a .Half .X ) - b .Pos .X ) + b .Radius
148+ topDist := (b .Pos .Y - (a .Pos .Y - a .Half .Y )) + b .Radius
149+ bottomDist := ((a .Pos .Y + a .Half .Y ) - b .Pos .Y ) + b .Radius
151150
152151 // Find minimum penetration
153152 minDist := math .Min (math .Min (leftDist , rightDist ), math .Min (topDist , bottomDist ))
@@ -185,19 +184,19 @@ func BoxCircleSweep2(box *AABB, circle *Circle, boxVel, circleVel v.Vec, h *Hit)
185184 if ! hitX {
186185 // Only Y axis moving
187186 h .Normal .X = 0
188- h .Normal .Y = math .Copysign (1 , - relVelY )
187+ h .Normal .Y = math .Copysign (1 , - relDeltaY )
189188 } else if ! hitY {
190189 // Only X axis moving
191- h .Normal .X = math .Copysign (1 , - relVelX )
190+ h .Normal .X = math .Copysign (1 , - relDeltaX )
192191 h .Normal .Y = 0
193192 } else if tminX > tminY {
194193 // X axis constrained the hit
195- h .Normal .X = math .Copysign (1 , - relVelX )
194+ h .Normal .X = math .Copysign (1 , - relDeltaX )
196195 h .Normal .Y = 0
197196 } else {
198197 // Y axis constrained the hit
199198 h .Normal .X = 0
200- h .Normal .Y = math .Copysign (1 , - relVelY )
199+ h .Normal .Y = math .Copysign (1 , - relDeltaY )
201200 }
202201
203202 h .Data = tmin
0 commit comments