Skip to content

Commit c645611

Browse files
committed
added test b2AABB_RayCast
1 parent 415d38e commit c645611

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

src/Box2D.NET/B2AABBs.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ public static B2CastOutput b2AABB_RayCast(B2AABB a, B2Vec2 p1, B2Vec2 p2)
107107

108108
if (t1 > t2)
109109
{
110-
float tmp = t1;
111-
t1 = t2;
112-
t2 = tmp;
110+
(t1, t2) = (t2, t1);
113111
s = 1.0f;
114112
}
115113

@@ -150,9 +148,7 @@ public static B2CastOutput b2AABB_RayCast(B2AABB a, B2Vec2 p1, B2Vec2 p2)
150148

151149
if (t1 > t2)
152150
{
153-
float tmp = t1;
154-
t1 = t2;
155-
t2 = tmp;
151+
(t1, t2) = (t2, t1);
156152
s = 1.0f;
157153
}
158154

test/Box2D.NET.Test/B2AABBTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,55 @@ public void Test_b2IsValidAABB()
205205
a5.upperBound = new B2Vec2(5, 5);
206206
Assert.That(b2IsValidAABB(a5), Is.False, "AABB with Infinity value should be invalid.");
207207
}
208+
209+
[Test]
210+
public void Test_b2AABB_RayCast()
211+
{
212+
// Case 1: Ray does not intersect the AABB (outside)
213+
var aabb1 = new B2AABB();
214+
aabb1.lowerBound = new B2Vec2(1, 1);
215+
aabb1.upperBound = new B2Vec2(3, 3);
216+
var startPoint1 = new B2Vec2(0, 0); // Start point outside AABB
217+
var endPoint1 = new B2Vec2(0, 4); // End point (ray direction)
218+
var result1 = b2AABB_RayCast(aabb1, startPoint1, endPoint1);
219+
Assert.That(result1.hit, Is.False, "Ray should not intersect the AABB.");
220+
221+
// Case 2: Ray intersects AABB (inside AABB)
222+
var aabb2 = new B2AABB();
223+
aabb2.lowerBound = new B2Vec2(1, 1);
224+
aabb2.upperBound = new B2Vec2(3, 3);
225+
var startPoint2 = new B2Vec2(0, 0); // Start point outside AABB
226+
var endPoint2 = new B2Vec2(2, 2); // End point (ray direction)
227+
var result2 = b2AABB_RayCast(aabb2, startPoint2, endPoint2);
228+
Assert.That(result2.hit, Is.True, "Ray should intersect the AABB.");
229+
Assert.That(result2.fraction, Is.EqualTo(0.5f), "Intersection point should be correct.");
230+
231+
// Case 3: Ray starts inside the AABB but goes outside (no intersection outside)
232+
var aabb3 = new B2AABB();
233+
aabb3.lowerBound = new B2Vec2(1, 1);
234+
aabb3.upperBound = new B2Vec2(3, 3);
235+
var startPoint3 = new B2Vec2(2, 2); // Start point inside AABB
236+
var endPoint3 = new B2Vec2(4, 4); // End point (ray direction goes outside)
237+
var result3 = b2AABB_RayCast(aabb3, startPoint3, endPoint3);
238+
Assert.That(result3.hit, Is.False, "Ray starting inside but going outside should not intersect.");
239+
240+
// Case 4: Ray intersects AABB at boundary (exactly on the edge)
241+
var aabb4 = new B2AABB();
242+
aabb4.lowerBound = new B2Vec2(1, 1);
243+
aabb4.upperBound = new B2Vec2(3, 3);
244+
var startPoint4 = new B2Vec2(0, 1); // Start point at edge of AABB
245+
var endPoint4 = new B2Vec2(4, 1); // End point (ray direction, horizontal)
246+
var result4 = b2AABB_RayCast(aabb4, startPoint4, endPoint4);
247+
Assert.That(result4.hit, Is.True, "Ray should intersect at the boundary of the AABB.");
248+
Assert.That(result4.fraction, Is.EqualTo(0.25f), "Intersection fraction should be 0.25 at the boundary.");
249+
250+
// Case 5: Ray starts inside and goes out (no intersection outside)
251+
var aabb5 = new B2AABB();
252+
aabb5.lowerBound = new B2Vec2(1, 1);
253+
aabb5.upperBound = new B2Vec2(3, 3);
254+
var startPoint5 = new B2Vec2(2, 2); // Start inside AABB
255+
var endPoint5 = new B2Vec2(4, 4); // End point (ray direction goes outside)
256+
var result5 = b2AABB_RayCast(aabb5, startPoint5, endPoint5);
257+
Assert.That(result5.hit, Is.False, "Ray going out of AABB should not intersect.");
258+
}
208259
}

0 commit comments

Comments
 (0)