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

Author Topic: Converting from Cartesian coordinate system to SFML.  (Read 8880 times)

0 Members and 1 Guest are viewing this topic.

conman420

  • Newbie
  • *
  • Posts: 6
    • View Profile
Converting from Cartesian coordinate system to SFML.
« on: August 25, 2011, 12:17:02 am »
Hi, my project is using the standard Cartesian system for position (X-axis goes right, Y-Axis goes Up) whereas SFML has an inverted Y axis.

I am trying to convert my game coordinates to SFML coordinates for drawing, it was working fine when I wasn't using views but now I have implemented that my algorithm for converting has failed completely.

Here is my current code:
Code: [Select]

sf::Vector2f BaseEntity::GameCoordsToSFML(sf::Vector2f Pos)
{
Pos.y *= -1;
return pRender->GetView().GetCenter() + Pos;
}

sf::Vector2f BaseEntity::SFMLToGameCoords(sf::Vector2f Pos)
{
Pos = Pos - pRender->GetView().GetCenter();
Pos.y *= -1;
return Pos;
}



I think I am forgetting something when converting because as soon as GetCenter() moves from the centre of the window the whole thing breaks down and it returns weird results.

I was thinking it might involve RenderWindow::ConvertCoords() but I have no idea really I have never played with views before.

conman420

  • Newbie
  • *
  • Posts: 6
    • View Profile
Converting from Cartesian coordinate system to SFML.
« Reply #1 on: August 25, 2011, 12:53:23 am »
Have been bruteforcing it. Managed to find the answer:

Code: [Select]

sf::Vector2f BaseEntity::GameCoordsToSFML(sf::Vector2f Pos)
{
Pos.y *= -1;
Pos = Pos + pRender->GetView().GetCenter() + (sf::Vector2f(500,500) - pRender->GetView().GetCenter());
return Pos;
}


500,500 is the centre of the screen :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Converting from Cartesian coordinate system to SFML.
« Reply #2 on: August 25, 2011, 01:09:42 am »
Code: [Select]
Pos = Pos + a + b - a;
Code: [Select]
Pos += b;
You know that +a and -a sum up to zero? :D
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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

conman420

  • Newbie
  • *
  • Posts: 6
    • View Profile
Converting from Cartesian coordinate system to SFML.
« Reply #3 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? :D
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.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Converting from Cartesian coordinate system to SFML.
« Reply #4 on: August 28, 2011, 04:12:54 pm »
Well most people do seperate their logic from the graphics but we don't try and oppose the defined standard in computing. Why use the cartesian system when the one already in use works? Also you don't need to move the center of the game world to make it work like the cartesian coordinates. The only difference between them is the y-axis. The center always stays the same, you only need to move the view. Backwards half it's size to make origo be centered in the view.

Reading from the phone so might be getting this wrong but looks like you are doin what view does for you.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Converting from Cartesian coordinate system to SFML.
« Reply #5 on: August 28, 2011, 06:06:26 pm »
Quote from: "conman420"
They aren't the same. Screen Center and View Center are different
Yes, but in your code, you accidentally use the same expression twice, namely pRender->GetView().GetCenter().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ph0t0n

  • Newbie
  • *
  • Posts: 1
    • View Profile
Converting from Cartesian coordinate system to SFML.
« Reply #6 on: January 27, 2012, 02:16:09 am »
maybe i'm missing something, but I don't understand how ScreenCentre wouldn't cancel out.

dPos = dPos + ViewCentre + (ScreenCentre - ViewCentre);

i'm pretty sure that order of operation does not matter when all operations are addition and/or subtraction.

mathematically, the line above should simplify to:

 dPos += ScreenCentre;