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

Author Topic: Window resize and world coordinates  (Read 3527 times)

0 Members and 1 Guest are viewing this topic.

franks

  • Newbie
  • *
  • Posts: 4
    • View Profile
Window resize and world coordinates
« on: March 22, 2017, 06:07:27 pm »
Hello,

I'm trying to develop a basic 2D platformer game with SFML2. I have a player that can move right, left and jump. But when I resize the window the movements are slow. I did some research and I found that I need to convert the world unit to pixel with mapCoordsToPixel ...

Do I really need to convert every coordinates of my entities ? Is there a camera like Libgdx and draw on this instead of the screen ? What is the best pratice to do that ?

nyenye

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Window resize and world coordinates
« Reply #1 on: March 22, 2017, 06:24:29 pm »
Hi! I'm very very new to SFML (just started today looking into it) but here are some thoughts: Why don't you scale up (or down) you rendered graphics? If you scale proportionally (aka, keeping the same aspect ratio) wouldn't it solve the problem?

franks

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Window resize and world coordinates
« Reply #2 on: March 22, 2017, 06:47:24 pm »
I can't scale my rendering region. I can just change the size of the rendering region. However I can use a view. But it's the same problem. When you use a custom view or when you resize the window I think I have to convert my coordinate.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Window resize and world coordinates
« Reply #3 on: March 22, 2017, 07:57:18 pm »
When you use a view that does not directly maps to the window's pixels anymore, you only have to call mapPixelToCoords to convert mouse coordinates to scene coordinates. If you only deal with scene coordinates (ie. your entities) you don't need to convert anything as long as everything stays in the same coordinate system.

Anyway, conversions between coordinate systems have nothing to do with movements being slower. Try to describe your problem precisely, instead of describing what you think the solution is.
Laurent Gomila - SFML developer

franks

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Window resize and world coordinates
« Reply #4 on: March 22, 2017, 08:45:49 pm »
Alright  :). This is what I done (I simplified). With or without the view I have the same problem. When I run my game the player has a certain speed. But when I scale up the window the player is slower. And when I scale down the window  the player is quicker. Why ?

#include <SFML/Graphics.hpp>

int main() {
   
   // Window and view
   sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML Test");
   sf::View view(sf::FloatRect(0, 0, 720, 480));
   window.setView(view);
   
   // Events
   sf::Event event;
   
   // Texture and sprite
   sf::Texture texture;
   texture.loadFromFile("../res/player.png");
   sf::Sprite player(texture);

   // Main loop
   while(window.isOpen()) {
     
      // Check events
      while(window.pollEvent(event))
         if(event.type == sf::Event::Closed)
            window.close();
     
     
      // Clear window
      window.clear(sf::Color::Black);
   
      // Movement
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
         player.move(-1, 0);
     
      if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
         player.move(1, 0);
   
      // Draw the plaxer
      window.draw(player);
   
      // End the current frame
      window.display();
   }

   return EXIT_SUCCESS;
}

 

PS : I added the view because I have a tiled map in my game.
« Last Edit: March 22, 2017, 10:04:13 pm by franks »

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Window resize and world coordinates
« Reply #5 on: March 22, 2017, 10:42:24 pm »
This is because your movement is based directly on your frame time. The larger your window the longer it takes to render a frame, and the slower the program updates. Take a look at the fix your timestep article for a good explanation. You're also moving in the middle of a clear/display cycle, when really you should be updating your logic outside of this.

franks

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Window resize and world coordinates
« Reply #6 on: March 24, 2017, 12:48:18 pm »
This is because your movement is based directly on your frame time. The larger your window the longer it takes to render a frame, and the slower the program updates. Take a look at the fix your timestep article for a good explanation. You're also moving in the middle of a clear/display cycle, when really you should be updating your logic outside of this.

Thank you, this works  ;D

Anyway, conversions between coordinate systems have nothing to do with movements being slower. Try to describe your problem precisely, instead of describing what you think the solution is.

Sorry, next time I will explain directly my problem