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

Author Topic: deltaTime changes drastically when window is resized ?  (Read 1832 times)

0 Members and 1 Guest are viewing this topic.

drinkadriu

  • Newbie
  • *
  • Posts: 5
    • View Profile
deltaTime changes drastically when window is resized ?
« on: January 19, 2016, 10:36:45 pm »
I've been working in a platformer and until now everything is working fine.I wanted the game to be something new so i added a feature where you can resize the window to resize the camera, here are some screenshots:
http://imgur.com/gallery/DAHRV/new
but the problem is that when i resize the window deltaTime changes drastically, and i don't want that because it changes the gameplay(like the jumpheight, speed, etc), i don't know how to fix this and here is the source code:
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <Windows.h>

int main()
{
        sf::RenderWindow window(sf::VideoMode(600, 600), "Platformer !");
        sf::Clock deltaClock;
        sf::View view(window.getDefaultView());
        float deltaTime;

        // Start

        float Gravity = 0;
        float NormalGravity = 2.6;
        float SlowGravity = 0.87;
        float DownGravity = 25;
        float JumpHeight = -1.0;
        float Acceleration = 1;
        bool IsGrounded = false;
        bool Walltouching = false;
        bool WalltouchingRight = false;
        bool WalltouchingLeft = false;
        sf::Vector2f Velocity = sf::Vector2f(0, 0);
        sf::Vector2f Position = sf::Vector2f(0, 0);
        sf::Vector2f Size = sf::Vector2f(50, 50);
        float Speed = 800;
        float Horizontal = 0;
        float Vertical = 0;
        sf::RectangleShape Player;
        Player.setSize(Size);
        Player.setFillColor(sf::Color(150, 40, 70, 200));
        sf::RectangleShape BLOK;
        BLOK.setSize(Size + Size);
        BLOK.setFillColor(sf::Color(250, 130, 40, 150));
        BLOK.setPosition(sf::Vector2f(500, 900));

        // Game Loop
        while (window.isOpen())
        {
                sf::Event event;
                sf::Time dt = deltaClock.restart();
                deltaTime = dt.asSeconds();

                while (window.pollEvent(event))  // Events
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::Resized:
                                window.setView(view = sf::View(sf::FloatRect(0.f, 0.f,
                                        static_cast<float>(window.getSize().x),
                                        static_cast<float>(window.getSize().y))));
                                break;
                        /*case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::Space){
                                        //if (IsGrounded){
                                                Velocity.y = JumpHeight;
                                                printf("JUMPIN");
                                        //}
                                }*/

                                //break;
                        }
                }

                        // Update

                        // Horizontal Input

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
                                Horizontal = -1;
                                if (Acceleration <= 2)
                                        Acceleration += 0.4*deltaTime;
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
                                Horizontal = 1;
                                if (Acceleration <= 2)
                                        Acceleration += 0.*deltaTime;
                        }
                        else {
                                if (Acceleration >= 0.01)
                                        Acceleration = 1;
                                if (Horizontal > 0)
                                        Horizontal -= 2*deltaTime;
                                else if (Horizontal < 0)
                                        Horizontal += 2*deltaTime;
                                else
                                        Horizontal = 0;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
                                Gravity = DownGravity;
                        }
                        else
                                Gravity = NormalGravity;

                        // Wall Collision

                        if (Position.x <= 0){
                                //IsGrounded = true;
                                Position.x = 0;
                                Walltouching = true;
                                WalltouchingLeft = true;
                                Gravity = SlowGravity;
                        }
                        else if (Position.x >= window.getSize().x-50){
                                //IsGrounded = true;
                                Position.x = window.getSize().x - 50;
                                Walltouching = true;
                                WalltouchingRight = true;
                                Gravity = SlowGravity;
                        }
                        else {
                                Walltouching = false;
                                WalltouchingRight = false;
                                WalltouchingLeft = false;
                        }

                        // COLLISION

                        if (Position.x < BLOK.getPosition().x + 100 &&
                                Position.x + 50 > BLOK.getPosition().x &&
                                Position.y < BLOK.getPosition().y + 100 &&
                                50 + Position.y > BLOK.getPosition().y){
                                printf("\nTOUCHING !");
                        }
                        else{
                                //system("cls");
                        }

                        // Gravity and down collision

                        if (Position.y >= window.getSize().y - 50){
                                IsGrounded = true;
                                Velocity.y = 0;
                        }
                        else if (Walltouching){
                                IsGrounded = true;
                                Velocity.y += Gravity*deltaTime;
                        }
                        else {
                                IsGrounded = false;
                                Velocity.y += Gravity*deltaTime;
                        }

                        // Jumping Input
                       
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
                                if (IsGrounded){
                                        Velocity.y = JumpHeight;
                                }
                        }
                       
                        // Movement Calculations

                        //14JumpHeight = -(window.getSize().x / window.getSize().x / 1.5);
                        Velocity.x = Horizontal*Acceleration*Speed*deltaTime;
                        Player.setPosition(Position);
                        Position += Velocity;
                        printf("\nPOSITION Y:     %f", Position.y);

                        // Draw
                        window.clear();
                        window.draw(Player);
                        window.draw(BLOK);
                        window.display();
                }

                return 0;
        }

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: deltaTime changes drastically when window is resized ?
« Reply #1 on: January 20, 2016, 12:45:58 pm »
I wanted the game to be something new so i added a feature where you can resize the window to resize the camera

