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;
}