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

Author Topic: Tracking a point on a sprite after rotation.  (Read 2242 times)

0 Members and 1 Guest are viewing this topic.

aaranaf

  • Newbie
  • *
  • Posts: 4
    • View Profile
Tracking a point on a sprite after rotation.
« on: July 18, 2013, 02:03:26 am »
Hi I'm a bit stuck on how I can track a point on a sprite after rotation is applied.

Let's say I have a point that I've saved away that I want my bullets to spawn at, typically the end of a rifle for an example. But after I execute the rotation code:
        sf::Vector2f curPos = GetSprite().getPosition();
        sf::Vector2i position = sf::Mouse::getPosition(window) ;

        const float PI = 3.14159265;

        float dX = curPos.x - position.x;
        float dY = curPos.y - position.y;

        rotation = (atan2(dY, dX)) * 180 / PI;

        GetSprite().setRotation(rotation - 90);
 
I want that point X to stay with the sprite so I can use it as an anchor for my bullets. I know there's and formula for it somewhere but I have not yet found it. Can anyone point me in the right direction?

Anonymouseable

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Tracking a point on a sprite after rotation.
« Reply #1 on: July 18, 2013, 02:13:20 am »
Which one? CurPos?

aaranaf

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Tracking a point on a sprite after rotation.
« Reply #2 on: July 18, 2013, 02:34:27 am »
Maybe I should clarify: I have a Point X witch represent the anchor when the rotation is currently at 0 what I want to do is after the rotation have been done I want that anchor to be placed on the same part of the image so newly created bullet actually comes out from the right part of the sprite instead of staying at the last spot witch no longer is the "tip of the gun" so to speak.

    sf::Vector2f curPos = GetSprite().getPosition();
    sf::Vector2i position = sf::Mouse::getPosition(window) ;
    sf::vector2f Anchor = GetSprite().getPosistion();

    const float PI = 3.14159265;

    float dX = curPos.x - position.x;
    float dY = curPos.y - position.y;

    rotation = (atan2(dY, dX)) * 180 / PI;

    GetSprite().setRotation(rotation - 90);
    Anchor.x = //Here is where i need the new location
    Anchor.y = //Here is where i need the new location

 

I put a simple ms paint picture as an attachment, maybe it can explain on what I mean.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Tracking a point on a sprite after rotation.
« Reply #3 on: July 18, 2013, 03:32:38 am »
Something along the lines of this should do the trick for you.

    sf::Vector2f curPos = GetSprite().getPosition();
    sf::Vector2i position = sf::Mouse::getPosition(window) ;
    sf::vector2f Anchor = GetSprite().getPosistion();

    const float PI = 3.14159265;

    float dX = curPos.x - position.x;
    float dY = curPos.y - position.y;

    rotation = (atan2(dY, dX)) * 180 / PI;

    GetSprite().setRotation(rotation - 90);
    Anchor.x = std::cos(rotation / 180 * PI) * [length from origin to anchor]
    Anchor.y = std::sin(rotation / 180 * PI) * [length from origin to anchor]

 
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

aaranaf

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Tracking a point on a sprite after rotation.
« Reply #4 on: July 18, 2013, 05:34:11 am »
Got it to work!
Thanks zsbzsb.