void OnDrawGizmos() { VolatileWorld world = VolatileWorld.Instance; if ((world != null) && (Application.isPlaying == true)) { VoltRayResult result = new VoltRayResult(); VoltRayCast cast = new VoltRayCast( transform.position, transform.position + (transform.up * 100.0f)); if (this.radius > 0.0f) world.World.CircleCast(ref cast, this.radius, ref result, this.Filter, -this.frameOffset); else world.World.RayCast(ref cast, ref result, this.Filter, -this.frameOffset); float drawRadius = (this.radius == 0.0f) ? 0.2f : this.radius; Gizmos.color = this.color; if (result.IsValid == true) { Vector2 point = transform.position + (transform.up * result.Distance); Gizmos.DrawLine(transform.position, point); Gizmos.DrawWireSphere(point,drawRadius); } else { Gizmos.DrawLine(transform.position, transform.position + (transform.up * 100.0f)); } } }
private bool CircleCastVertices( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result) { float sqrRadius = radius * radius; bool castHit = false; for (int i = 0; i < this.countBody; i++) { castHit |= Collision.CircleRayCast( this, this.bodyVertices[i], sqrRadius, ref bodySpaceRay, ref result); if (result.IsContained == true) { return(true); } } return(castHit); }
/// <summary> /// Performs a circle cast on all world bodies. /// </summary> public bool CircleCast( ref VoltRayCast ray, Fix64 radius, ref VoltRayResult result, VoltBodyFilter filter = null, int ticksBehind = 0) { if (ticksBehind < 0) { throw new ArgumentOutOfRangeException("ticksBehind"); } this.reusableBuffer.Clear(); this.staticBroadphase.CircleCast(ref ray, radius, this.reusableBuffer); this.dynamicBroadphase.CircleCast(ref ray, radius, this.reusableBuffer); for (int i = 0; i < this.reusableBuffer.Count; i++) { VoltBody body = this.reusableBuffer[i]; if (VoltBody.Filter(body, filter)) { body.CircleCast(ref ray, radius, ref result, ticksBehind); if (result.IsContained) { return(true); } } } return(result.IsValid); }
/// <summary> /// Checks a ray against a circle with a given origin and square radius. /// </summary> internal static bool CircleRayCast( VoltShape shape, Vector2 shapeOrigin, float sqrRadius, ref VoltRayCast ray, ref VoltRayResult result) { Vector2 toOrigin = shapeOrigin - ray.origin; if (toOrigin.sqrMagnitude < sqrRadius) { result.SetContained(shape); return true; } float slope = Vector2.Dot(toOrigin, ray.direction); if (slope < 0) return false; float sqrSlope = slope * slope; float d = sqrRadius + sqrSlope - Vector2.Dot(toOrigin, toOrigin); if (d < 0) return false; float dist = slope - Mathf.Sqrt(d); if (dist < 0 || dist > ray.distance) return false; // N.B.: For historical raycasts this normal will be wrong! // Must be either transformed back to world or invalidated later. Vector2 normal = (dist * ray.direction - toOrigin).normalized; result.Set(shape, dist, normal); return true; }
private void ExplosionCallback( VoltRayCast rayCast, VoltRayResult rayResult, float rayWeight) { Vector2 point = rayResult.ComputePoint(ref rayCast); this.hits.Add(point); }
/// <summary> /// Performs a raycast check on this shape. /// Begins with an AABB check. /// </summary> internal bool RayCast( ref VoltRayCast bodySpaceRay, ref VoltRayResult result) { // Queries and casts on shapes are always done in body space if (this.bodySpaceAABB.RayCast(ref bodySpaceRay)) { return(this.ShapeRayCast(ref bodySpaceRay, ref result)); } return(false); }
protected override bool ShapeRayCast( ref VoltRayCast bodySpaceRay, ref VoltRayResult result) { return(Collision.CircleRayCast( this, this.bodySpaceOrigin, this.sqrRadius, ref bodySpaceRay, ref result)); }
/// <summary> /// Performs a circlecast check on this shape. /// Begins with an AABB check. /// </summary> internal bool CircleCast( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result) { // Queries and casts on shapes are always done in body space if (this.bodySpaceAABB.CircleCastApprox(ref bodySpaceRay, radius)) { return(this.ShapeCircleCast(ref bodySpaceRay, radius, ref result)); } return(false); }
protected override bool ShapeCircleCast( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result) { float totalRadius = this.radius + radius; return(Collision.CircleRayCast( this, this.bodySpaceOrigin, totalRadius * totalRadius, ref bodySpaceRay, ref result)); }
/// <summary> /// Checks a ray against a circle with a given origin and square radius. /// </summary> internal static bool CircleRayCast( VoltShape shape, Vector2 shapeOrigin, float sqrRadius, ref VoltRayCast ray, ref VoltRayResult result) { Vector2 toOrigin = shapeOrigin - ray.origin; if (toOrigin.sqrMagnitude < sqrRadius) { result.SetContained(shape); return(true); } float slope = Vector2.Dot(toOrigin, ray.direction); if (slope < 0) { return(false); } float sqrSlope = slope * slope; float d = sqrRadius + sqrSlope - Vector2.Dot(toOrigin, toOrigin); if (d < 0) { return(false); } float dist = slope - Mathf.Sqrt(d); if (dist < 0 || dist > ray.distance) { return(false); } // N.B.: For historical raycasts this normal will be wrong! // Must be either transformed back to world or invalidated later. Vector2 normal = (dist * ray.direction - toOrigin).normalized; result.Set(shape, dist, normal); return(true); }
protected override bool ShapeCircleCast( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result) { bool checkVertices = this.CircleCastVertices( ref bodySpaceRay, radius, ref result); bool checkEdges = this.CircleCastEdges( ref bodySpaceRay, radius, ref result); // We need to check both to get the closest hit distance return(checkVertices || checkEdges); }
/// <summary> /// Tests all valid explosion targets for a given ray. /// </summary> private void TestTargets( VoltRayCast ray, VoltExplosionCallback callback, int ticksBehind, FP minOccluderDistance, FP rayWeight) { for (int i = 0; i < this.targetBodies.Count; i++) { VoltBody targetBody = this.targetBodies[i]; VoltRayResult result = default(VoltRayResult); if (targetBody.RayCast(ref ray, ref result, ticksBehind)) { if (result.Distance < minOccluderDistance) { callback.Invoke(ray, result, rayWeight); } } } }
/// <summary> /// Gets the distance to the closest occluder for the given ray. /// </summary> private FP GetOccludingDistance( VoltRayCast ray, int ticksBehind) { FP distance = FP.MaxValue; VoltRayResult result = default(VoltRayResult); for (int i = 0; i < this.occludingBodies.Count; i++) { if (this.occludingBodies[i].RayCast(ref ray, ref result, ticksBehind)) { distance = result.Distance; } if (result.IsContained) { break; } } return(distance); }
/// <summary> /// Performs a circle cast check on this body. /// Begins with AABB checks. /// </summary> internal bool CircleCast( ref VoltRayCast ray, FP radius, ref VoltRayResult result, int ticksBehind, bool bypassAABB = false) { HistoryRecord record = this.GetState(ticksBehind); // AABB check done in world space (because it keeps changing) if (bypassAABB == false) { if (record.aabb.CircleCastApprox(ref ray, radius) == false) { return(false); } } // Actual tests on shapes done in body space VoltRayCast bodySpaceRay = record.WorldToBodyRay(ref ray); for (int i = 0; i < this.shapeCount; i++) { if (this.shapes[i].CircleCast(ref bodySpaceRay, radius, ref result)) { if (result.IsContained) { return(true); } } } // We need to convert the results back to world space to be any use // (Doesn't matter if we were contained since there will be no normal) if (result.Body == this) { result.normal = record.BodyToWorldDirection(result.normal); } return(result.IsValid); }
protected override bool ShapeRayCast( ref VoltRayCast bodySpaceRay, ref VoltRayResult result) { int foundIndex = -1; float inner = float.MaxValue; float outer = 0; bool couldBeContained = true; for (int i = 0; i < this.countBody; i++) { Axis curAxis = this.bodyAxes[i]; // Distance between the ray origin and the axis/edge along the // normal (i.e., shortest distance between ray origin and the edge) float proj = Vector2.Dot(curAxis.Normal, bodySpaceRay.origin) - curAxis.Width; // See if the point is outside of any of the axes if (proj > 0.0f) couldBeContained = false; // Projection of the ray direction onto the axis normal (use // negative normal because we want to get the penetration length) float slope = Vector2.Dot(-curAxis.Normal, bodySpaceRay.direction); if (slope == 0.0f) continue; float dist = proj / slope; // The ray is pointing opposite the edge normal (towards the edge) if (slope > 0.0f) { if (dist > inner) { return false; } if (dist > outer) { outer = dist; foundIndex = i; } } // The ray is pointing along the edge normal (away from the edge) else { if (dist < outer) { return false; } if (dist < inner) { inner = dist; } } } if (couldBeContained == true) { result.SetContained(this); return true; } else if (foundIndex >= 0 && outer <= bodySpaceRay.distance) { result.Set( this, outer, this.bodyAxes[foundIndex].Normal); return true; } return false; }
protected override bool ShapeRayCast( ref VoltRayCast bodySpaceRay, ref VoltRayResult result) { int foundIndex = -1; float inner = float.MaxValue; float outer = 0; bool couldBeContained = true; for (int i = 0; i < this.countBody; i++) { Axis curAxis = this.bodyAxes[i]; // Distance between the ray origin and the axis/edge along the // normal (i.e., shortest distance between ray origin and the edge) float proj = Vector2.Dot(curAxis.Normal, bodySpaceRay.origin) - curAxis.Width; // See if the point is outside of any of the axes if (proj > 0.0f) { couldBeContained = false; } // Projection of the ray direction onto the axis normal (use // negative normal because we want to get the penetration length) float slope = Vector2.Dot(-curAxis.Normal, bodySpaceRay.direction); if (slope == 0.0f) { continue; } float dist = proj / slope; // The ray is pointing opposite the edge normal (towards the edge) if (slope > 0.0f) { if (dist > inner) { return(false); } if (dist > outer) { outer = dist; foundIndex = i; } } // The ray is pointing along the edge normal (away from the edge) else { if (dist < outer) { return(false); } if (dist < inner) { inner = dist; } } } if (couldBeContained == true) { result.SetContained(this); return(true); } else if (foundIndex >= 0 && outer <= bodySpaceRay.distance) { result.Set( this, outer, this.bodyAxes[foundIndex].Normal); return(true); } return(false); }
/// <summary> /// Performs a raycast on all world bodies. /// </summary> public bool RayCast( ref VoltRayCast ray, ref VoltRayResult result, VoltBodyFilter filter = null, int ticksBehind = 0) { if (ticksBehind < 0) throw new ArgumentOutOfRangeException("ticksBehind"); this.reusableBuffer.Clear(); this.staticBroadphase.RayCast(ref ray, this.reusableBuffer); this.dynamicBroadphase.RayCast(ref ray, this.reusableBuffer); for (int i = 0; i < this.reusableBuffer.Count; i++) { VoltBody body = this.reusableBuffer[i]; if (VoltBody.Filter(body, filter)) { body.RayCast(ref ray, ref result, ticksBehind); if (result.IsContained) return true; } } return result.IsValid; }
protected abstract bool ShapeCircleCast( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result);
protected abstract bool ShapeRayCast( ref VoltRayCast bodySpaceRay, ref VoltRayResult result);
private bool CircleCastVertices( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result) { float sqrRadius = radius * radius; bool castHit = false; for (int i = 0; i < this.countBody; i++) { castHit |= Collision.CircleRayCast( this, this.bodyVertices[i], sqrRadius, ref bodySpaceRay, ref result); if (result.IsContained == true) return true; } return castHit; }
private bool CircleCastEdges( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result) { int foundIndex = -1; bool couldBeContained = true; // Pre-compute and initialize values float shortestDist = float.MaxValue; Vector2 v3 = bodySpaceRay.direction.Left(); // Check the edges -- this will be different from the raycast because // we care about staying within the ends of the edge line segment for (int i = 0; i < this.countBody; i++) { Axis curAxis = this.bodyAxes[i]; // Push the edges out by the radius Vector2 extension = curAxis.Normal * radius; Vector2 a = this.bodyVertices[i] + extension; Vector2 b = this.bodyVertices[(i + 1) % this.countBody] + extension; // Update the check for containment if (couldBeContained == true) { float proj = Vector2.Dot(curAxis.Normal, bodySpaceRay.origin) - curAxis.Width; // The point lies outside of the outer layer if (proj > radius) { couldBeContained = false; } // The point lies between the outer and inner layer else if (proj > 0.0f) { // See if the point is within the center Vornoi region of the edge float d = VoltMath.Cross(curAxis.Normal, bodySpaceRay.origin); if (d > VoltMath.Cross(curAxis.Normal, a)) { couldBeContained = false; } if (d < VoltMath.Cross(curAxis.Normal, b)) { couldBeContained = false; } } } // For the cast, only consider rays pointing towards the edge if (Vector2.Dot(curAxis.Normal, bodySpaceRay.direction) >= 0.0f) { continue; } // See: // https://rootllama.wordpress.com/2014/06/20/ray-line-segment-intersection-test-in-2d/ Vector2 v1 = bodySpaceRay.origin - a; Vector2 v2 = b - a; float denominator = Vector2.Dot(v2, v3); float t1 = VoltMath.Cross(v2, v1) / denominator; float t2 = Vector2.Dot(v1, v3) / denominator; if ((t2 >= 0.0f) && (t2 <= 1.0f) && (t1 > 0.0f) && (t1 < shortestDist)) { // See if the point is outside of any of the axes shortestDist = t1; foundIndex = i; } } // Report results if (couldBeContained == true) { result.SetContained(this); return(true); } else if (foundIndex >= 0 && shortestDist <= bodySpaceRay.distance) { result.Set( this, shortestDist, this.bodyAxes[foundIndex].Normal); return(true); } return(false); }
protected override bool ShapeCircleCast( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result) { bool checkVertices = this.CircleCastVertices( ref bodySpaceRay, radius, ref result); bool checkEdges = this.CircleCastEdges( ref bodySpaceRay, radius, ref result); // We need to check both to get the closest hit distance return checkVertices || checkEdges; }
private bool CircleCastEdges( ref VoltRayCast bodySpaceRay, float radius, ref VoltRayResult result) { int foundIndex = -1; bool couldBeContained = true; // Pre-compute and initialize values float shortestDist = float.MaxValue; Vector2 v3 = bodySpaceRay.direction.Left(); // Check the edges -- this will be different from the raycast because // we care about staying within the ends of the edge line segment for (int i = 0; i < this.countBody; i++) { Axis curAxis = this.bodyAxes[i]; // Push the edges out by the radius Vector2 extension = curAxis.Normal * radius; Vector2 a = this.bodyVertices[i] + extension; Vector2 b = this.bodyVertices[(i + 1) % this.countBody] + extension; // Update the check for containment if (couldBeContained == true) { float proj = Vector2.Dot(curAxis.Normal, bodySpaceRay.origin) - curAxis.Width; // The point lies outside of the outer layer if (proj > radius) { couldBeContained = false; } // The point lies between the outer and inner layer else if (proj > 0.0f) { // See if the point is within the center Vornoi region of the edge float d = VoltMath.Cross(curAxis.Normal, bodySpaceRay.origin); if (d > VoltMath.Cross(curAxis.Normal, a)) couldBeContained = false; if (d < VoltMath.Cross(curAxis.Normal, b)) couldBeContained = false; } } // For the cast, only consider rays pointing towards the edge if (Vector2.Dot(curAxis.Normal, bodySpaceRay.direction) >= 0.0f) continue; // See: // https://rootllama.wordpress.com/2014/06/20/ray-line-segment-intersection-test-in-2d/ Vector2 v1 = bodySpaceRay.origin - a; Vector2 v2 = b - a; float denominator = Vector2.Dot(v2, v3); float t1 = VoltMath.Cross(v2, v1) / denominator; float t2 = Vector2.Dot(v1, v3) / denominator; if ((t2 >= 0.0f) && (t2 <= 1.0f) && (t1 > 0.0f) && (t1 < shortestDist)) { // See if the point is outside of any of the axes shortestDist = t1; foundIndex = i; } } // Report results if (couldBeContained == true) { result.SetContained(this); return true; } else if (foundIndex >= 0 && shortestDist <= bodySpaceRay.distance) { result.Set( this, shortestDist, this.bodyAxes[foundIndex].Normal); return true; } return false; }