/// <summary> /// Perform a raycast test and return colliding results. /// </summary> /// <param name="start">Start ray vector.</param> /// <param name="end">End ray vector.</param> /// <param name="resultsCallback">BulletSharp results callback.</param> internal RaycastResults Raycast(Vector3 start, Vector3 end, BulletSharp.RayResultCallback resultsCallback) { // convert start and end vectors to bullet vectors BulletSharp.Math.Vector3 bStart = ToBullet.Vector(start); BulletSharp.Math.Vector3 bEnd = ToBullet.Vector(end); // perform the ray test return(Raycast(bStart, bEnd, resultsCallback)); }
/// <summary> /// Perform a raycast test and return colliding results. /// </summary> /// <param name="start">Start ray vector.</param> /// <param name="end">End ray vector.</param> /// <param name="returnNearest">If true, will only return the nearest object collided.</param> public RaycastResults Raycast(Vector3 start, Vector3 end, bool returnNearest = true) { // convert start and end vectors to bullet vectors BulletSharp.Math.Vector3 bStart = ToBullet.Vector(start); BulletSharp.Math.Vector3 bEnd = ToBullet.Vector(end); // create class to hold results BulletSharp.RayResultCallback resultsCallback = returnNearest ? new BulletSharp.ClosestRayResultCallback(ref bStart, ref bEnd) as BulletSharp.RayResultCallback : new BulletSharp.AllHitsRayResultCallback(bStart, bEnd); // perform ray cast return(Raycast(bStart, bEnd, resultsCallback)); }
/// <summary> /// Perform a raycast test and return colliding results, using native bullet objects. /// </summary> /// <param name="bStart">Start ray vector (bullet vector).</param> /// <param name="bEnd">End ray vector (bullet vector).</param> /// <param name="resultsCallback">BulletSharp results callback.</param> internal RaycastResults Raycast(BulletSharp.Math.Vector3 bStart, BulletSharp.Math.Vector3 bEnd, BulletSharp.RayResultCallback resultsCallback) { // perform the ray test _world.RayTestRef(ref bStart, ref bEnd, resultsCallback); // create results object to return RaycastResults results = new RaycastResults(); // parse data based on type // closest result / closest but not me types: if (resultsCallback is BulletSharp.ClosestRayResultCallback) { // convert to closest results type BulletSharp.ClosestRayResultCallback closestReults = resultsCallback as BulletSharp.ClosestRayResultCallback; // set results data results.HasHit = closestReults.HasHit; if (results.HasHit) { results.Collisions = new RaycastResults.SingleResult[1]; results.Collisions[0].HitFraction = closestReults.ClosestHitFraction; results.Collisions[0].CollisionNormal = ToMonoGame.Vector(closestReults.HitNormalWorld); results.Collisions[0].CollisionPoint = ToMonoGame.Vector(closestReults.HitPointWorld); results.Collisions[0].CollisionBody = (closestReults.CollisionObject.UserObject as BasicPhysicalBody).EcsComponent; } } // all results type else if (resultsCallback is BulletSharp.AllHitsRayResultCallback) { // convert to all results type BulletSharp.AllHitsRayResultCallback allResults = resultsCallback as BulletSharp.AllHitsRayResultCallback; // set results data results.HasHit = allResults.HasHit; if (results.HasHit) { results.Collisions = new RaycastResults.SingleResult[allResults.CollisionObjects.Count]; for (int i = 0; i < allResults.CollisionObjects.Count; ++i) { results.Collisions[i].HitFraction = allResults.HitFractions[i]; results.Collisions[i].CollisionNormal = ToMonoGame.Vector(allResults.HitNormalWorld[i]); results.Collisions[i].CollisionPoint = ToMonoGame.Vector(allResults.HitPointWorld[i]); results.Collisions[i].CollisionBody = (allResults.CollisionObjects[i].UserObject as BasicPhysicalBody).EcsComponent; } } } // finally, return parsed results return(results); }