SFML community forums

Help => Graphics => Topic started by: jabza on January 17, 2013, 01:18:30 am

Title: Using sf::Transform to move and rotate one point relative to another?
Post by: jabza on January 17, 2013, 01:18:30 am
Hi,

As I understand it, sf::Transform should be used for any transform operations that are not covered by the higher level sf::Transformable 'helper' class. My issue is as follows:

In my game I have a boat, on board the boat is a selection of 'crew members' which I would like to move freely around said boat. Effectively I want to 'glue' the crew member to the boat. Originally I did this by setting the crew's position to boat's position + an offset, however this felt wrong and although it did partly work obviously when the ship rotate the crew members do not. I've come to the conclusion that I must somehow 'merge' the boat and crew member's transform, but I'm not sure exactly how to go about it. Technically I want to set the origin of the crew member to that of the boat.

This is what I have so far:

void CrewMember::update()
{
    //Glue the crew member to the boat.
    sf::Transform boat = m_pVessel->m_decks[0].getTransform(); //The boat the crew member is on.
    sf::Transform crew = m_avatar.getTransform();

    crew = crew.combine(boat); //Merge the transforms?

    m_avatar.setPosition(crew.transformPoint(m_avatar.getPosition())); //Update the crew member to the correct position.

}

So far I've had no luck, any suggestions?
Thanks in advance!
Title: Re: Using sf::Transform to move and rotate one point relative to another?
Post by: Weeve on January 19, 2013, 09:49:07 pm
Try using a 2D Matrix, which is basically just two vectors, one for the y axis, and one for the x axis, and whenever you rotate the boat, rotate both vectors; then for each person, rotate them, and then translate them to the sum of your 2D coordinates that are multiplied by the Matrix's vectors

I'm used to writing as much code as I can by myself, unless its really really low level, so there is likely someone out there with a higher-level solution, but for now you could apply my solution to a wrapper of the sfml class ;D
Title: Re: Using sf::Transform to move and rotate one point relative to another?
Post by: jabza on January 20, 2013, 08:05:22 pm
Thanks for the suggestion, I'll give that way a shot. I usually end up solving problems that I later find out they have already been solved in the SFML library - but in this case I have no idea if there is a higher-level way of doing it.
Title: AW: Using sf::Transform to move and rotate one point relative to another?
Post by: eXpl0it3r on January 20, 2013, 08:21:35 pm
SFML is not a math library and the transformations are only there for convinience, if you want something more complex, you'll have to do it on your own. ;)
sf::Transform is iirc a 3x3 matrix with some operation on it.
Title: Re: Using sf::Transform to move and rotate one point relative to another?
Post by: Laurent on January 20, 2013, 08:34:14 pm
sf::Transform is a 3x3 matrix, so it can contain any 2D transformation, even non-linear ones (like translations). So you probably don't need something else.

For your initial problem, I don't know what the problem is (but your last line looks really wrong). You should try to explain it better; a complete and minimal code that reproduces the problem would be perfect.