Jump to content
  • Advertisement
Sign in to follow this  
exorcist_bob

Unity conversion between radians and degrees

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Community! I just had an idea on how to make radian/degree conversion easier in my engine. What if I encapsulated each in a class that automatically makes the conversion. However, in a previous post, the concept of making a class based off of intrisic was doomed to failure. ex:
class Radian
{
	Real mRad;

public:
	explicit Radian ( Real r=0 ) : mRad(r) {}
	Radian ( const Degree& d );
	const Radian& operator = ( const Real& f ) { mRad = f; return *this; }
	const Radian& operator = ( const Radian& r ) { mRad = r.mRad; return *this; }
	const Radian& operator = ( const Degree& d );

	Real GetDegrees() const; // see bottom of this file
	Real GetRadians() const { return mRad; }
	Real GetAngleUnits() const;

	Radian operator + ( const Radian& r ) const { return Radian ( mRad + r.mRad ); }
	Radian operator + ( const Degree& d ) const;
	Radian& operator += ( const Radian& r ) { mRad += r.mRad; return *this; }
	Radian& operator += ( const Degree& d );
	Radian operator - () { return Radian(-mRad); }
	Radian operator - ( const Radian& r ) const { return Radian ( mRad - r.mRad ); }
	Radian operator - ( const Degree& d ) const;
	Radian& operator -= ( const Radian& r ) { mRad -= r.mRad; return *this; }
	Radian& operator -= ( const Degree& d );
	Radian operator * ( Real f ) const { return Radian ( mRad * f ); }
    Radian operator * ( const Radian& f ) const { return Radian ( mRad * f.mRad ); }
	Radian& operator *= ( Real f ) { mRad *= f; return *this; }
	Radian operator / ( Real f ) const { return Radian ( mRad / f ); }
	Radian& operator /= ( Real f ) { mRad /= f; return *this; }

	bool operator <  ( const Radian& r ) const { return mRad <  r.mRad; }
	bool operator <= ( const Radian& r ) const { return mRad <= r.mRad; }
	bool operator == ( const Radian& r ) const { return mRad == r.mRad; }
	bool operator != ( const Radian& r ) const { return mRad != r.mRad; }
	bool operator >= ( const Radian& r ) const { return mRad >= r.mRad; }
	bool operator >  ( const Radian& r ) const { return mRad >  r.mRad; }
};

Where 'Real' can be typedef'd to float or double; PS. This is from the OGRE engine [smile] I'm just curious to see if it sounds like a good idea, exorcist_bob

Share this post


Link to post
Share on other sites
Advertisement
Why not just make the interface to all your engine's methods take radians, rather than an arbitrary combination of different angular units? Then you don't need a separate class at all.

Share this post


Link to post
Share on other sites
The reason for it is for extensibilities sake, so that if someone else builds a module that returns degrees, I can easily convert it over.

Share this post


Link to post
Share on other sites
You don't need full blown classes for that. Just have a rad_2_deg() and deg_2_rad() function and you're set.

Share this post


Link to post
Share on other sites
Quote:
Original post by exorcist_bob
The reason for it is for extensibilities sake, so that if someone else builds a module that returns degrees, I can easily convert it over.


YAGNI ?

What's wrong with:
namespace {
const float pi = 3.14159f;

inline float degToRad(float rad)
{
return rad * 180.0f / pi;
}

inline float radToDeg(float deg)
{
return deg * pi / 180.0f;
}
}

Share this post


Link to post
Share on other sites
or even:

#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f

Share this post


Link to post
Share on other sites
Quote:
Original post by exorcist_bob
The reason for it is for extensibilities sake, so that if someone else builds a module that returns degrees, I can easily convert it over.

As the engine's interface designer, you get to specify what is reasonable and what is not. My opinion is that if you already support radians, you need not support degrees, and vice versa. That is, it is not unreasonable to declare "my interface shall only take radians, and your modules must comply accordingly".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!