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

Author Topic: Position Forward Up from Transform  (Read 3690 times)

0 Members and 1 Guest are viewing this topic.

tibor698

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Position Forward Up from Transform
« on: October 06, 2018, 10:51:34 pm »
I recently started using SFML and its transforms and I had to extend sf::Transform as it lacked some functionality that I needed.

This is an example of my code:
// some class
sf::Transform _transform;

sf::Vector2f Position2() const { return sf::Vector2f(_transform.getMatrix()[2], _transform.getMatrix()[6]); }
sf::Vector2f Scale2() const { return sf::Vector2f(_transform.getMatrix()[0], _transform.getMatrix()[5]); }
void Position2(sf::Vector2f value)
{
        sf::Transform tmp(_transform.getMatrix()[0], _transform.getMatrix()[1], value.x,
                _transform.getMatrix()[4], _transform.getMatrix()[5], value.y,
                _transform.getMatrix()[8], _transform.getMatrix()[9], _transform.getMatrix()[10]);
        _transform = tmp;
}
sf::Vector2f Right2() const { return sf::Vector2f(_transform.getMatrix()[0], _transform.getMatrix()[1]); }
sf::Vector2f Up2() const { return sf::Vector2f(_transform.getMatrix()[4], _transform.getMatrix()[5]); }
 

Could something like this be implemented into sf::Transform? For e.g. there is a method sf::Transform::translate() which translates based of current values but there is no functionality to set position directly.

tomvidm

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Position Forward Up from Transform
« Reply #1 on: October 09, 2018, 12:09:07 am »
The transform you retrieve from a transformable is a composition of several transforms. If I understand you correctly, what you want is to easily extract information such as position, scaling and rotation from a matrix. However, in general, there is no one to one correspondence between the transformation matrix and any combination of position, scaling and rotation.

If you look at the translation matrix itself, then yes, the position is perfectly retrievable. However, composite matrices (such as those constructed by sf::Transformable) are more complex. The matrix entries in the composite matrix corresponding to those of a translation matrix are very different. The composite matrices are a composition of translation (for setting origin), rotation, scaling and translation (for positioning), so the matrix is a lot more "mixed" up, than you would like. I suggest trying it out on paper so you can see for yourself.

In short: In general, you cannot extract information from a transformation matrix, as composition of transforms are not commutative.

EDIT: This is what the Transformable class is for. It precomputes the matrix and the inverse matrix for you, and it already gives you methods such as getPosition and getScale  :)

EDIT 2: Another point is that the transformations are affine. Let's say a point (1, 0) is transformed by some matrix T to produce the point (2, 0). What did the transformation do? Did it translate the vector, or did it scale the vector? Affine 2D transformations carries an extra column for the translations, so the transformation can also be seen as projecting 3 dimensional vector onto 2D space. This results in a loss of information.
« Last Edit: October 09, 2018, 02:41:12 pm by tomvidm »

tibor698

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Position Forward Up from Transform
« Reply #2 on: October 10, 2018, 07:38:46 pm »
If you look at the translation matrix itself, then yes, the position is perfectly retrievable.

It is if the object is inheriting a sf:Transformable class. The thing is that I only inherited from sf::Drawable and used sf::Transform inside this class. So with this there is no getPosition() from sf:Transform but instead I had to create the code above.

 

anything