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

Author Topic: Position relative to a sprite  (Read 3976 times)

0 Members and 1 Guest are viewing this topic.

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Position relative to a sprite
« on: December 28, 2014, 03:38:23 pm »
Hi how do i get and use a vector2 that has coordinates relative to a sprite ?
say setPosition(vector2f PosRelativeToSprite)
« Last Edit: December 28, 2014, 03:41:35 pm by Athomield »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Position relative to a sprite
« Reply #1 on: December 28, 2014, 03:45:30 pm »
« Last Edit: December 28, 2014, 03:49:00 pm by Jesper Juhl »

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Position relative to a sprite
« Reply #2 on: December 28, 2014, 03:50:10 pm »
I didn't get it, how you're gonna get it when you're the one who will give the offset parameter.
 the example that I provided is not what i exactly needed,I just want to get a position relative to a sprite in a vector2 that ignores rotations.
To be more specific, that position uses the sprite as coordinate system.
« Last Edit: December 28, 2014, 03:59:45 pm by Athomield »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Position relative to a sprite
« Reply #3 on: December 28, 2014, 05:21:59 pm »
It's very unclear what you're trying to ask.

Are you saying you want sprite1's position to be, say, 10 pixels to the right of where it is now?  If so, like Jesper said, there's a move() function for that.

Are you saying you want to set sprite1's position to be, say, 10 pixels to the right of and 20 pixels above sprite2?  If so, you can already do that quite easily:
sprite1.setPosition(sprite2.getPosition() + sf::Vector2f(10, 20));
« Last Edit: December 28, 2014, 05:23:46 pm by Ixrec »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Position relative to a sprite
« Reply #4 on: December 28, 2014, 06:51:37 pm »
sprite.getTransform() gives you the transformation that can transform from local to global coordinates (or the opposite if you use its inverse). This may help you achieve what you want.
Laurent Gomila - SFML developer

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Position relative to a sprite
« Reply #5 on: December 28, 2014, 07:23:14 pm »
@Ixrec what i wanted is to use coordinates of a sprite instead of coordinates (not coordinated) of screen
@Laurent Thanks ! I'll experiment with that !
« Last Edit: December 28, 2014, 08:14:22 pm by Athomield »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Position relative to a sprite
« Reply #6 on: December 28, 2014, 08:04:25 pm »
Since you said "instead of ... screen" there's a slight chance mapPixelToCoords and/or mapCoordsToPixel are also relevant.

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Position relative to a sprite
« Reply #7 on: December 28, 2014, 08:16:34 pm »
@Ixrec I'm afraid that only applies to custom view but still i'll experiment with it
@Laurent 
sf::Vector2f newPoint = sprite.getTransform().transformPoint(pointToTransform);

is this what i'm looking for ? And does it get affected by rotation ? scale ?
« Last Edit: December 28, 2014, 08:27:30 pm by Athomield »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Position relative to a sprite
« Reply #8 on: December 28, 2014, 08:58:49 pm »
Quote
is this what i'm looking for ?
I don't know, your explanations are still not very clear.

What it does is the following: pointToTransform is relative to the (origin of the) sprite, and the resulting newPoint is the same but transformed to global coordinates. In other words, you can draw something directly at this position, it will always follow the position / rotation / scale of the sprite. Is this what you want to do?

Quote
And does it get affected by rotation ? scale ?
Yes of course, it takes in account everything.
« Last Edit: December 28, 2014, 09:01:44 pm by Laurent »
Laurent Gomila - SFML developer

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Position relative to a sprite
« Reply #9 on: December 28, 2014, 10:40:15 pm »
My guess would be, that he wants to move something along its own local axis. So if you move it (0|10) when it is unrotated it moves up but if you rotate it 90° , it will move to the right instead, if you still move it by (0|10) because from the sprites view, up is to the left in world view

Athomield

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Position relative to a sprite
« Reply #10 on: December 29, 2014, 06:03:53 pm »
@Cepcter I honestly didn't get you, but still thank you for your help :)
@Laurent I was kinda confused me too, that I didn't know what I wanted. But Thanks to you the function gave me seems to be what I needed :)