2015-02-11 18:14:22 +08:00
|
|
|
/****************************************************************************
|
2015-02-25 18:05:41 +08:00
|
|
|
Copyright (C) 2013 Henry van Merode. All rights reserved.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
#include "CCPUUtil.h"
|
2015-02-11 18:14:22 +08:00
|
|
|
#include "base/ccMacros.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
cocos2d::Vec3 PUUtil::randomDeviant(const Vec3& src, float angle, const Vec3& up /*= Vec3::ZERO*/)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
Vec3 newUp;
|
|
|
|
|
|
|
|
if (up == Vec3::ZERO)
|
|
|
|
{
|
|
|
|
// Generate an up vector
|
|
|
|
newUp = perpendicular(src);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newUp = up;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate up vector by random amount around this
|
2021-12-25 10:04:45 +08:00
|
|
|
// Quaternion q;
|
|
|
|
// q.FromAngleAxis( Radian(Math::UnitRandom() * Math::TWO_PI), *this );
|
|
|
|
// newUp = q * newUp;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//// Finally rotate this by given angle around randomised up
|
2021-12-25 10:04:45 +08:00
|
|
|
// q.FromAngleAxis( angle, newUp );
|
|
|
|
// return q * (*this);
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Quaternion q;
|
|
|
|
Mat4 mat;
|
|
|
|
Quaternion::createFromAxisAngle(src, CCRANDOM_0_1() * M_PI * 2.0f, &q);
|
2015-02-11 18:14:22 +08:00
|
|
|
Mat4::createRotation(q, &mat);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
//{
|
|
|
|
// Vec3 uv, uuv;
|
|
|
|
// Vec3 qvec(q.x, q.y, q.z);
|
|
|
|
// Vec3::cross(qvec, newUp, &uv);
|
|
|
|
// Vec3::cross(qvec, uv, &uuv);
|
|
|
|
// uv *= (2.0f * q.w);
|
|
|
|
// uuv *= 2.0f;
|
|
|
|
// newUp = newUp + uv + uuv;
|
|
|
|
//}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
newUp = mat * newUp;
|
2021-12-25 10:04:45 +08:00
|
|
|
Quaternion::createFromAxisAngle(newUp, angle, &q);
|
2015-02-11 18:14:22 +08:00
|
|
|
Mat4::createRotation(q, &mat);
|
2021-12-25 10:04:45 +08:00
|
|
|
return mat * src;
|
|
|
|
|
|
|
|
//{
|
|
|
|
// Vec3 uv, uuv;
|
|
|
|
// Vec3 qvec(q.x, q.y, q.z);
|
|
|
|
// Vec3::cross(qvec, src, &uv);
|
|
|
|
// Vec3::cross(qvec, uv, &uuv);
|
|
|
|
// uv *= (2.0f * q.w);
|
|
|
|
// uuv *= 2.0f;
|
|
|
|
// return src + uv + uuv;
|
|
|
|
//}
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
cocos2d::Vec3 PUUtil::perpendicular(const Vec3& src)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// static const float fSquareZero = (float)(1e-06 * 1e-06);
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// Vector3 perp = this->crossProduct( Vector3::UNIT_X );
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//// Check length
|
2021-12-25 10:04:45 +08:00
|
|
|
// if( perp.squaredLength() < fSquareZero )
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
|
|
|
// /* This vector is the Y axis multiplied by a scalar, so we have
|
|
|
|
// to use another axis.
|
|
|
|
// */
|
|
|
|
// perp = this->crossProduct( Vector3::UNIT_Y );
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
|
|
|
// perp.normalise();
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// return perp;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
static const float fSquareZero = (float)(1e-06 * 1e-06);
|
|
|
|
Vec3 perp;
|
|
|
|
Vec3::cross(src, Vec3::UNIT_X, &perp);
|
|
|
|
// Check length
|
2021-12-25 10:04:45 +08:00
|
|
|
if (perp.lengthSquared() < fSquareZero)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
/* This vector is the Y axis multiplied by a scalar, so we have
|
|
|
|
to use another axis.
|
|
|
|
*/
|
|
|
|
Vec3::cross(src, Vec3::UNIT_Y, &perp);
|
|
|
|
}
|
|
|
|
perp.normalize();
|
|
|
|
return perp;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|