@@ -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