-1
\$\begingroup\$

Why do the coordinates x and y in the 2D game both floating-point numbers, not integers? For example, the SFML setPosition function requires two floating-point values as arguments. However, the setup screen mode or video mode requires two integers. The left hand didn't talk to the right hand?

\$\endgroup\$
1
  • \$\begingroup\$ Keep in mind that SFML has also the concept of View::zoom(float). This lets you resize what is displayed on the screen. If you were only allowed to use integers, that would give very odd results. \$\endgroup\$ Commented Jul 26, 2022 at 16:25

2 Answers 2

2
\$\begingroup\$

Integers are used for initializing the screen because that's how the hardware (screen) treats them. Even if you're doing something like subpixel rendering, fundamentally either you have a pixel or you don't.

World space positioning models real life positioning. Things can exist at non integer locations. Floating point numbers aren't a perfect representation, (because they are an approximation of real numbers), but they work reasonably well for this.

Eventually things in world space get rendered & rasterized to screen space:

taken from https://www.arxiv-vanity.com/papers/1901.05567/
(image source)

But prematurely casting your coordinates to integers would result in a loss of precision & introduce rendering errors. And again, the high level API (you your case SFML) is using what the lower level API (OpenGL) and hardware (video card) supports - floating point.

You can treat world space & screen space as the same. Some 2D games do that in part because it simplifies some of the math & reduces what you have to keep track of. But that simplification isn't necessarily true in general for all games or how things are modelled in general.

The SFML tutorial on Using sf::View touches on the differences between using two different coordinate systems & how to use mapPixelToCoords(…) and mapCoordsToPixel(…) to convert between them.

\$\endgroup\$
1
\$\begingroup\$

Because our game loop is 60 fps or even higher, it would be nice if we could make a sprite moving a little bit, a fraction of pixels for each frame rate. A fraction of the pixels, a floating-point value, that we could add or subtract from the sprite's current position makes it move smoothly and nicely in each frame.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Um... Is this answer answering the question, or as a supplement to the question? You can edit the question directly. World space and screen space are not the same, world space uses floating point coordinates, screen space uses pixel coordinates, the conversion relationship between them depends on the camera, in 2d games, it depends on the width of the orthographic camera. \$\endgroup\$ Commented Jul 26, 2022 at 9:10
  • \$\begingroup\$ What sources or books did you read that from? Could you share them with me? I've just touched on game programming. \$\endgroup\$ Commented Jul 26, 2022 at 13:31
  • 2
    \$\begingroup\$ @ĐạtPhạm Check how SFML implements its API: it's over OpenGL, which also relies partially on the OS API. OS windows are using integers because windows can't use half pixels, that's how the OS is designed. Within the OpenGL API, though, this limitation is no longer there, so we can use whatever coordinates we want. \$\endgroup\$ Commented Jul 26, 2022 at 14:57
  • 1
    \$\begingroup\$ @ĐạtPhạm To some extent this is general knowledge. As another answer said, some high-level APIs(such as SFML) for 2D games simplify this process, they adjust the scaling so that 1 unit of world coordinate measurement is exactly equal to 1 pixel. Maybe a book focused on SFML doesn't mention this concept to reduce complexity, which is beneficial. Some general game programming books, or books on low-level APIs (such as OpenGL) will definitely mention this concept. \$\endgroup\$ Commented Jul 26, 2022 at 14:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.