2 Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.
\r
5 Physics Effects is open software; you can redistribute it and/or
\r
6 modify it under the terms of the BSD License.
\r
8 Physics Effects is distributed in the hope that it will be useful,
\r
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
\r
11 See the BSD License for more details.
\r
13 A copy of the BSD License is distributed with
\r
14 Physics Effects under the filename: physics_effects_license.txt
\r
17 #include "../../../include/physics_effects/base_level/collision/pfx_sphere.h"
\r
18 #include "pfx_intersect_common.h"
\r
19 #include "pfx_intersect_ray_sphere.h"
\r
22 namespace PhysicsEffects {
\r
24 PfxBool pfxIntersectRaySphere(const PfxRayInput &ray,PfxRayOutput &out,const PfxSphere &sphere,const PfxTransform3 &transform)
\r
26 PfxVector3 v = ray.m_startPosition - transform.getTranslation();
\r
28 PfxFloat a = dot(ray.m_direction,ray.m_direction);
\r
29 PfxFloat b = dot(v,ray.m_direction);
\r
30 PfxFloat c = dot(v,v) - sphere.m_radius * sphere.m_radius;
\r
32 if(c < 0.0f) return false;
\r
34 PfxFloat d = b * b - a * c;
\r
36 if(d < 0.0f || fabsf(a) < 0.00001f) return false;
\r
38 PfxFloat tt = ( -b - sqrtf(d) ) / a;
\r
40 if(tt < 0.0f || tt > 1.0f) return false;
\r
42 if(tt < out.m_variable) {
\r
43 out.m_contactFlag = true;
\r
44 out.m_variable = tt;
\r
45 out.m_contactPoint = ray.m_startPosition + tt * ray.m_direction;
\r
46 out.m_contactNormal = normalize(out.m_contactPoint - transform.getTranslation());
\r
47 out.m_subData.m_type = PfxSubData::NONE;
\r
54 } //namespace PhysicsEffects
\r