/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #define _MAGNUM_DO_NOT_WARN_DEPRECATED_SHAPES #include "Sphere.h" #include "Magnum/Magnum.h" #include "Magnum/Math/Distance.h" #include "Magnum/Math/Functions.h" #include "Magnum/Math/Matrix3.h" #include "Magnum/Math/Matrix4.h" #include "Magnum/Shapes/LineSegment.h" #include "Magnum/Shapes/Point.h" namespace Magnum { namespace Shapes { CORRADE_IGNORE_DEPRECATED_PUSH template Sphere Sphere::transformed(const MatrixTypeFor& matrix) const { return Sphere(matrix.transformPoint(_position), matrix.uniformScaling()*_radius); } template bool Sphere::operator%(const Point& other) const { return (_position - other.position()).dot() < Math::pow<2>(_radius); } template bool InvertedSphere::operator%(const Point& other) const { return (other.position() - position()).dot() > Math::pow<2>(radius()); } template Collision Sphere::operator/(const Point& other) const { const VectorTypeFor separating = _position - other.position(); const Float dot = separating.dot(); /* No collision occured */ if(dot > Math::pow<2>(_radius)) return {}; /* Actual distance from the center */ const Float distance = Math::sqrt(dot); /* Separating normal. If can't decide on direction, just move up. */ /** @todo How to handle this in a configurable way? */ const VectorTypeFor separatingNormal = Math::TypeTraits::equals(dot, 0.0f) ? VectorTypeFor::yAxis() : separating/distance; /* Collision position is on the point */ return Collision(other.position(), separatingNormal, _radius - distance); } template Collision InvertedSphere::operator/(const Point& other) const { const VectorTypeFor separating = other.position() - position(); const Float dot = separating.dot(); /* No collision occured */ if(dot < Math::pow<2>(radius())) return {}; /* Actual distance from the center */ const Float distance = Math::sqrt(dot); /* Separating normal */ const VectorTypeFor separatingNormal = separating/distance; /* Collision position is on the point */ return Collision(other.position(), separatingNormal, distance - radius()); } template bool Sphere::operator%(const Line& other) const { return Math::Distance::linePointSquared(other.a(), other.b(), _position) < Math::pow<2>(_radius); } template bool Sphere::operator%(const LineSegment& other) const { return Math::Distance::lineSegmentPointSquared(other.a(), other.b(), _position) < Math::pow<2>(_radius); } template bool Sphere::operator%(const Sphere& other) const { return (_position - other._position).dot() < Math::pow<2>(_radius + other._radius); } template bool InvertedSphere::operator%(const Sphere& other) const { return (position() - other.position()).dot() > Math::pow<2>(radius() - other.radius()); } template Collision Sphere::operator/(const Sphere& other) const { const Float minDistance = _radius + other._radius; const VectorTypeFor separating = _position - other._position; const Float dot = separating.dot(); /* No collision occured */ if(dot > Math::pow<2>(minDistance)) return {}; /* Actual distance */ const Float distance = Math::sqrt(dot); /* Separating normal. If can't decide on direction, just move up. */ /** @todo How to handle this in a configurable way? */ const VectorTypeFor separatingNormal = Math::TypeTraits::equals(dot, 0.0f) ? VectorTypeFor::yAxis() : separating/distance; /* Contact position is on the surface of `other`, minDistace > distance */ return Collision(other._position + separatingNormal*other._radius, separatingNormal, minDistance - distance); } template Collision InvertedSphere::operator/(const Sphere& other) const { const Float maxDistance = radius() - other.radius(); /** @todo How to handle inseparable shapes or shapes which can't be separated by movement only (i.e. two half-spaces)? */ CORRADE_INTERNAL_ASSERT(maxDistance > 0.0f); const VectorTypeFor separating = other.position() - position(); const Float dot = separating.dot(); /* No collision occured */ if(dot < Math::pow<2>(maxDistance)) return {}; /* Actual distance */ const Float distance = Math::sqrt(dot); /* Separating normal */ const VectorTypeFor separatingNormal = separating/distance; /* Contact position is on the surface of `other`, distance > maxDistance */ return Collision(other.position() + separatingNormal*other.radius(), separatingNormal, distance - maxDistance); } #ifndef DOXYGEN_GENERATING_OUTPUT template class MAGNUM_SHAPES_EXPORT Sphere<2>; template class MAGNUM_SHAPES_EXPORT Sphere<3>; template class MAGNUM_SHAPES_EXPORT InvertedSphere<2>; template class MAGNUM_SHAPES_EXPORT InvertedSphere<3>; #endif CORRADE_IGNORE_DEPRECATED_POP }}