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

Author Topic: Using sf::Transform to move and rotate one point relative to another?  (Read 2505 times)

0 Members and 1 Guest are viewing this topic.

jabza

  • Newbie
  • *
  • Posts: 22
    • View Profile
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!
« Last Edit: January 18, 2013, 12:03:02 am by jabza »

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: Using sf::Transform to move and rotate one point relative to another?
« Reply #1 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
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

jabza

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Using sf::Transform to move and rotate one point relative to another?
« Reply #2 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
AW: Using sf::Transform to move and rotate one point relative to another?
« Reply #3 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.
« Last Edit: January 20, 2013, 09:46:54 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using sf::Transform to move and rotate one point relative to another?
« Reply #4 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.
Laurent Gomila - SFML developer

 

anything