Make CRelAngle conform input angles to full rotation

This commit is contained in:
Jack Andersen 2017-05-04 08:55:40 -10:00
parent 85e340855a
commit 83ef557a02
1 changed files with 20 additions and 8 deletions

View File

@ -13,8 +13,18 @@ struct CRelAngle
{ {
float angle = 0.f; float angle = 0.f;
static float MakeRelativeAngle(float angle)
{
float ret = angle - std::floor(angle / (2.f * M_PIF)) * (2.f * M_PIF);
if (ret < 0.f)
ret += 2.f * M_PIF;
return ret;
}
CRelAngle() = default; CRelAngle() = default;
CRelAngle(float angle) : angle(angle) {} CRelAngle(float angle) : angle(MakeRelativeAngle(angle)) {}
CRelAngle& operator=(float ang) { angle = MakeRelativeAngle(ang); return *this; }
CRelAngle& operator=(const CRelAngle& ang) { angle = ang.angle; return *this; }
float asDegrees() const { return radToDeg(angle); } float asDegrees() const { return radToDeg(angle); }
float asRadians() const { return angle; } float asRadians() const { return angle; }
float arcCosine() const { return std::acos(angle); } float arcCosine() const { return std::acos(angle); }
@ -22,7 +32,7 @@ struct CRelAngle
static CRelAngle FromDegrees(float angle) static CRelAngle FromDegrees(float angle)
{ {
CRelAngle ret; CRelAngle ret;
ret.angle = degToRad(angle); ret.angle = MakeRelativeAngle(degToRad(angle));
return ret; return ret;
} }
@ -30,12 +40,14 @@ struct CRelAngle
static CRelAngle FromRadians(float angle) { return CRelAngle(angle); } static CRelAngle FromRadians(float angle) { return CRelAngle(angle); }
bool operator <(const CRelAngle& other) const { return angle < other.angle; } bool operator <(const CRelAngle& other) const { return angle < other.angle; }
CRelAngle& operator +=(const CRelAngle& other) { angle += other.angle; return *this; } CRelAngle& operator +=(const CRelAngle& other) { angle = MakeRelativeAngle(angle + other.angle); return *this; }
CRelAngle& operator +=(float r) { angle += r; return *this; } CRelAngle& operator +=(float r) { angle = MakeRelativeAngle(angle + r); return *this; }
CRelAngle& operator *=(const CRelAngle& other) { angle *= other.angle; return *this; } CRelAngle& operator -=(const CRelAngle& other) { angle = MakeRelativeAngle(angle - other.angle); return *this; }
CRelAngle& operator *=(float r) { angle *= r; return *this;} CRelAngle& operator -=(float r) { angle = MakeRelativeAngle(angle - r); return *this; }
CRelAngle& operator /=(const CRelAngle& other) { angle /= other.angle; return *this;} CRelAngle& operator *=(const CRelAngle& other) { angle = MakeRelativeAngle(angle * other.angle); return *this; }
CRelAngle& operator /=(float r) { angle /= r; return *this;} CRelAngle& operator *=(float r) { angle = MakeRelativeAngle(angle * r); return *this; }
CRelAngle& operator /=(const CRelAngle& other) { angle = MakeRelativeAngle(angle / other.angle); return *this; }
CRelAngle& operator /=(float r) { angle = MakeRelativeAngle(angle / r); return *this; }
}; };
} }