Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Change Unit of Measure  (Read 23898 times)

0 Members and 3 Guests are viewing this topic.

xerzi

  • Newbie
  • *
  • Posts: 15
    • View Profile
Change Unit of Measure
« on: July 08, 2013, 09:12:41 am »
I know this is unlikely to happen, it would break too much in terms of compatibility (perhaps in SFML 3.0 someday). Angles are currently represented as degrees, it would be much easier for a lot of implementations for angles to be represented as radians instead. Yah sure degrees are easier to work with, but realistically when do you ever use degrees ? Perhaps when you are manually inputting a constant variable (in this case having a toRadian() function would cost nothing in terms of computation as it is a constant) but in every other case it costs cycles. The standard C math libraries all use radians so whenever you want to calculate sin or otherwise computations are wasted again.

As an example if I choose to store my angles are radians I would need to convert to degrees and than say I pass it to Transform::rotate(), well rotate() needs to convert them back to radians to use the sin/cos functions so I do a conversion that was never necessary.

Transform& Transform::rotate(float angle)
{
    float rad = angle * 3.141592654f / 180.f;
    float cos = std::cos(rad);
    float sin = std::sin(rad);

    Transform rotation(cos, -sin, 0,
                       sin,  cos, 0,
                       0,    0,   1);

    return combine(rotation);
}

I guess a question for the author, any reasoning for the use of degrees over radians ?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Change Unit of Measure
« Reply #1 on: July 08, 2013, 12:42:07 pm »
The standard C math libraries all use radians so whenever you want to calculate sin or otherwise computations are wasted again.

I guess a question for the author, any reasoning for the use of degrees over radians ?

I am not the SFML author (Laurent is) or an OpenGL programmer but I am going to say I believe the answer lies with OpenGL. From the quick looking at OpenGL I just did it seems that OpenGL uses degrees instead of radians and since SFML is a wrapper around OpenGL, Laurent decided to let the conversions up to the user instead of providing an API with radians.

http://stackoverflow.com/questions/2146884/why-does-opengl-use-degrees-instead-of-radians
http://www.swiftless.com/tutorials/opengl/rotation.html
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Lynix

  • Sr. Member
  • ****
  • Posts: 403
    • View Profile
Re: Change Unit of Measure
« Reply #2 on: July 08, 2013, 01:17:43 pm »
The standard C math libraries all use radians so whenever you want to calculate sin or otherwise computations are wasted again.

I guess a question for the author, any reasoning for the use of degrees over radians ?

I am not the SFML author (Laurent is) or an OpenGL programmer but I am going to say I believe the answer lies with OpenGL. From the quick looking at OpenGL I just did it seems that OpenGL uses degrees instead of radians and since SFML is a wrapper around OpenGL, Laurent decided to let the conversions up to the user instead of providing an API with radians.

http://stackoverflow.com/questions/2146884/why-does-opengl-use-degrees-instead-of-radians
http://www.swiftless.com/tutorials/opengl/rotation.html

This has nothing to do with OpenGL, it's a choice Laurent has made, he doesn't use any OpenGL function that takes angles.
Degrees are easiers to work with, that's why (I think).
« Last Edit: July 08, 2013, 02:26:50 pm by Lynix »

xerzi

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Change Unit of Measure
« Reply #3 on: July 08, 2013, 02:55:16 pm »
Yah the only function that takes degrees in OpenGL is glRotatef and that's been deprecated since OpenGL 3.0+ and removed since 3.1+. I think that's a bad call if the only reason is degrees are easier to work with, at least from my standpoint if you want to be compatible with SFML that means you have to have a function to convert from degrees to radians whenever you want to use any trig. If I were to use radians than I'd need to convert from radians to degrees for SFML. If SFML used radians and I used radians though, there would be no need for conversion except for the odd case of a constant angle in code or printing some sort of debug message. In either case there is conversions but I find it is obvious which case would require less conversions and is more efficient than the other cases.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Change Unit of Measure
« Reply #4 on: July 08, 2013, 03:18:31 pm »
Yes, I chose degrees because they are more natural. A good question would be: why did the standard C library choose radians?

Quote
it costs cycles
Seriously? Come back when it becomes the bottleneck of your application... :P
Laurent Gomila - SFML developer

AxelRantila

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Twitter
Re: Change Unit of Measure
« Reply #5 on: July 08, 2013, 03:32:54 pm »
It's easier to count derivates and such in radians

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Change Unit of Measure
« Reply #6 on: July 08, 2013, 03:38:16 pm »
From Transformable.cpp:
////////////////////////////////////////////////////////////
const Transform& Transformable::getTransform() const
{
    // Recompute the combined transform if needed
    if (m_transformNeedUpdate)
    {
        float angle  = -m_rotation * 3.141592654f / 180.f;
        float cosine = static_cast<float>(std::cos(angle));
        float sine   = static_cast<float>(std::sin(angle));
        float sxc    = m_scale.x * cosine;
        float syc    = m_scale.y * cosine;
        float sxs    = m_scale.x * sine;
        float sys    = m_scale.y * sine;
        float tx     = -m_origin.x * sxc - m_origin.y * sys + m_position.x;
        float ty     =  m_origin.x * sxs - m_origin.y * syc + m_position.y;

        m_transform = Transform( sxc, sys, tx,
                                -sxs, syc, ty,
                                 0.f, 0.f, 1.f);
        m_transformNeedUpdate = false;
    }

    return m_transform;
}
So obviously Laurent accepts degrees, converts them to radians to generate the transformation matrix and feeds that to OpenGL via glLoadMatrix.

he doesn't use any OpenGL function that takes angles
He does the matrix maths himself instead of let OpenGL do it for him.

