mSphere.cpp
Engine/source/math/mSphere.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "math/mSphere.h" 26 27#include "math/mMatrix.h" 28 29 30bool SphereF::intersectsRay( const Point3F &start, const Point3F &end ) const 31{ 32 MatrixF worldToObj( true ); 33 worldToObj.setPosition( center ); 34 worldToObj.inverse(); 35 36 VectorF dir = end - start; 37 dir.normalize(); 38 39 Point3F tmpStart = start; 40 worldToObj.mulP( tmpStart ); 41 42 //Compute A, B and C coefficients 43 F32 a = mDot(dir, dir); 44 F32 b = 2 * mDot(dir, tmpStart); 45 F32 c = mDot(tmpStart, tmpStart) - (radius * radius); 46 47 //Find discriminant 48 F32 disc = b * b - 4 * a * c; 49 50 // if discriminant is negative there are no real roots, so return 51 // false as ray misses sphere 52 if ( disc < 0 ) 53 return false; 54 55 // compute q as described above 56 F32 distSqrt = mSqrt( disc ); 57 F32 q; 58 if ( b < 0 ) 59 q = (-b - distSqrt)/2.0; 60 else 61 q = (-b + distSqrt)/2.0; 62 63 // compute t0 and t1 64 F32 t0 = q / a; 65 F32 t1 = c / q; 66 67 // make sure t0 is smaller than t1 68 if ( t0 > t1 ) 69 { 70 // if t0 is bigger than t1 swap them around 71 F32 temp = t0; 72 t0 = t1; 73 t1 = temp; 74 } 75 76 // This function doesn't use it 77 // but t would be the interpolant 78 // value for getting the exact 79 // intersection point, by interpolating 80 // start to end by t. 81 82 /* 83 F32 t = 0; 84 TORQUE_UNUSED(t); 85 */ 86 87 // if t1 is less than zero, the object is in the ray's negative direction 88 // and consequently the ray misses the sphere 89 if ( t1 < 0 ) 90 return false; 91 92 // if t0 is less than zero, the intersection point is at t1 93 if ( t0 < 0 ) // t = t1; 94 return true; 95 else // else the intersection point is at t0 96 return true; // t = t0; 97} 98