Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Plane Colitions //
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- bool PlatformMath::Intersect(omPlane& _plane, omPlane& _plane2, omVector3& P)
- {
- //<Producto cruz entre las normales de los planos>//
- omVector3 vecTemp = omVector3::CrossProduct(_plane.m_vecNormal, _plane2.m_vecNormal);
- //<Cuadrado del resultado para sacar el determinante>//
- float det = Math::Square(omVector3::Vector3Length(vecTemp));
- //<Si los planos no son paralelos habra colision>//
- if (omVector3::DotProduct(vecTemp, vecTemp) != 0)
- {
- float d11 = omVector3::DotProduct(_plane.m_vecNormal, _plane.m_vecNormal);
- float d12 = omVector3::DotProduct(_plane.m_vecNormal, _plane2.m_vecNormal);
- float d22 = omVector3::DotProduct(_plane2.m_vecNormal, _plane2.m_vecNormal);
- float denom = d11 * d22 - d12 * d12;
- float k1 = (_plane.m_W * d22 - _plane2.m_W * d12) / denom;
- float k2 = (_plane2.m_W * d11 - _plane.m_W * d12) / denom;
- P = (_plane.m_vecNormal * k1) + (_plane2.m_vecNormal * k2);
- return true;
- }
- return false;
- }
- bool Intersect(omPlane& _plane, omSphere& _sphere)
- {
- //Calculate a vector from the point on the plane to the center of the sphere
- omVector3 vecTemp = omVector3::Normalize(_plane.m_vecNormal);//un punto en el plano?
- //Calculate the distance: dot product of the new vector with the plane's normal
- float fDist = omVector3::DotProduct(vecTemp, _plane.m_vecNormal);
- if (fDist > _sphere.m_fRadius)
- {
- //The sphere is not touching the plane
- return false;
- }
- //Else, the sphere is colliding with the plane
- return true;
- }
- bool Intersect(omPlane& _plane, omRay& _ray)
- {
- float denom = omVector3::DotProduct(_plane.m_vecNormal, _ray.m_vecDirection);
- if (abs(denom) > PlatformMath::EPSILON) // your favorite epsilon
- {
- float t = (center - omVector3::DotProduct(_ray.m_vecOrigin, _plane.m_vecNormal)) / denom;//centro del plano?
- if (t >= PlatformMath::EPSILON) return true; // you might want to allow an epsilon here too
- }
- return false;
- }
- bool Intersect(omPlane& _plane, omAABB& _aabb)
- {
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Sphere Colitions //
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- bool Intersect(omSphere& _sphere, omPlane& _plane)
- {
- //Calculate a vector from the point on the plane to the center of the sphere
- omVector3 vecTemp(_sphere.m_vecCenter - vecPoint);//un punto en el plano?
- //Calculate the distance: dot product of the new vector with the plane's normal
- float fDist = omVector3::DotProduct(vecTemp, _plane.m_vecNormal);
- if (fDist > _sphere.m_fRadius)
- {
- //The sphere is not touching the plane
- return false;
- }
- //Else, the sphere is colliding with the plane
- return true;
- }
- bool Intersect(omSphere& _sphere, omSphere& _sphere2)
- {
- //<Calculamos distancia entre los centros de ambas esferas>//
- omVector3 vecDiff = _sphere.m_vecCenter - _sphere2.m_vecCenter;
- float distance = omVector3::Vector3Length(vecDiff);
- //<Suma de radios de las esferas>//
- float sumRadius = _sphere.m_fRadius + _sphere2.m_fRadius;
- //<si la distancia entre las esferas es menor a la suma de los radios entonces habra colision>//
- if (distance < sumRadius)
- return true;
- return false;
- }
- bool Intersect(omSphere& _sphere, omRay& _ray)
- {
- omVector3 vecTemp = _ray.m_vecOrigin - _sphere.m_vecCenter;
- //Squared distance between ray origin and sphere center
- float squaredDist = omVector3::DotProduct(vecTemp, vecTemp);
- float squaredRadius = PlatformMath::Square(_sphere.m_fRadius);
- //If the distance is less than the squared radius of the sphere...
- if (squaredDist <= squaredRadius)
- {
- //Point is in sphere, consider as no intersection existing
- return false;
- }
- //Will hold solution to quadratic equation
- float t0, t1;
- //Calculating the coefficients of the quadratic equation
- float a = omVector3::DotProduct(_ray.m_vecDirection, _ray.m_vecDirection); // a = d*d
- float b = 2.0f*(omVector3::DotProduct(_ray.m_vecDirection, vecTemp)); // b = 2d(o-C)
- float c = squaredDist - squaredRadius; // c = (o-C)^2-R^2
- //Calculate discriminant
- float disc = (b*b) - (4.0f*a*c);
- if (disc < 0) //If discriminant is negative no intersection happens
- {
- //std::cout << "No intersection with sphere..." << std::endl;
- return false;
- }
- else //If discriminant is positive one or two intersections (two solutions) exists
- {
- float sqrt_disc = PlatformMath::Sqrt(disc);
- t0 = (-b - sqrt_disc) / (2.0f * a);
- t1 = (-b + sqrt_disc) / (2.0f * a);
- }
- //If the second intersection has a negative value then the intersections
- //happen behind the ray origin which is not considered. Otherwise t0 is
- //the intersection to be considered
- if (t1<0)
- {
- //std::cout << "No intersection with sphere..." << std::endl;
- return false;
- }
- else
- {
- //std::cout << "Intersection with sphere..." << std::endl;
- return true;
- }
- }
- bool Intersect(omSphere& _sphere, omAABB& _aabb)
- {
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Ray Colitions //
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- bool Intersect(omRay& _ray, omPlane& _plane)
- {
- float denom = omVector3::DotProduct(_plane.m_vecNormal, _ray.m_vecDirection);
- if (abs(denom) > PlatformMath::EPSILON) // your favorite epsilon
- {
- float t = (center -omVector3::DotProduct(_ray.m_vecOrigin, _plane.m_vecNormal)) / denom;//centro del plano?
- if (t >= PlatformMath::EPSILON) return true; // you might want to allow an epsilon here too
- }
- return false;
- }
- bool Intersect(omRay& _ray, omSphere& _sphere)
- {
- omVector3 vecTemp = _ray.m_vecOrigin - _sphere.m_vecCenter;
- //Squared distance between ray origin and sphere center
- float squaredDist = omVector3::DotProduct(vecTemp, vecTemp);
- float squaredRadius = PlatformMath::Square(_sphere.m_fRadius);
- //If the distance is less than the squared radius of the sphere...
- if (squaredDist <= squaredRadius)
- {
- //Point is in sphere, consider as no intersection existing
- return false;
- }
- //Will hold solution to quadratic equation
- float t0, t1;
- //Calculating the coefficients of the quadratic equation
- float a = omVector3::DotProduct(_ray.m_vecDirection, _ray.m_vecDirection); // a = d*d
- float b = 2.0f*(omVector3::DotProduct(_ray.m_vecDirection, vecTemp)); // b = 2d(o-C)
- float c = squaredDist - squaredRadius; // c = (o-C)^2-R^2
- //Calculate discriminant
- float disc = (b*b) - (4.0f*a*c);
- if (disc < 0) //If discriminant is negative no intersection happens
- {
- //std::cout << "No intersection with sphere..." << std::endl;
- return false;
- }
- else //If discriminant is positive one or two intersections (two solutions) exists
- {
- float sqrt_disc = PlatformMath::Sqrt(disc);
- t0 = (-b - sqrt_disc) / (2.0f * a);
- t1 = (-b + sqrt_disc) / (2.0f * a);
- }
- //If the second intersection has a negative value then the intersections
- //happen behind the ray origin which is not considered. Otherwise t0 is
- //the intersection to be considered
- if (t1<0)
- {
- //std::cout << "No intersection with sphere..." << std::endl;
- return false;
- }
- else
- {
- //std::cout << "Intersection with sphere..." << std::endl;
- return true;
- }
- }
- bool Intersect(omRay& _ray, omRay& _ray2)
- {
- omVector3 Lt = _ray2.m_vecOrigin;
- Lt = Lt + _ray2.m_vecDirection;
- omVector3 Ls = _ray.m_vecOrigin + _ray.m_vecDirection;
- if (Lt == Ls)
- return true;
- return false;
- }
- bool Intersect(omRay& _ray, omAABB& _aabb)
- {
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Ray Colitions //
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- bool Intersect(omAABB& _aabb, omPlane& _plane)
- {
- }
- bool Intersect(omAABB& _aabb, omSphere& _sphere)
- {
- }
- bool Intersect(omAABB& _aabb, omRay& _ray)
- {
- }
- bool Intersect(omAABB& _aabb, omAABB& _aabb2)
- {
- if (abs(_aabb.m_vecCenter[0] - _aabb2.m_vecCenter[0]) > (_aabb.m_vecDimentions[0] + _aabb2.m_vecDimentions[0])) return false;
- if (abs(_aabb.m_vecCenter[1] - _aabb2.m_vecCenter[1]) > (_aabb.m_vecDimentions[1] + _aabb2.m_vecDimentions[1])) return false;
- if (abs(_aabb.m_vecCenter[2] - _aabb2.m_vecCenter[2]) > (_aabb.m_vecDimentions[2] + _aabb2.m_vecDimentions[2])) return false;
- //<Las cajas se Sobreponen>//
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement