/// <summary> /// Similar to RayCast() but all intersections, not just the nearest one, is processed and /// added to the passed in list of ray hits. Said list is not sorted by distance. If you /// require a sorted list you must do this yourself in the calling function. /// Returns true if at least one geom is intersected. /// </summary> /// <param name="ray">Ray</param> /// <param name="rayHits">List of Ray Hits</param> /// <param name="collisionGroup">Collision Group</param> /// <param name="tmax">Max Ray Distance</param> /// <returns>If any geoms are intersected</returns> public bool RayCastAll(gxtRay ray, out List<gxtRayHit> rayHits, gxtCollisionGroup collisionGroup = gxtCollisionGroup.ALL, float tmax = float.MaxValue) { rayHits = new List<gxtRayHit>(); if (collisionGroup == gxtCollisionGroup.NONE) return false; List<gxtGeom> geoms = broadphaseCollider.RayCastAll(ray, tmax); if (geoms.Count == 0) return false; // variables in ray hit float t; Vector2 pt, normal; for (int i = 0; i < geoms.Count; i++) { if (CanCollide(collisionGroup, geoms[i])) { if (gxtGJKCollider.RayCast(ray, geoms[i].Polygon, tmax, out t, out pt, out normal)) { gxtRayHit rayHit = new gxtRayHit(); rayHit.Intersection = true; rayHit.Distance = t; rayHit.Normal = normal; rayHit.Point = pt; rayHit.Geom = geoms[i]; rayHits.Add(rayHit); } } } return rayHits.Count > 0; }
/// <summary> /// /// </summary> /// <param name="ray">Ray</param> /// <param name="rayHits">List of Ray Hits</param> /// <param name="collisionGroupName">Collision Group</param> /// <param name="tmax">Max Ray Distance</param> /// <returns></returns> public bool RayCastAll(gxtRay ray, out List<gxtRayHit> rayHits, string collisionGroupName, float tmax = float.MaxValue) { gxtDebug.Assert(collisionGroupsMap.ContainsKey(collisionGroupName), "Named Collision Group: {0} Does Not Exist In Physics World"); gxtCollisionGroup cgroup = collisionGroupsMap[collisionGroupName]; return RayCastAll(ray, out rayHits, cgroup, tmax); }
/// <summary> /// Casts a ray into the world with the given max distance and collision filter /// Packages the nearest hit object and information on the ray cast into the rayHit structure /// Returns true if an object is intersected by the ray, false otherwise /// </summary> /// <param name="ray">Ray</param> /// <param name="rayHit">Ray Hit</param> /// <param name="tmax">Max Ray Distance</param> /// <param name="collisionGroup">Collision Groups</param> /// <returns>If an object was hit</returns> public bool RayCast(gxtRay ray, out gxtRayHit rayHit, gxtCollisionGroup collisionGroup = gxtCollisionGroup.ALL, float tmax = float.MaxValue) { rayHit = new gxtRayHit(); if (collisionGroup == gxtCollisionGroup.NONE) return false; List<gxtGeom> geoms = broadphaseCollider.RayCastAll(ray, tmax, true); if (geoms.Count == 0) return false; float t; float minDist = tmax; Vector2 pt, normal; for (int i = 0; i < geoms.Count; i++) { if (CanCollide(collisionGroup, geoms[i])) { if (gxtGJKCollider.RayCast(ray, geoms[i].Polygon, tmax, out t, out pt, out normal)) { if (t <= minDist) { rayHit.Intersection = true; rayHit.Distance = t; rayHit.Normal = normal; rayHit.Point = pt; rayHit.Geom = geoms[i]; minDist = t; } } } } return rayHit.Intersection; }