Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / src / base_level / collision / pfx_intersect_ray_sphere.cpp
1 /*\r
2 Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.\r
3 All rights reserved.\r
4 \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
7 \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
12 \r
13 A copy of the BSD License is distributed with\r
14 Physics Effects under the filename: physics_effects_license.txt\r
15 */\r
16 \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
20 \r
21 namespace sce {\r
22 namespace PhysicsEffects {\r
23 \r
24 PfxBool pfxIntersectRaySphere(const PfxRayInput &ray,PfxRayOutput &out,const PfxSphere &sphere,const PfxTransform3 &transform)\r
25 {\r
26         PfxVector3 v = ray.m_startPosition - transform.getTranslation();\r
27 \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
31 \r
32         if(c < 0.0f) return false;\r
33 \r
34         PfxFloat d = b * b - a * c;\r
35         \r
36         if(d < 0.0f || fabsf(a) < 0.00001f) return false;\r
37         \r
38         PfxFloat tt = ( -b - sqrtf(d) ) / a;\r
39         \r
40         if(tt < 0.0f || tt > 1.0f) return false;\r
41         \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
48                 return true;\r
49         }\r
50         \r
51         return false;\r
52 }\r
53 \r
54 } //namespace PhysicsEffects\r
55 } //namespace sce\r