SFML converting local object coordinates to global coordinates

Started by
7 comments, last by gamervb 7 years, 7 months ago

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.

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Something like this?

http://www.sfml-dev.org/documentation/2.4.0/classsf_1_1RenderTarget.php#a46eb08f775dd1420d6207ea87dde6e54

The sf::renderWindow has a few helper functions for going from world coordinates to screen coordinates.

@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").

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".
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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).

Thanks for the help Khatharr and Alayric, cleared things up.

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.

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.

This topic is closed to new replies.

Advertisement