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

Author Topic: Strange behavior  (Read 1749 times)

0 Members and 1 Guest are viewing this topic.

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Strange behavior
« on: June 21, 2012, 04:16:33 pm »
Strange behavior

I'm starting a simple projectile calculator and made this test for vertical falling object, I used one of the tutorials as starting point. The object will bounce on impact with the ground and there is a damping so it won't keep bouncing, but the problem is that if you shake the window of the program with your mouse, the object will bounce higher even though the code has nothing to do with that.

Try it, compile this code and run it then shake the window or even just move it and the box will fly out of it. Can anyone tell me what is happening here?



#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;



int main()
{


    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Projectile_Test");

    sf::Clock Clock;

    sf::Shape Polygon;
           Polygon.AddPoint(200, 200,  sf::Color(255, 0, 0),     sf::Color(0, 128, 128));
           Polygon.AddPoint(300, 200,   sf::Color(255, 85, 85),   sf::Color(0, 128, 128));
           Polygon.AddPoint(300, 300,  sf::Color(255, 170, 170), sf::Color(0, 128, 128));
           Polygon.AddPoint(200, 300,  sf::Color(255, 170, 170), sf::Color(0, 128, 128));

           Polygon.SetCenter(250,250);
           Polygon.Move(250,250);

    while (App.IsOpened())
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {

            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        float t = Clock.GetElapsedTime();

        float ElapsedTime = App.GetFrameTime();


        // Move the sprite
        if (App.GetInput().IsKeyDown(sf::Key::Left))  Polygon.Move(-1 , 0);
        if (App.GetInput().IsKeyDown(sf::Key::Right)) Polygon.Move( 300 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Up))    Polygon.Move(0, -300 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Down))  Polygon.Move(0,  300 * ElapsedTime);

        // Rotate the sprite
        if (App.GetInput().IsKeyDown(sf::Key::Add))      Polygon.Rotate(- 100 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Subtract)) Polygon.Rotate(+ 100 * ElapsedTime);


        // Gravitational acceleration value

        float acc=0.5*t;

        // Gravity
        Polygon.Move(0,  acc);



        cout<<t<<'\n';
        float y2 = Polygon.GetPosition().y;

        if (y2 > 550)
        {


                float s= acc ;
                Clock.Reset();

                t = Clock.GetElapsedTime();
                float acc=0.5*t;





                float m =5;

                while(m>0)

                {
                        t = Clock.GetElapsedTime();
                        acc=0.5*t;
                        Polygon.Move(0,  acc);
                        Polygon.Move(0,  -s/1.1);


                        //Polygon.Move(0,  -s/1000);
                        cout<<"loop"<<'\n';
                        cout << "distance="<< y2 <<'\n';
                        cout << "time="<< acc <<'\n';
                        cout << "s="<< s <<'\n';
                        m = s- acc;
                        App.Clear();

                        App.Draw(sf::Shape::Rectangle(200, 350, 600, 400, sf::Color::Green));


                // Display window contents on screen

                        App.Draw(Polygon);

                        App.Display();

                }

                Clock.Reset();
        }



        cout << y2 <<'\n';


        // Clear screen
        App.Clear();

        App.Draw(sf::Shape::Rectangle(200, 350, 600, 400, sf::Color::Green));


        // Display window contents on screen

        App.Draw(Polygon);

        App.Display();




    }

    return EXIT_SUCCESS;
}
 
« Last Edit: June 21, 2012, 04:19:57 pm by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10883
    • View Profile
    • development blog
    • Email
Re: Strange behavior
« Reply #1 on: June 21, 2012, 04:40:14 pm »
Since you're asking for help, the post would have fitted better into the help section...

Stop using SFML 1.6 and start sing SFML 2.0rc! (I should make a hotkey for that notice.)

I didn't look at your code in depth (since I've other stuff that I want to do), but since you're depending your movement on a clock, I can think of a problem that might occure. Whenever you grab your window the rendering part will stop, so will the position calculation, but the clock will keep running. Once you release the window again the clock has moved forward quite a bit and the next position update will move your object out of the screen.

Could this be the case?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Strange behavior
« Reply #2 on: June 21, 2012, 04:58:12 pm »
Yes, I believe that is correct. I changed the time from getting the time from the clock to a number increasing with each loop and that solved the problem. Thanks a lot :  )

Quote
Stop using SFML 1.6 and start sing SFML 2.0rc!

Will do. Hope the clock stops also with window movement in 2.0.
« Last Edit: June 21, 2012, 05:01:33 pm by K.F »