Skip to content
This repository has been archived by the owner on Feb 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #33 from LoghamLogan/fix/Incorrect_pushback_mesh_c…
Browse files Browse the repository at this point in the history
…ollider_no_BSPTree

Fixed incorrect pushback position when mesh has no ClosestPointOnSurface
  • Loading branch information
IronWarrior committed Nov 29, 2016
2 parents b8c6ad2 + 76ec294 commit d59764e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
13 changes: 9 additions & 4 deletions Assets/SuperCharacterController/Core/SuperCharacterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,18 @@ void RecursivePushback(int depth, int maxDepth)
foreach (Collider col in Physics.OverlapSphere((SpherePosition(sphere)), radius, Walkable, triggerInteraction))
{
Vector3 position = SpherePosition(sphere);
Vector3 contactPoint = SuperCollider.ClosestPointOnSurface(col, position, radius);

Vector3 contactPoint;
bool contactPointSuccess = SuperCollider.ClosestPointOnSurface(col, position, radius, out contactPoint);

if (!contactPointSuccess)
{
return;
}

if (debugPushbackMesssages)
DebugDraw.DrawMarker(contactPoint, 2.0f, Color.cyan, 0.0f, false);

Vector3 v = contactPoint - position;

if (v != Vector3.zero)
{
// Cache the collider's layer so that we can cast against it
Expand Down
32 changes: 20 additions & 12 deletions Assets/SuperCharacterController/Core/SuperCollider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,60 @@

public static class SuperCollider {

public static Vector3 ClosestPointOnSurface(Collider collider, Vector3 to, float radius)
public static bool ClosestPointOnSurface(Collider collider, Vector3 to, float radius, out Vector3 closestPointOnSurface)
{
if (collider is BoxCollider)
{
return SuperCollider.ClosestPointOnSurface((BoxCollider)collider, to);
closestPointOnSurface = SuperCollider.ClosestPointOnSurface((BoxCollider)collider, to);
return true;
}
else if (collider is SphereCollider)
{
return SuperCollider.ClosestPointOnSurface((SphereCollider)collider, to);
closestPointOnSurface = SuperCollider.ClosestPointOnSurface((SphereCollider)collider, to);
return true;
}
else if (collider is CapsuleCollider)
{
return SuperCollider.ClosestPointOnSurface((CapsuleCollider)collider, to);
closestPointOnSurface = SuperCollider.ClosestPointOnSurface((CapsuleCollider)collider, to);
return true;
}
else if (collider is MeshCollider)
{
RPGMesh rpgMesh = collider.GetComponent<RPGMesh>();

if (rpgMesh != null)
{
return rpgMesh.ClosestPointOn(to, radius, false, false);
closestPointOnSurface = rpgMesh.ClosestPointOn(to, radius, false, false);
return true;
}

BSPTree bsp = collider.GetComponent<BSPTree>();

if (bsp != null)
{
return bsp.ClosestPointOn(to, radius);
closestPointOnSurface = bsp.ClosestPointOn(to, radius);
return true;
}

BruteForceMesh bfm = collider.GetComponent<BruteForceMesh>();

if (bfm != null)
{
return bfm.ClosestPointOn(to);
closestPointOnSurface = bfm.ClosestPointOn(to);
return true;
}
}
else if (collider is TerrainCollider)
{
return SuperCollider.ClosestPointOnSurface((TerrainCollider)collider, to, radius, false);
closestPointOnSurface = SuperCollider.ClosestPointOnSurface((TerrainCollider)collider, to, radius, false);
return true;
}

Debug.LogError(string.Format("{0} does not have an implementation for ClosestPointOnSurface", collider.GetType()));

return Vector3.zero;
Debug.LogError(string.Format("{0} does not have an implementation for ClosestPointOnSurface; GameObject.Name='{1}'", collider.GetType(), collider.gameObject.name));
closestPointOnSurface = Vector3.zero;
return false;
}

public static Vector3 ClosestPointOnSurface(SphereCollider collider, Vector3 to)
{
Vector3 p;
Expand Down Expand Up @@ -268,4 +275,5 @@ public static Vector3 ClosestPointOnSurface(TerrainCollider collider, Vector3 to

return collider.transform.TransformPoint(shortestPoint);
}

}

0 comments on commit d59764e

Please sign in to comment.