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

Author Topic: Vector Math Functions  (Read 13496 times)

0 Members and 1 Guest are viewing this topic.

foobarbaz

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Vector Math Functions
« on: May 08, 2013, 07:32:34 am »
Hey all, I think Vector math functions (normalize, magnitude, rotation, etc.) are important for any game developer, so I think they'd be a good addition to SFML. Right now, every time I get the latest SFML, I have to go back and add in those functions to the Vector2 class. Sure, it only takes a few seconds, but if everyone is using them (and I have sure used these in just about every game I have ever made), why not just include them? I searched the forum a bit, and found that Vector math had already been discussed, but the link provided is dead.

We have already had a discussion about vector functions (-> Link)...

I understand that Vector2 is an abstracted class, but, honestly, who uses Vector2 for anything other than math? I know both Ogre3D and Irrlicht provide extensive math helper functions for their Vector classes.

        Vector2<T>& normalize()
        {
                float length = (float)(x*x + y*y);
                if (length == 0.f)
                        return *this;
                length = sqrt ( 1.f/length );
                x = (T)(x * length);
                y = (T)(y * length);
                return *this;
        }

        Vector2<T>& rotateBy(float degrees, const Vector2<T>& center=Vector2<T>())
        {
                degrees *= 3.14159f/180.f;
                const float cs = cos(degrees);
                const float sn = sin(degrees);

                x -= center.x;
                y -= center.y;

                T nx = (T)(x*cs - y*sn);
                T ny = (T)(x*sn + y*cs);

        x = nx;
        y = ny;

                x += center.x;
                y += center.y;
                return *this;
        }

    // returns trig angle
        float getAngle() const
        {
                if (y == 0)
                        return x < 0 ? 180 : 0;
                else if (x == 0)
                        return y < 0 ? 270 : 90;

                if ( y > 0)
                        if (x > 0)
                                return atan(y/x) * 180.f/3.14159f;
                        else
                                return 180.0-atan(y/-x) * 180.f/3.14159f;
                else
                        if (x > 0)
                                return 360.0-atan(-y/x) * 180.f/3.14159f;
                        else
                                return 180.0+atan(-y/-x) * 180.f/3.14159f;
        }

        float getLength()
        {
            return sqrt(x*x + y*y);
        }
 
« Last Edit: May 08, 2013, 07:36:32 am by DrSuperSocks »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
Re: Vector Math Functions
« Reply #1 on: May 08, 2013, 07:56:17 am »
SFML = Simple and Fast Multimedia Library
If you're looking for some math library, you might want to check out other libraries -- this or at least very similar requests have been discussed enough often. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Vector Math Functions
« Reply #2 on: May 08, 2013, 09:57:06 am »
If you have gotten to the point where sf::Vector just doesn't cut it anymore, you should start using a dedicated math/LA library such as Eigen. sf::Vector's only purpose is to fill in the holes in the SFML interface (and keep it nice, clean and RAII-ish) and as such should not try to do more than that.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Vector Math Functions
« Reply #3 on: May 08, 2013, 02:21:33 pm »
You should not follow the example of Ogre and Irrlicht, they do it completely wrong. In general, their C++ style often dates back to the nineties ;)

Don't implement everything as member function, especially not binary operators. You just prevent implicit conversions and create asymmetry issues. It is also questionable to overload all the possible operators just because it's possible (vector + scalar, seriously?).

If it helps you somehow, you can take a look (or directly use) the Vectors module of my Thor library. It contains many functions as a non-intrusive addition to SFML vectors. And it's generic.
« Last Edit: May 08, 2013, 02:24:10 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

foobarbaz

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Vector Math Functions
« Reply #4 on: May 08, 2013, 08:32:21 pm »
I can go down the list of APIs that include Vector classes with math helpers, including Unity, Box2D, Irrlicht, Ogre3D, CrystalSpace, etc. I just find it extremely hard to believe that this very small community is the only community on the entire planet that knows how to code properly. Just saying. But, on the other hand, I suppose I can understand the desire for minimalism. I suppose I will just throw these in a math header.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Vector Math Functions
« Reply #5 on: May 08, 2013, 08:58:30 pm »
Quote
I just find it extremely hard to believe that this very small community is the only community on the entire planet that knows how to code properly. Just saying. But, on the other hand, I suppose I can understand the desire for minimalism. I suppose I will just throw these in a math header.
You compare apples with pears. ;) SFML is not a game engine -- all the other libraries you mentioned are, however (except Box2D, but it's relatively clear that it needs more math features than SFML). SFML is a high-level interface for working with hardware in regard to multimedia. If SFML doesn't need vector algebra itself, it won't include it in its headers.

And hey, this community is everything but small. ;) Btw, Laurent, any chance to get some stats of downloads/web page visits, out of curiosity?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Vector Math Functions
« Reply #6 on: May 08, 2013, 09:19:45 pm »
I just find it extremely hard to believe that this very small community is the only community on the entire planet that knows how to code properly. Just saying.
What does the decision to include or exclude a certain feature have to do with the knowledge to code properly?

In case you are referring to my statement that Ogre and Irrlicht do it wrong: If you read books by C++ experts like Herb Sutter, they will share my point of view concerning modern C++. Don't hesitate to ask if it's unclear what "wrong" means and how one could do it better ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Vector Math Functions
« Reply #7 on: May 08, 2013, 09:27:35 pm »
Quote
Don't hesitate to ask if it's unclear what "wrong" means and how one could do it better ;)
What does Irrlicht do wrong other than reinventing the wheel lots of times? :P It apparently is designed very good and cleanly. http://cppdepend.wordpress.com/2009/11/21/irrlichtthe-art-of-using-grasp-patterns/
Quote
If you read books by C++ experts like Herb Sutter, they will share my point of view concerning modern C++.
Most people sadly don't. :(
« Last Edit: May 08, 2013, 09:30:58 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Vector Math Functions
« Reply #8 on: May 08, 2013, 09:49:24 pm »
What does Irrlicht do wrong other than reinventing the wheel lots of times? :P
Just some points that directly come to my mind:
  • Inconsistent hungarian notation (some types have it, some not, loads of useless prefixes)
  • Manual reference counting
  • The operator issues I mentioned above
  • No use of STL

It apparently is designed very good and cleanly.
Yes, the API is basically nice and easy enough to use. Also the class hierarchies are well-thought. I personally like the library, especially in comparison with the heavyweight Ogre. And concerning the functionality, Irrlicht is really great -- it's not that I consider it a bad library, not at all.

It's just that today, one would almost certainly approach things differently. This concerns rather code style and idioms than OOP design.
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: Vector Math Functions
« Reply #9 on: May 08, 2013, 10:32:21 pm »
Don't make me start about OpenSceneGraph... Most horrible thing I have been forced to work with so far... of course for a University project ;).
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vector Math Functions
« Reply #10 on: May 09, 2013, 08:55:21 am »
Quote
Btw, Laurent, any chance to get some stats of downloads/web page visits, out of curiosity?
I can give some stats, yes. You should open a new thread and list which stats you would like me to show you.

Sorry for the off-topic.
Laurent Gomila - SFML developer