1
General / Re: Vertical falling ball. need help.
« on: January 03, 2013, 02:30:31 pm »
I've written a shorter code then the previouse one and now it's works. Here it is, if someone is interested.
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "Ball ... ");
sf::Clock Clock;
sf::Image bg;
bg.LoadFromFile("ball.png");
sf::Sprite ball;
ball.SetCenter(60,120);
ball.SetImage(bg);
ball.SetPosition(300,120);
bool running = true;
float g = 0.6;
float tempV;
int y;
int i=2;
bool Collission = false;
float v;
float t;
sf::Event Event;
while(running == true)
{
t = Clock.GetElapsedTime();
if( Collission == false) {v=g*t;}
ball.Move(0,v);
y = ball.GetPosition().y;
if(y >= 600)
{
Collission = true;
tempV = v;
Clock.Reset();
ball.SetPosition(ball.GetPosition().x,599.9999);
i++;
}
t = Clock.GetElapsedTime();
if(Collission == true)
{
v = -((1*tempV)/2-g*t);
if(-v <= 0){Collission == false;}
}
Game.Clear();
Game.Draw(ball);
Game.Display();
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "Ball ... ");
sf::Clock Clock;
sf::Image bg;
bg.LoadFromFile("ball.png");
sf::Sprite ball;
ball.SetCenter(60,120);
ball.SetImage(bg);
ball.SetPosition(300,120);
bool running = true;
float g = 0.6;
float tempV;
int y;
int i=2;
bool Collission = false;
float v;
float t;
sf::Event Event;
while(running == true)
{
t = Clock.GetElapsedTime();
if( Collission == false) {v=g*t;}
ball.Move(0,v);
y = ball.GetPosition().y;
if(y >= 600)
{
Collission = true;
tempV = v;
Clock.Reset();
ball.SetPosition(ball.GetPosition().x,599.9999);
i++;
}
t = Clock.GetElapsedTime();
if(Collission == true)
{
v = -((1*tempV)/2-g*t);
if(-v <= 0){Collission == false;}
}
Game.Clear();
Game.Draw(ball);
Game.Display();
}
return 0;
}