1
Graphics / Converting from Cartesian coordinate system to SFML.
« on: August 28, 2011, 04:00:03 pm »Quote from: "Nexus"
Code: [Select]Pos = Pos + a + b - a;
Code: [Select]Pos += b;
You know that +a and -a sum up to zero?
You should write:Code: [Select]sf::Vector2f BaseEntity::GameCoordsToSFML(sf::Vector2f Pos)
{
Pos.y = -Pos.y;
Pos += sf::Vector2f(pRender->GetWidth() / 2.f, pRender->GetHeight() / 2.f);
return Pos;
}
Although I find it strange that you also change the X coordinate, if you say that only the Y axis is inverted.
They aren't the same. Screen Center and View Center are different, because the view is moving around. Screen Center is the coordinate of the pixel in the middle of the screen.
You may have misunderstood what the function was meant to do, it is trying to convert game coordinates to which pixel that corresponds to on the screen. This position is dependent on where the view is looking so that is why that's included
Code: [Select]
sf::Vector2f BaseEntity::GameCoordsToSFML(sf::Vector2f Pos)
{
sf::Vector2<double> dPos;
dPos.x = Pos.x;
dPos.y = Pos.y;
dPos.y *= -1;
sf::Vector2<double> ScreenCentre = sf::Vector2<double>(pRender->GetWidth() / 2, pRender->GetHeight() / 2);
sf::Vector2<double> ViewCentre;
ViewCentre.x = pRender->GetView().GetCenter().x;
ViewCentre.y = pRender->GetView().GetCenter().y;
dPos = dPos + ViewCentre + (ScreenCentre - ViewCentre);
Pos.x = dPos.x;
Pos.y = dPos.y;
return Pos;
}
I know this has already been solved but I thought I would make a diagram of what situation the above code is useful for, in case anyone else needed it:
The green axis are SFML's coordinate system. The red box represents the pixels on the screen. The green axis is SFML's default origin and SFML's axis (note the Y axis runs in the opposite direction to the game coordinates)
The black axis are the game coordinates. The code takes a coordinate in game space and then converts that to the corresponding pixel on the screen. I hope it isn't too confusing as I can't imagine I am the only one who separated their game coordinates from SFML's.