• Advertisement
Sign in to follow this  

SFML converting local object coordinates to global coordinates

This topic is 584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How does one convert from local coordinates to global coordinates in SFML. I currently have the following

    triangle[0].position = sf::Vector2f(getPosition().x, getPosition().y);

    triangle[1].position = sf::Vector2f(getPosition().x - 40.0f, getPosition().y + 50.0f);

    triangle[2].position = sf::Vector2f(getPosition().x + 40.0f, getPosition().y + 50.0f);

when I call the following function

const sf::Vector2f Entity::getPositionVertex1() const

{

    return sf::Vector2f(triangle[1].position.x, triangle[1].position.y);

}

I get the position of the vertex relative to the local orgin of the object. I instead want the global position of the vertex relative to the world origin.

 

 

From the image below, I want v1 not relative to the blue X (which is the objects local origin), but to the orange dot that is the global origin. Is this possible in SFML? Some help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement

Add the position of the origin to the vertex offset. In your example the world space location of V1 is X + V1. sf::Vector<T> has math operators defines, so something like:

auto worldCoordV1 = triangle.position + triangle.vertices[1];

can work just fine.

 

Note that if you start involving rotation and scaling then you'll need to account for the whole transform instead of only the translation. What you're really doing here is applying the same transform to each vertex. Generally geometry instances are stored as a collection of vertices (the shape) paired with a transform (the position/scale/rotation). You don't need to have a specific vertex to serve as the origin of the model space.

Share this post


Link to post
Share on other sites

@Khantharr, thanks for the help.

 

Could you give a code snippet of what you're talking about when it comes to rotation and scaling? I'm not 100% what you mean ("account for the whole transform").

Share this post


Link to post
Share on other sites
If you're going to scale and rotate as well then you'll need to get familiar with transformation matrices. Most of what you'll find on the subject deals with 3D spaces, but the concept is the same either way. The transform is stored as an N+1 (N being dimensions) matrix that holds the information about the position, orientation, and scale of the object. You multiply each vertex by the matrix to place the object in the world space.

This sounds very complicated, and indeed the math takes a little while to learn, but SFML has the sf::Transform class that can help you work with it.

You'll still want to familiarize yourself with what's going on though, as these operations are the very core of what drives modern renderers, and they're also widely used in physics. There are a ton of resources for learning linear algebra around the internet, as well as for transforms in particular. If you have some time on your hands I recommend MFGD:

https://youtube.com/playlist?list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My

But you can also find videos from KhanAcademy and various others just by searching "introduction to transform matrices".

Share this post


Link to post
Share on other sites

If your entity is an sf::Shape (= is inheriting from an sf::Shape (or inheriting from sf::Transformable)), you can call getTransform to get the transform matrix of your object (it contains every transformations like origin, translation, rotation, ...) and then use transformPoint  to apply this transformation matrix to any point (any vertex of your shape).

Share this post


Link to post
Share on other sites

This is why matrices exists.

To transform from X system to Y system.

A transofrmation matrix is the most simple one, just build a4x4 transformation according to the view matrix and you have the coordinate. 

Share this post


Link to post
Share on other sites

The position of your objects would be on the frame of global coordinate system. If you get vertex coordinate related to that, just subtract the vertex's position from your object's position. But this is assuming you didn't use rotation or scaling. From the pictures you posted, you probably didn't.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement