[cpp] Fixed constants in MathUtil.

This commit is contained in:
badlogic 2018-06-06 16:24:46 +02:00
parent b724853158
commit e1daddebf4
10 changed files with 59 additions and 54 deletions

View File

@ -33,20 +33,20 @@
#include <spine/SpineObject.h>
#include <float.h>
#include <string.h>
namespace spine {
static const float PI = 3.1415926535897932385f;
static const float PI_2 = PI * 2;
static const float DEG_RAD = (PI / 180.0f);
static const float RAD_DEG = (180.0f / PI);
class MathUtil : public SpineObject {
private:
MathUtil();
public:
static const float Pi;
static const float Pi_2;
static const float Deg_Rad;
static const float Rad_Deg;
template<typename T>
static inline T min(T a, T b) { return a < b ? a : b; }

View File

@ -164,11 +164,11 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float
s = MathUtil::abs(pa * pd - pb * pc) / s;
pb = pc * s;
pd = pa * s;
prx = MathUtil::atan2(pc, pa) * RAD_DEG;
prx = MathUtil::atan2(pc, pa) * MathUtil::Rad_Deg;
} else {
pa = 0;
pc = 0;
prx = 90 - MathUtil::atan2(pd, pb) * RAD_DEG;
prx = 90 - MathUtil::atan2(pd, pb) * MathUtil::Rad_Deg;
}
rx = rotation + shearX - prx;
ry = rotation + shearY - prx + 90;
@ -195,7 +195,7 @@ Bone::updateWorldTransform(float x, float y, float rotation, float scaleX, float
za *= s;
zc *= s;
s = MathUtil::sqrt(za * za + zc * zc);
r = PI / 2 + MathUtil::atan2(zc, za);
r = MathUtil::Pi / 2 + MathUtil::atan2(zc, za);
zb = MathUtil::cos(r) * s;
zd = MathUtil::sin(r) * s;
la = MathUtil::cosDeg(shearX) * scaleX;
@ -258,14 +258,14 @@ float Bone::worldToLocalRotation(float worldRotation) {
float sin = MathUtil::sinDeg(worldRotation);
float cos = MathUtil::cosDeg(worldRotation);
return MathUtil::atan2(_a * sin - _c * cos, _d * cos - _b * sin) * RAD_DEG;
return MathUtil::atan2(_a * sin - _c * cos, _d * cos - _b * sin) * MathUtil::Rad_Deg;
}
float Bone::localToWorldRotation(float localRotation) {
float sin = MathUtil::sinDeg(localRotation);
float cos = MathUtil::cosDeg(localRotation);
return MathUtil::atan2(cos * _c + sin * _d, cos * _a + sin * _b) * RAD_DEG;
return MathUtil::atan2(cos * _c + sin * _d, cos * _a + sin * _b) * MathUtil::Rad_Deg;
}
void Bone::rotateWorld(float degrees) {
@ -298,7 +298,7 @@ float Bone::getWorldToLocalRotationX() {
float a = _a;
float c = _c;
return MathUtil::atan2(pa * c - pc * a, pd * a - pb * c) * RAD_DEG;
return MathUtil::atan2(pa * c - pc * a, pd * a - pb * c) * MathUtil::Rad_Deg;
}
float Bone::getWorldToLocalRotationY() {
@ -314,7 +314,7 @@ float Bone::getWorldToLocalRotationY() {
float b = _b;
float d = _d;
return MathUtil::atan2(pa * d - pc * b, pd * b - pb * d) * RAD_DEG;
return MathUtil::atan2(pa * d - pc * b, pd * b - pb * d) * MathUtil::Rad_Deg;
}
BoneData &Bone::getData() {
@ -494,11 +494,11 @@ void Bone::setWorldY(float inValue) {
}
float Bone::getWorldRotationX() {
return MathUtil::atan2(_c, _a) * RAD_DEG;
return MathUtil::atan2(_c, _a) * MathUtil::MathUtil::Rad_Deg;
}
float Bone::getWorldRotationY() {
return MathUtil::atan2(_d, _b) * RAD_DEG;
return MathUtil::atan2(_d, _b) * MathUtil::Rad_Deg;
}
float Bone::getWorldScaleX() {
@ -522,11 +522,11 @@ void Bone::updateAppliedTransform() {
if (!parent) {
_ax = _worldX;
_ay = _worldY;
_arotation = MathUtil::atan2(_c, _a) * RAD_DEG;
_arotation = MathUtil::atan2(_c, _a) * MathUtil::Rad_Deg;
_ascaleX = MathUtil::sqrt(_a * _a + _c * _c);
_ascaleY = MathUtil::sqrt(_b * _b + _d * _d);
_ashearX = 0;
_ashearY = MathUtil::atan2(_a * _b + _c * _d, _a * _d - _b * _c) * RAD_DEG;
_ashearY = MathUtil::atan2(_a * _b + _c * _d, _a * _d - _b * _c) * MathUtil::Rad_Deg;
} else {
float pa = parent->_a, pb = parent->_b, pc = parent->_c, pd = parent->_d;
float pid = 1 / (pa * pd - pb * pc);
@ -546,13 +546,13 @@ void Bone::updateAppliedTransform() {
if (_ascaleX > 0.0001f) {
float det = ra * rd - rb * rc;
_ascaleY = det / _ascaleX;
_ashearY = MathUtil::atan2(ra * rb + rc * rd, det) * RAD_DEG;
_arotation = MathUtil::atan2(rc, ra) * RAD_DEG;
_ashearY = MathUtil::atan2(ra * rb + rc * rd, det) * MathUtil::Rad_Deg;
_arotation = MathUtil::atan2(rc, ra) * MathUtil::Rad_Deg;
} else {
_ascaleX = 0;
_ascaleY = MathUtil::sqrt(rb * rb + rd * rd);
_ashearY = 0;
_arotation = 90 - MathUtil::atan2(rd, rb) * RAD_DEG;
_arotation = 90 - MathUtil::atan2(rd, rb) * MathUtil::Rad_Deg;
}
}
}

View File

@ -52,7 +52,7 @@ void IkConstraint::apply(Bone &bone, float targetX, float targetY, float alpha)
x = targetX - p->_worldX, y = targetY - p->_worldY;
tx = (x * p->_d - y * p->_b) * id - bone._ax;
ty = (y * p->_a - x * p->_c) * id - bone._ay;
rotationIK = MathUtil::atan2(ty, tx) * RAD_DEG - bone._ashearX - bone._arotation;
rotationIK = MathUtil::atan2(ty, tx) * MathUtil::Rad_Deg - bone._ashearX - bone._arotation;
if (bone._ascaleX < 0) rotationIK += 180;
if (rotationIK > 180) rotationIK -= 360;
else if (rotationIK < -180) rotationIK += 360;
@ -148,7 +148,7 @@ void IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY
}
}
{
float minAngle = PI, minX = l1 - a, minDist = minX * minX, minY = 0;
float minAngle = MathUtil::Pi, minX = l1 - a, minDist = minX * minX, minY = 0;
float maxAngle = 0, maxX = l1 + a, maxDist = maxX * maxX, maxY = 0;
c0 = -a * l1 / (aa - bb);
if (c0 >= -1 && c0 <= 1) {
@ -181,11 +181,11 @@ void IkConstraint::apply(Bone &parent, Bone &child, float targetX, float targetY
break_outer:
{
float os = MathUtil::atan2(cy, cx) * s2;
a1 = (a1 - os) * RAD_DEG + o1 - parent._arotation;
a1 = (a1 - os) * MathUtil::Rad_Deg + o1 - parent._arotation;
if (a1 > 180) a1 -= 360;
else if (a1 < -180) a1 += 360;
parent.updateWorldTransform(px, py, parent._rotation + a1 * alpha, parent._ascaleX, parent._ascaleY, 0, 0);
a2 = ((a2 + os) * RAD_DEG - child._ashearX) * s2 + o2 - child._arotation;
a2 = ((a2 + os) * MathUtil::Rad_Deg - child._ashearX) * s2 + o2 - child._arotation;
if (a2 > 180) a2 -= 360;
else if (a2 < -180) a2 += 360;
child.updateWorldTransform(cx, cy, child._arotation + a2 * alpha, child._ascaleX, child._ascaleY,

View File

@ -39,6 +39,11 @@
using namespace spine;
const float MathUtil::Pi = 3.1415926535897932385f;
const float MathUtil::Pi_2 = 3.1415926535897932385f * 2;
const float MathUtil::Deg_Rad = (3.1415926535897932385f / 180.0f);
const float MathUtil::Rad_Deg = (180.0f / 3.1415926535897932385f);
float MathUtil::abs(float v) {
return ((v) < 0 ? -(v) : (v));
}
@ -81,12 +86,12 @@ float MathUtil::acos(float v) {
/// Returns the sine in radians from a lookup table.
float MathUtil::sinDeg(float degrees) {
return (float)::sin(degrees * DEG_RAD);
return (float)::sin(degrees * MathUtil::Deg_Rad);
}
/// Returns the cosine in radians from a lookup table.
float MathUtil::cosDeg(float degrees) {
return (float)::cos(degrees * DEG_RAD);
return (float)::cos(degrees * MathUtil::Deg_Rad);
}
/* Need to pass 0 as an argument, so VC++ doesn't error with C2124 */

View File

@ -148,7 +148,7 @@ void PathConstraint::update() {
} else {
tip = false;
Bone &p = _target->getBone();
offsetRotation *= p.getA() * p.getD() - p.getB() * p.getC() > 0 ? DEG_RAD : -DEG_RAD;
offsetRotation *= p.getA() * p.getD() - p.getB() * p.getC() > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad;
}
for (size_t i = 0, p = 3; i < boneCount; i++, p += 3) {
@ -194,10 +194,10 @@ void PathConstraint::update() {
r += offsetRotation;
}
if (r > PI) {
r -= PI_2;
} else if (r < -PI) {
r += PI_2;
if (r > MathUtil::Pi) {
r -= MathUtil::Pi_2;
} else if (r < -MathUtil::Pi) {
r += MathUtil::Pi_2;
}
r *= rotateMix;

View File

@ -55,7 +55,7 @@ float PointAttachment::computeWorldRotation(Bone &bone) {
float ix = cos * bone._a + sin * bone._b;
float iy = cos * bone._c + sin * bone._d;
return MathUtil::atan2(iy, ix) * RAD_DEG;
return MathUtil::atan2(iy, ix) * MathUtil::Rad_Deg;
}
float PointAttachment::getX() {

View File

@ -443,7 +443,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
Json *attachmentMap;
for (attachmentMap = attachmentsMap->_child; attachmentMap; attachmentMap = attachmentMap->_next) {
Attachment *attachment;
Attachment *attachment = NULL;
const char *skinAttachmentName = attachmentMap->_name;
const char *attachmentName = Json::getString(attachmentMap, "name", skinAttachmentName);
const char *attachmentPath = Json::getString(attachmentMap, "path", attachmentName);

View File

@ -138,8 +138,8 @@ void Skin::attachAll(Skeleton &skeleton, Skin &oldSkin) {
Slot *slot = slots[slotIndex];
if (slot->getAttachment() == entry._attachment) {
Attachment *attachment = NULL;
if ((attachment = getAttachment(slotIndex, entry._name))) {
Attachment *attachment = getAttachment(slotIndex, entry._name);
if (attachment) {
slot->setAttachment(attachment);
}
}

View File

@ -140,7 +140,7 @@ void TransformConstraint::applyAbsoluteWorld() {
float rotateMix = _rotateMix, translateMix = _translateMix, scaleMix = _scaleMix, shearMix = _shearMix;
Bone &target = *_target;
float ta = target._a, tb = target._b, tc = target._c, td = target._d;
float degRadReflect = ta * td - tb * tc > 0 ? DEG_RAD : -DEG_RAD;
float degRadReflect = ta * td - tb * tc > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad;
float offsetRotation = _data._offsetRotation * degRadReflect, offsetShearY = _data._offsetShearY * degRadReflect;
for (size_t i = 0; i < _bones.size(); ++i) {
@ -152,10 +152,10 @@ void TransformConstraint::applyAbsoluteWorld() {
if (rotateMix != 0) {
float a = bone._a, b = bone._b, c = bone._c, d = bone._d;
float r = MathUtil::atan2(tc, ta) - MathUtil::atan2(c, a) + offsetRotation;
if (r > PI) {
r -= PI_2;
} else if (r < -PI) {
r += PI_2;
if (r > MathUtil::Pi) {
r -= MathUtil::Pi_2;
} else if (r < -MathUtil::Pi) {
r += MathUtil::Pi_2;
}
r *= rotateMix;
@ -197,10 +197,10 @@ void TransformConstraint::applyAbsoluteWorld() {
float b = bone._b, d = bone._d;
float by = MathUtil::atan2(d, b);
float r = MathUtil::atan2(td, tb) - MathUtil::atan2(tc, ta) - (by - MathUtil::atan2(bone._c, bone._a));
if (r > PI) {
r -= PI_2;
} else if (r < -PI) {
r += PI_2;
if (r > MathUtil::Pi) {
r -= MathUtil::Pi_2;
} else if (r < -MathUtil::Pi) {
r += MathUtil::Pi_2;
}
r = by + (r + offsetShearY) * shearMix;
@ -220,7 +220,7 @@ void TransformConstraint::applyRelativeWorld() {
float rotateMix = _rotateMix, translateMix = _translateMix, scaleMix = _scaleMix, shearMix = _shearMix;
Bone &target = *_target;
float ta = target._a, tb = target._b, tc = target._c, td = target._d;
float degRadReflect = ta * td - tb * tc > 0 ? DEG_RAD : -DEG_RAD;
float degRadReflect = ta * td - tb * tc > 0 ? MathUtil::Deg_Rad : -MathUtil::Deg_Rad;
float offsetRotation = _data._offsetRotation * degRadReflect, offsetShearY = _data._offsetShearY * degRadReflect;
for (size_t i = 0; i < _bones.size(); ++i) {
Bone *item = _bones[i];
@ -231,10 +231,10 @@ void TransformConstraint::applyRelativeWorld() {
if (rotateMix != 0) {
float a = bone._a, b = bone._b, c = bone._c, d = bone._d;
float r = MathUtil::atan2(tc, ta) + offsetRotation;
if (r > PI) {
r -= PI_2;
} else if (r < -PI) {
r += PI_2;
if (r > MathUtil::Pi) {
r -= MathUtil::Pi_2;
} else if (r < -MathUtil::Pi) {
r += MathUtil::Pi_2;
}
r *= rotateMix;
@ -266,14 +266,14 @@ void TransformConstraint::applyRelativeWorld() {
if (shearMix > 0) {
float r = MathUtil::atan2(td, tb) - MathUtil::atan2(tc, ta);
if (r > PI) {
r -= PI_2;
} else if (r < -PI) {
r += PI_2;
if (r > MathUtil::Pi) {
r -= MathUtil::Pi_2;
} else if (r < -MathUtil::Pi) {
r += MathUtil::Pi_2;
}
float b = bone._b, d = bone._d;
r = MathUtil::atan2(d, b) + (r - PI / 2 + offsetShearY) * shearMix;
r = MathUtil::atan2(d, b) + (r - MathUtil::Pi / 2 + offsetShearY) * shearMix;
float s = MathUtil::sqrt(b * b + d * d);
bone._b = MathUtil::cos(r) * s;
bone._d = MathUtil::sin(r) * s;

View File

@ -136,7 +136,7 @@ float SwirlVertexEffect::getRadius() {
}
void SwirlVertexEffect::setAngle(float angle) {
_angle = angle * spine::DEG_RAD;
_angle = angle * MathUtil::Deg_Rad;
}
float SwirlVertexEffect::getAngle() {