SFML community forums

Help => Graphics => Topic started by: Athomield on December 28, 2014, 03:38:23 pm

Title: Position relative to a sprite
Post by: Athomield 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)
Title: Re: Position relative to a sprite
Post by: Jesper Juhl on December 28, 2014, 03:45:30 pm
Read the documentation. It sounds like you want sf::Sprite::move.
http://www.sfml-dev.org/documentation/2.2/classsf_1_1Transformable.php#ab9ca691522f6ddc1a40406849b87c469
Title: Re: Position relative to a sprite
Post by: Athomield 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.
Title: Re: Position relative to a sprite
Post by: Ixrec 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));
Title: Re: Position relative to a sprite
Post by: Laurent 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.
Title: Re: Position relative to a sprite
Post by: Athomield 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 !
Title: Re: Position relative to a sprite
Post by: Ixrec on December 28, 2014, 08:04:25 pm
Since you said "instead of ... screen" there's a slight chance mapPixelToCoords and/or mapCoordsToPixel (http://www.sfml-dev.org/tutorials/2.2/graphics-view.php#coordinates-conversions) are also relevant.
Title: Re: Position relative to a sprite
Post by: Athomield 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 ?
Title: Re: Position relative to a sprite
Post by: Laurent 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.
Title: Re: Position relative to a sprite
Post by: SpeCter 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
Title: Re: Position relative to a sprite
Post by: Athomield 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 :)