mirror of https://github.com/axmolengine/axmol.git
96 lines
2.6 KiB
C++
96 lines
2.6 KiB
C++
|
/****************************************************************************
|
||
|
Copyright (c) 2014 Chukong Technologies Inc.
|
||
|
|
||
|
http://www.cocos2d-x.org
|
||
|
|
||
|
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.
|
||
|
****************************************************************************/
|
||
|
|
||
|
#include "CCPlane.h"
|
||
|
|
||
|
NS_CC_BEGIN
|
||
|
|
||
|
Plane::Plane()
|
||
|
{
|
||
|
_normal.set(0.0f, 0.0f, 1.0f);
|
||
|
_dist = 0.0f;
|
||
|
}
|
||
|
|
||
|
// create plane from tree point
|
||
|
Plane::Plane(const Vec3& p1, const Vec3& p2, const Vec3& p3)
|
||
|
{
|
||
|
initPlane(p1, p2, p3);
|
||
|
}
|
||
|
|
||
|
// create plane from normal and dist
|
||
|
Plane::Plane(const Vec3& normal, float dist)
|
||
|
{
|
||
|
initPlane(normal, dist);
|
||
|
}
|
||
|
|
||
|
// create plane from normal and a point on plane
|
||
|
Plane::Plane(const Vec3& normal, const Vec3& point)
|
||
|
{
|
||
|
initPlane(normal, point);
|
||
|
}
|
||
|
|
||
|
void Plane::initPlane(const Vec3& p1, const Vec3& p2, const Vec3& p3)
|
||
|
{
|
||
|
Vec3 p21 = p2 - p1;
|
||
|
Vec3 p32 = p3 - p2;
|
||
|
|
||
|
Vec3::cross(p21, p32, &_normal);
|
||
|
|
||
|
_normal.normalize();
|
||
|
_dist = _normal.dot(p1);
|
||
|
}
|
||
|
|
||
|
void Plane::initPlane(const Vec3& normal, float dist)
|
||
|
{
|
||
|
float oneOverLength = 1 / normal.length();
|
||
|
_normal = normal * oneOverLength;
|
||
|
_dist = dist * oneOverLength;
|
||
|
}
|
||
|
|
||
|
void Plane::initPlane(const Vec3& normal, const Vec3& point)
|
||
|
{
|
||
|
_normal = normal;
|
||
|
_normal.normalize();
|
||
|
_dist = _normal.dot(point);
|
||
|
}
|
||
|
|
||
|
float Plane::dist2Plane(const Vec3& p) const
|
||
|
{
|
||
|
return _normal.dot(p) - _dist;
|
||
|
}
|
||
|
|
||
|
|
||
|
Plane::POINT_SIDE Plane::getSide(const Vec3& point) const
|
||
|
{
|
||
|
float dist = dist2Plane(point);
|
||
|
if (dist > 0)
|
||
|
return Plane::FRONT_PLANE;
|
||
|
else if (dist < 0)
|
||
|
return Plane::BEHIND_PLANE;
|
||
|
|
||
|
return Plane::ON_PLANE;
|
||
|
}
|
||
|
|
||
|
NS_CC_END
|