case sf::Event::Resized:
                                window.setView(view = sf::View(sf::FloatRect(0.f, 0.f,
                                        static_cast<float>(window.getSize().x),
                                        static_cast<float>(window.getSize().y))));
                                break;
 

This is a very naive approach for what you want to achieve. The problem is that if you have a 640x480 window and then resize to a 1920x1080 window you will see x3 more horizontally and x2.25 more vertically. So if your character fits "nicely" in one resolution it will be either way too big or way too small on another.

A common approach is to have a fixed vertical or horizontal size and have the other axis vary depending on the aspect ratio.

For a fixed height and variable width:

float height = 10.0f; //fixed height
float width = height * ((float)window.getSize().x / (float)window.getSize().y); //13.3 on 640x480 & 17.7 on 1920x1080
 

but the problem is that when i resize the window deltaTime changes drastically, and i don't want that because it changes the gameplay(like the jumpheight, speed, etc)

You are not being very clear as to what the problem is (also, don't post your entire code, narrow it down to the individual piece of code that's causing problems and then if you still can't figure it out ask).

One thing to note is that if you drag the window and your application tries to poll for the event it will freeze the thread until the dragging has stopped, which is turn will give a sudden increase in your delta. Not sure if this is related to the problem you're having, though.

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: deltaTime changes drastically when window is resized ?
« Reply #2 on: January 20, 2016, 01:53:48 pm »
His main problem is, that the timer restarts after the window has been resized.
The program stops while resizing.
Normally the time gets reset at lets say for example every 16ms so his deltatime is always somewhere around 16ms.
The moment he begins resizing/moving his window, event handling etc. "stops".
If the time he needed to resize would be one second for example, the time would now be at 1000ms.
That means everything he moves would move more than 60 times as far as it is supposed to.

I would suggest reading this:
http://gafferongames.com/game-physics/fix-your-timestep/


Hapax

  • Hero Member
  • *****
  • Posts: 3353
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: deltaTime changes drastically when window is resized ?
« Reply #3 on: January 20, 2016, 08:56:08 pm »
The problem that is not clearly described might also be that when the window is larger, the frame rate drops and the delta time is therefore less.
When delta time changes, the physics/logic changes too so variable timestep can cause reliability/stability/replayability issues.

However, the solution is the same and is, as posted above, to read this article:
http://gafferongames.com/game-physics/fix-your-timestep/

Or, you could use my small timing library and then do this simply like this;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything