Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Plane Colitions //
  3. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. bool PlatformMath::Intersect(omPlane& _plane, omPlane& _plane2, omVector3& P)
  5. {
  6. //<Producto cruz entre las normales de los planos>//
  7. omVector3 vecTemp = omVector3::CrossProduct(_plane.m_vecNormal, _plane2.m_vecNormal);
  8.  
  9. //<Cuadrado del resultado para sacar el determinante>//
  10. float det = Math::Square(omVector3::Vector3Length(vecTemp));
  11.  
  12. //<Si los planos no son paralelos habra colision>//
  13. if (omVector3::DotProduct(vecTemp, vecTemp) != 0)
  14. {
  15. float d11 = omVector3::DotProduct(_plane.m_vecNormal, _plane.m_vecNormal);
  16. float d12 = omVector3::DotProduct(_plane.m_vecNormal, _plane2.m_vecNormal);
  17. float d22 = omVector3::DotProduct(_plane2.m_vecNormal, _plane2.m_vecNormal);
  18.  
  19. float denom = d11 * d22 - d12 * d12;
  20.  
  21. float k1 = (_plane.m_W * d22 - _plane2.m_W * d12) / denom;
  22. float k2 = (_plane2.m_W * d11 - _plane.m_W * d12) / denom;
  23.  
  24. P = (_plane.m_vecNormal * k1) + (_plane2.m_vecNormal * k2);
  25. return true;
  26. }
  27.  
  28. return false;
  29. }
  30.  
  31. bool Intersect(omPlane& _plane, omSphere& _sphere)
  32. {
  33. //Calculate a vector from the point on the plane to the center of the sphere
  34. omVector3 vecTemp = omVector3::Normalize(_plane.m_vecNormal);//un punto en el plano?
  35.  
  36. //Calculate the distance: dot product of the new vector with the plane's normal
  37. float fDist = omVector3::DotProduct(vecTemp, _plane.m_vecNormal);
  38.  
  39. if (fDist > _sphere.m_fRadius)
  40. {
  41. //The sphere is not touching the plane
  42. return false;
  43. }
  44.  
  45. //Else, the sphere is colliding with the plane
  46. return true;
  47. }
  48.  
  49. bool Intersect(omPlane& _plane, omRay& _ray)
  50. {
  51. float denom = omVector3::DotProduct(_plane.m_vecNormal, _ray.m_vecDirection);
  52. if (abs(denom) > PlatformMath::EPSILON) // your favorite epsilon
  53. {
  54. float t = (center - omVector3::DotProduct(_ray.m_vecOrigin, _plane.m_vecNormal)) / denom;//centro del plano?
  55. if (t >= PlatformMath::EPSILON) return true; // you might want to allow an epsilon here too
  56. }
  57. return false;
  58. }
  59.  
  60. bool Intersect(omPlane& _plane, omAABB& _aabb)
  61. {
  62.  
  63. }
  64.  
  65. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. // Sphere Colitions //
  67. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. bool Intersect(omSphere& _sphere, omPlane& _plane)
  69. {
  70. //Calculate a vector from the point on the plane to the center of the sphere
  71. omVector3 vecTemp(_sphere.m_vecCenter - vecPoint);//un punto en el plano?
  72.  
  73. //Calculate the distance: dot product of the new vector with the plane's normal
  74. float fDist = omVector3::DotProduct(vecTemp, _plane.m_vecNormal);
  75.  
  76. if (fDist > _sphere.m_fRadius)
  77. {
  78. //The sphere is not touching the plane
  79. return false;
  80. }
  81.  
  82. //Else, the sphere is colliding with the plane
  83. return true;
  84. }
  85.  
  86. bool Intersect(omSphere& _sphere, omSphere& _sphere2)
  87. {
  88. //<Calculamos distancia entre los centros de ambas esferas>//
  89. omVector3 vecDiff = _sphere.m_vecCenter - _sphere2.m_vecCenter;
  90. float distance = omVector3::Vector3Length(vecDiff);
  91.  
  92. //<Suma de radios de las esferas>//
  93. float sumRadius = _sphere.m_fRadius + _sphere2.m_fRadius;
  94.  
  95. //<si la distancia entre las esferas es menor a la suma de los radios entonces habra colision>//
  96. if (distance < sumRadius)
  97. return true;
  98.  
  99. return false;
  100. }
  101.  
  102. bool Intersect(omSphere& _sphere, omRay& _ray)
  103. {
  104. omVector3 vecTemp = _ray.m_vecOrigin - _sphere.m_vecCenter;
  105. //Squared distance between ray origin and sphere center
  106. float squaredDist = omVector3::DotProduct(vecTemp, vecTemp);
  107. float squaredRadius = PlatformMath::Square(_sphere.m_fRadius);
  108.  
  109. //If the distance is less than the squared radius of the sphere...
  110. if (squaredDist <= squaredRadius)
  111. {
  112. //Point is in sphere, consider as no intersection existing
  113. return false;
  114. }
  115.  
  116. //Will hold solution to quadratic equation
  117. float t0, t1;
  118.  
  119. //Calculating the coefficients of the quadratic equation
  120. float a = omVector3::DotProduct(_ray.m_vecDirection, _ray.m_vecDirection); // a = d*d
  121. float b = 2.0f*(omVector3::DotProduct(_ray.m_vecDirection, vecTemp)); // b = 2d(o-C)
  122. float c = squaredDist - squaredRadius; // c = (o-C)^2-R^2
  123.  
  124. //Calculate discriminant
  125. float disc = (b*b) - (4.0f*a*c);
  126.  
  127. if (disc < 0) //If discriminant is negative no intersection happens
  128. {
  129. //std::cout << "No intersection with sphere..." << std::endl;
  130. return false;
  131. }
  132. else //If discriminant is positive one or two intersections (two solutions) exists
  133. {
  134. float sqrt_disc = PlatformMath::Sqrt(disc);
  135. t0 = (-b - sqrt_disc) / (2.0f * a);
  136. t1 = (-b + sqrt_disc) / (2.0f * a);
  137. }
  138.  
  139. //If the second intersection has a negative value then the intersections
  140. //happen behind the ray origin which is not considered. Otherwise t0 is
  141. //the intersection to be considered
  142. if (t1<0)
  143. {
  144. //std::cout << "No intersection with sphere..." << std::endl;
  145. return false;
  146. }
  147. else
  148. {
  149. //std::cout << "Intersection with sphere..." << std::endl;
  150. return true;
  151. }
  152. }
  153.  
  154. bool Intersect(omSphere& _sphere, omAABB& _aabb)
  155. {
  156.  
  157. }
  158.  
  159. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  160. // Ray Colitions //
  161. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162. bool Intersect(omRay& _ray, omPlane& _plane)
  163. {
  164. float denom = omVector3::DotProduct(_plane.m_vecNormal, _ray.m_vecDirection);
  165. if (abs(denom) > PlatformMath::EPSILON) // your favorite epsilon
  166. {
  167. float t = (center -omVector3::DotProduct(_ray.m_vecOrigin, _plane.m_vecNormal)) / denom;//centro del plano?
  168. if (t >= PlatformMath::EPSILON) return true; // you might want to allow an epsilon here too
  169. }
  170. return false;
  171. }
  172.  
  173. bool Intersect(omRay& _ray, omSphere& _sphere)
  174. {
  175. omVector3 vecTemp = _ray.m_vecOrigin - _sphere.m_vecCenter;
  176. //Squared distance between ray origin and sphere center
  177. float squaredDist = omVector3::DotProduct(vecTemp, vecTemp);
  178. float squaredRadius = PlatformMath::Square(_sphere.m_fRadius);
  179.  
  180. //If the distance is less than the squared radius of the sphere...
  181. if (squaredDist <= squaredRadius)
  182. {
  183. //Point is in sphere, consider as no intersection existing
  184. return false;
  185. }
  186.  
  187. //Will hold solution to quadratic equation
  188. float t0, t1;
  189.  
  190. //Calculating the coefficients of the quadratic equation
  191. float a = omVector3::DotProduct(_ray.m_vecDirection, _ray.m_vecDirection); // a = d*d
  192. float b = 2.0f*(omVector3::DotProduct(_ray.m_vecDirection, vecTemp)); // b = 2d(o-C)
  193. float c = squaredDist - squaredRadius; // c = (o-C)^2-R^2
  194.  
  195. //Calculate discriminant
  196. float disc = (b*b) - (4.0f*a*c);
  197.  
  198. if (disc < 0) //If discriminant is negative no intersection happens
  199. {
  200. //std::cout << "No intersection with sphere..." << std::endl;
  201. return false;
  202. }
  203. else //If discriminant is positive one or two intersections (two solutions) exists
  204. {
  205. float sqrt_disc = PlatformMath::Sqrt(disc);
  206. t0 = (-b - sqrt_disc) / (2.0f * a);
  207. t1 = (-b + sqrt_disc) / (2.0f * a);
  208. }
  209.  
  210. //If the second intersection has a negative value then the intersections
  211. //happen behind the ray origin which is not considered. Otherwise t0 is
  212. //the intersection to be considered
  213. if (t1<0)
  214. {
  215. //std::cout << "No intersection with sphere..." << std::endl;
  216. return false;
  217. }
  218. else
  219. {
  220. //std::cout << "Intersection with sphere..." << std::endl;
  221. return true;
  222. }
  223. }
  224. bool Intersect(omRay& _ray, omRay& _ray2)
  225. {
  226. omVector3 Lt = _ray2.m_vecOrigin;
  227. Lt = Lt + _ray2.m_vecDirection;
  228.  
  229. omVector3 Ls = _ray.m_vecOrigin + _ray.m_vecDirection;
  230.  
  231. if (Lt == Ls)
  232. return true;
  233.  
  234. return false;
  235. }
  236. bool Intersect(omRay& _ray, omAABB& _aabb)
  237. {
  238.  
  239. }
  240.  
  241. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  242. // Ray Colitions //
  243. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  244. bool Intersect(omAABB& _aabb, omPlane& _plane)
  245. {
  246.  
  247. }
  248.  
  249. bool Intersect(omAABB& _aabb, omSphere& _sphere)
  250. {
  251.  
  252. }
  253.  
  254. bool Intersect(omAABB& _aabb, omRay& _ray)
  255. {
  256.  
  257. }
  258.  
  259. bool Intersect(omAABB& _aabb, omAABB& _aabb2)
  260. {
  261. if (abs(_aabb.m_vecCenter[0] - _aabb2.m_vecCenter[0]) > (_aabb.m_vecDimentions[0] + _aabb2.m_vecDimentions[0])) return false;
  262. if (abs(_aabb.m_vecCenter[1] - _aabb2.m_vecCenter[1]) > (_aabb.m_vecDimentions[1] + _aabb2.m_vecDimentions[1])) return false;
  263. if (abs(_aabb.m_vecCenter[2] - _aabb2.m_vecCenter[2]) > (_aabb.m_vecDimentions[2] + _aabb2.m_vecDimentions[2])) return false;
  264.  
  265. //<Las cajas se Sobreponen>//
  266. return true;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement