diff --git a/src/collision/shapes/BoxShape.cpp b/src/collision/shapes/BoxShape.cpp index d99e3835..38a69d15 100644 --- a/src/collision/shapes/BoxShape.cpp +++ b/src/collision/shapes/BoxShape.cpp @@ -90,8 +90,10 @@ bool BoxShape::raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* pro // Compute the intersection of the ray with the near and far plane of the slab decimal oneOverD = decimal(1.0) / rayDirection[i]; decimal t1 = (-mExtent[i] - point1[i]) * oneOverD; - decimal t2 = (mExtent[i] - point1 [i]) * oneOverD; - currentNormal = -mExtent; + decimal t2 = (mExtent[i] - point1[i]) * oneOverD; + currentNormal[0] = (i == 0) ? -mExtent[i] : decimal(0.0); + currentNormal[1] = (i == 1) ? -mExtent[i] : decimal(0.0); + currentNormal[2] = (i == 2) ? -mExtent[i] : decimal(0.0); // Swap t1 and t2 if need so that t1 is intersection with near plane and // t2 with far plane @@ -125,6 +127,7 @@ bool BoxShape::raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* pro raycastInfo.proxyShape = proxyShape; raycastInfo.hitFraction = tMin; raycastInfo.worldPoint = localToWorldTransform * localHitPoint; + normalDirection.normalize(); raycastInfo.worldNormal = localToWorldTransform.getOrientation() * normalDirection; return true; diff --git a/src/collision/shapes/ConeShape.cpp b/src/collision/shapes/ConeShape.cpp index 89d67471..83ff73c4 100644 --- a/src/collision/shapes/ConeShape.cpp +++ b/src/collision/shapes/ConeShape.cpp @@ -215,14 +215,17 @@ bool ConeShape::raycast(const Ray& ray, RaycastInfo& raycastInfo, ProxyShape* pr // Compute the normal direction for hit against side of the cone if (hitIndex != 2) { - decimal m = std::sqrt(localHitPoint[hitIndex].x * localHitPoint[hitIndex].x + - localHitPoint[hitIndex].z * localHitPoint[hitIndex].z); decimal h = decimal(2.0) * mHalfHeight; - decimal hOverR = h / mRadius; - decimal hOverROverM = hOverR / m; - localNormal[hitIndex].x = localHitPoint[hitIndex].x * hOverROverM; - localNormal[hitIndex].y = mRadius / h; - localNormal[hitIndex].z = localHitPoint[hitIndex].z * hOverROverM; + decimal value1 = (localHitPoint[hitIndex].x * localHitPoint[hitIndex].x + + localHitPoint[hitIndex].z * localHitPoint[hitIndex].z); + decimal rOverH = mRadius / h; + decimal value2 = decimal(1.0) + rOverH * rOverH; + decimal factor = decimal(1.0) / std::sqrt(value1 * value2); + decimal x = localHitPoint[hitIndex].x * factor; + decimal z = localHitPoint[hitIndex].z * factor; + localNormal[hitIndex].x = x; + localNormal[hitIndex].y = std::sqrt(x * x + z * z) * rOverH; + localNormal[hitIndex].z = z; } raycastInfo.body = proxyShape->getBody();