Yah the only function that takes degrees in OpenGL is glRotatef and that's been deprecated since OpenGL 3.0+ and removed since 3.1+.
Just like almost every other function Laurent uses in SFML... Laurent has other things to worry about than self-imposed deprecation restrictions. He already said OpenGL ES support was a goal, but until then, you just have to live with the fact that SFML runs on deprecated OpenGL code.

In either case there is conversions but I find it is obvious which case would require less conversions and is more efficient than the other cases.
You really shouldn't worry about angle conversions. It is a pure mult/div in hardware. It can be done in very few clock cycles if indeed more than a single one. This is not even nearly comparable to other things in your code.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

xerzi

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Change Unit of Measure
« Reply #7 on: July 08, 2013, 03:57:38 pm »
@Laurent
Believe that is partially at least because C took some roots from Fortran which was targeted for scientific computing which doesn't use degrees. Now though there is hardware support for said functions and last I've they take radians with no support for degrees. I guess it can be argued whether degrees are natural or not, from my viewpoint at least I haven't seen degrees on a mathematical equation for years all while using triple integrals to calculate volumes using degrees becomes irrelevant.

Also a bit curious why you didn't simply define a PI constant somewhere, do you have those several digits of PI memorized ?

You really shouldn't worry about angle conversions. It is a pure mult/div in hardware. It can be done in very few clock cycles if indeed more than a single one. This is not even nearly comparable to other things in your code.
Can be said otherwise when I have an entity with 5-10 angles all of which are used in multiple calculations, when I have on a scale of 1000 objects those few cycles now become a few 100 000 cycles of meaningless and avoidable operations.

Quote
Just like almost every other function Laurent uses in SFML... Laurent has other things to worry about than self-imposed deprecation restrictions. He already said OpenGL ES support was a goal, but until then, you just have to live with the fact that SFML runs on deprecated OpenGL code.
Wasn't really the intention of that comment, I could careless what he uses underneath, the API I am interacting with is what I am worried about. For 2D purposes there is no need for any of the new OpenGL features. The fixed pipeline was also great for debug purposes, bit bummed they completely removed it instead of just removing support in shader objects.

« Last Edit: July 08, 2013, 04:12:19 pm by xerzi »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Change Unit of Measure
« Reply #8 on: July 08, 2013, 04:02:21 pm »
A good question would be: why did the standard C library choose radians?
Probably because they are the standard unit for angles in mathematics. Many things are simpler to express with radians, see here. On the contrary, the degree scale is completely arbitrary.

I think it might be a valid point to consider in the future, but not because of cycles, rather to keep code simpler by avoiding conversions. One drawback is that you would have to define sf::Pi.
« Last Edit: July 08, 2013, 04:03:59 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Change Unit of Measure
« Reply #9 on: July 08, 2013, 04:09:00 pm »
Can be said otherwise when I have an entity with 5-10 angles all of which are used in multiple calculations, when I have on a scale of 1000 objects those few cycles now become a few 100 000 cycles of meaningless and avoidable operations.
Compare that to the 100 000 000 cycles you would need to do other meaningful things with those entities and you have a whole new picture.

A good question would be: why did the standard C library choose radians?
A good guess would be, because C was supposed to interface with hardware well, it had to provide an API that allows access to low level hardware operations, and maybe hardware FPUs handle radians better than degrees. I know for a fact that CORDIC works way better with radians than it does with degrees.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

xerzi

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Change Unit of Measure
« Reply #10 on: July 08, 2013, 04:53:59 pm »
Quote
Compare that to the 100 000 000 cycles you would need to do other meaningful things with those entities and you have a whole new picture.
What other meaningful stuff takes 100 000 000 do tell.

In any case not being wasteful is only an added benefit. Having to do conversions to have access to function calls is tedious and a design flaw imo.
« Last Edit: July 08, 2013, 05:06:28 pm by xerzi »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Change Unit of Measure
« Reply #11 on: July 08, 2013, 07:02:16 pm »
Quoting Knuth on premature optimisations:

Quote
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

Have a look around on the web to get why such modifications should not be considered because of performance. (If a small change improve the design, then that's another story.)
SFML / OS X developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Change Unit of Measure
« Reply #12 on: July 08, 2013, 07:08:52 pm »
It won't be changed until at least SFML 3.0 anyway (it breaks the API), so don't expect too much.
Laurent Gomila - SFML developer

Klaim

  • Full Member
  • ***
  • Posts: 137
    • View Profile
Re: Change Unit of Measure
« Reply #13 on: July 10, 2013, 10:03:48 am »
In case you want an example of how to provide both (and use only one internally), Ogre does it nicely. It have two types which can be converted to each other but only in an explicit way, which expose the places where conversion happen. It uses radians internally (to talk directly to DX and OGL with no conversion)  but the user easily can use degrees everywhere in his code and provide it to Ogre which will convert it before processing internally. Also easy to get the degree version of any angle value Ogre provide.
It's also pretty simple to setup. As Degree and Radian are different unrelated types there is no way to mix them without explicit conversion. See https://bitbucket.org/sinbad/ogre/src/0bba4f7cdb953c083aa9e3e15a13857e0988d48a/OgreMain/include/OgreMath.h?at=default for details, you can also see that it could be implemented in a simpler way but the idea is clear. (note that by default Real is float, you can change it to double in Ogre hard config macros)

Not sure if it can be done without breaking SFML 2.0 interface, so assuming it would be for 3.0, I think it's a nice example of design for providing angles values.


binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Change Unit of Measure
« Reply #14 on: July 10, 2013, 12:26:55 pm »
Since SFML 3.0 will probably have C++11 support, Laurent might want to try out making use of user defined literals in order to solve this problem ;). SFML would probably be one of the first libraries to make use of UDL, but you know it sounds too good to pass up :).
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

 

anything