summaryrefslogtreecommitdiffhomepage
path: root/src/models.c
diff options
context:
space:
mode:
authorProfJski <[email protected]>2019-05-08 13:54:12 -0400
committerGitHub <[email protected]>2019-05-08 13:54:12 -0400
commitd3dae384497f61ad7eb1d9578db8860e6e6336aa (patch)
tree664b740a204eaef448a1f3ebd72c297c1818030c /src/models.c
parent97c8a28aaac8d2ac8689d18a619d32107d6b96f7 (diff)
downloadraylib-d3dae384497f61ad7eb1d9578db8860e6e6336aa.tar.gz
raylib-d3dae384497f61ad7eb1d9578db8860e6e6336aa.zip
Update CheckCollisionSpheres() to avoid sqrt
Square root calls are computationally expensive. In this case, they can be avoided. Instead of checking distance<RadA+RadB, check distance squared against (RadA+RadB) squared. The dot product of Vector3Subtract(B,A) with itself gives distance squared, so I used this code instead of an element-by-element computation of distance squared. The only downside is that your geometric code is very readable, whereas this is less so.
Diffstat (limited to 'src/models.c')
-rw-r--r--src/models.c12
1 files changed, 1 insertions, 11 deletions
diff --git a/src/models.c b/src/models.c
index c81dfe19..9e534a7c 100644
--- a/src/models.c
+++ b/src/models.c
@@ -2472,17 +2472,7 @@ void DrawBoundingBox(BoundingBox box, Color color)
// Detect collision between two spheres
bool CheckCollisionSpheres(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB)
{
- bool collision = false;
-
- float dx = centerA.x - centerB.x; // X distance between centers
- float dy = centerA.y - centerB.y; // Y distance between centers
- float dz = centerA.z - centerB.z; // Y distance between centers
-
- float distance = sqrtf(dx*dx + dy*dy + dz*dz); // Distance between centers
-
- if (distance <= (radiusA + radiusB)) collision = true;
-
- return collision;
+ return Vector3DotProduct(Vector3Subtract(B,A),Vector3Subtract(B,A))<=(RadA+RadB)*(RadA+RadB);
}
// Detect collision between two boxes