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

Author Topic: Using SFML with math libraries? (GLM, Eigen...)  (Read 5522 times)

0 Members and 1 Guest are viewing this topic.

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Using SFML with math libraries? (GLM, Eigen...)
« on: March 10, 2017, 08:55:56 am »
I was wondering if someone here uses math lib along with SFML. I just find sf::Vector2f functionality lacking a lot of time when I need to compute something hard. I ended up writing a bunch of mathematical functions for them myself, but the number of them is increasing and I'm considering to use GLM for all the math stuff.

So, my question is: have anyone used it succesfully SFML and what were the steps with SFML's integration? Maybe I should add some implicit conversions from sf::Vector2f to glm::vec2 and vice versa? Or maybe I should always use doSomething(float x, float y) when calling SFML's API when possible?

What other things may I want to do?
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using SFML with math libraries? (GLM, Eigen...)
« Reply #1 on: March 10, 2017, 09:09:02 am »
The function overloads that take separate components will vanish (probably in SFML 3), so I wouldn't use them too much.

Automatic conversions between the two types is what I'd probably do.
Laurent Gomila - SFML developer

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: Using SFML with math libraries? (GLM, Eigen...)
« Reply #2 on: March 10, 2017, 09:13:39 am »
Yeah, and frequently converting from glm::vec2 to sf::Vector2f shouldn't cause much overhead, right?

And I wonder if I should just start using glm stuff for everything or still use sf::Vector2f in some places?
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using SFML with math libraries? (GLM, Eigen...)
« Reply #3 on: March 10, 2017, 09:24:44 am »
Quote
Yeah, and frequently converting from glm::vec2 to sf::Vector2f shouldn't cause much overhead, right?
I don't think you'll get to the point where these conversions noticeably slow down your application :)

Quote
And I wonder if I should just start using glm stuff for everything or still use sf::Vector2f in some places?
That's hard to say without being in this situation, but I think I'd try glm everywhere, and only convert when interfacing with SFML.
Laurent Gomila - SFML developer

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: Using SFML with math libraries? (GLM, Eigen...)
« Reply #4 on: March 10, 2017, 09:27:07 am »
Okay, thanks! I'll see how it will work out and will ask some questions if they arise in the process. :)
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Using SFML with math libraries? (GLM, Eigen...)
« Reply #5 on: March 10, 2017, 09:51:11 pm »
I have successfully built and used SFML with 'slightly' modified Vector2.hpp file:
#ifndef SFML_VECTOR2_HPP
#define SFML_VECTOR2_HPP
#include <glm/vec2.hpp>

namespace sf
{
        template<typename T>
        using Vector2 = glm::tvec2<T>;

        // Define the most common types
        typedef glm::tvec2<int>          Vector2i;
        typedef glm::tvec2<unsigned int> Vector2u;
        typedef glm::tvec2<float>        Vector2f;
} // namespace sf

#endif // SFML_VECTOR2_HPP
It is pretty small modification to SFML's code and let's you do all you want without additional conversions, especially implicit ones (which areā€¦ bad af?). I thought this may be way to go for you.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using SFML with math libraries? (GLM, Eigen...)
« Reply #6 on: March 10, 2017, 10:41:29 pm »
Quote
It is pretty small modification to SFML's code
Pretty small but it is one anyway, which prevents your code from working with official (and all other) SFML builds.
Laurent Gomila - SFML developer

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Using SFML with math libraries? (GLM, Eigen...)
« Reply #7 on: March 11, 2017, 10:05:09 am »
Quote
It is pretty small modification to SFML's code
Pretty small but it is one anyway, which prevents your code from working with official (and all other) SFML builds.
True dat. After all it's just a suggestion, @OP can decide what's the best approach for him.

Btw if I was forced not to modify SFML code, I'd use some proxy class that would inherit from sf::Vector2<T>, but also could be converted to and set by glm::tvec2<T>.

template<class T>
class ProxyVec
: public sf::Vector2<T> {
public:
        using sf::Vector2<T>::Vector2;

        glm::tvec2<T> glm() {
                return {this->x, this->y};
        }
        ProxyVec& operator=(glm::tvec2<T> v) {
                this->x = v.x;
                this->y = v.y;

                return *this;
        }
};

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Using SFML with math libraries? (GLM, Eigen...)
« Reply #8 on: March 11, 2017, 11:44:46 am »
If conversions between GLM and SFML vectors appear throughout your entire code base, your problem lies somewhere else.

Most probably, you have not properly separated the game logics from your rendering/graphics module. SFML is there to visualize the contents on your screen, not to store your game objects' positions. It can of course be handy to use sf::Vector2 also in your game logic, if the class is already there, but it's definitely not a requirement.

With a clean separation, you would update your graphics according to the state of your game logic -- ideally one layer in your application would be responsible for the interfacing of both. In this layer, you can then apply arbitrary transforms (e.g. units (m) to pixels, GLM to SFML vectors, radians to degrees, etc.). This would not only solve the problem described in this thread, but also allows you to treat both more independently, and modify one without affecting the other.

Also, as a side note: if not the entire flexibility of GLM is needed, it may be enough to enhance SFML's vector with some vector algebra functionality. One example would be my library Thor.Vectors.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: