-
I'm trying to create a Vertical falling ball, without any move controls or something else. I have written a code that i think shall work, but it doesn't work : :o Please look at the source code. Or if someone an oter code, pleas share it with me :D.
Sorry for my bad english.
#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;
int s = 595;
int y;
int i=1;
bool move = false;
int vy;
int v = 1;
float t = 1;
sf::Event Event;
while(running == true)
{
if(i == 1)
{
ball.Move(0,g*Clock.GetElapsedTime());
}
y = ball.GetPosition().y;
std::cout<<"y= "<<y<<"\n";
std::cout<<"s= "<<s<<"\n";
if(y == s)
{
v=g*Clock.GetElapsedTime();
move = true;
i++;
Clock.Reset();
}
if(move == true && i>1)
{
vy=-(v-g*Clock.GetElapsedTime());
if(vy == 0)
{
Clock.Reset();
move = false;
}
ball.Move(0,vy);
}
if(i>1 && move == false)
{
ball.Move(0,g*(Clock.GetElapsedTime()));
}
while(Game.GetEvent(Event))
{
}
Game.Clear();
Game.Draw(ball);
Game.Display();
}
return 0;
}
-
What exactly are you trying to control with all your bunch of conditionals? We could try and analyze each, but it's a lot faster if you just tell us.
Also start using SFML 2, it has less bugs and many other features that make it far more worth it.
-
Ok. The first if(y == s) collision with the window, if(move == true && i>1) to change the direction of the moving and other parameters, if(i>1 && move == false) the end of the up move and again falling down.
-
What appears on the screen? Is it crashing or is the ball just not moving how you want it to?
http://en.sfml-dev.org/forums/index.php?topic=5559.0 (http://en.sfml-dev.org/forums/index.php?topic=5559.0)
Cite anything that's not been specified already.
-
It's falling down, and then at the collission with the window it's stoping and after some time it's moving up. You can try the code to see.
-
I think it is better for me to install SFML 2.0 and write the same code for it.
-
Just ported it to SFML 2.0 and tested it, the problem is obviously in the window collision as it just goes through, I'll fix it later as I have no time in my hands right now, but here's the ported version:
int main()
{
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "Ball ... ");
sf::Clock Clock;
sf::Texture bg;
bg.loadFromFile("C:/Users/Carlos/Desktop/Sprites/Flame Shot Basic 1.png");
sf::Sprite ball;
ball.setOrigin(60,120);
ball.setTexture(bg);
ball.setPosition(300,120);
bool running = true;
float g = 0.6;
int s = 595;
int y;
int i=1;
bool move = false;
int vy;
int v = 1;
float t = 1;
sf::Event Event;
while(running == true)
{
if(i == 1)
{
ball.move(0,g*Clock.getElapsedTime().asSeconds());
}
y = ball.getPosition().y;
std::cout<<"y= "<<y<<"\n";
std::cout<<"s= "<<s<<"\n";
if(y == s)
{
v = g*Clock.getElapsedTime().asSeconds();
move = true;
i++;
Clock.restart();
}
if(move == true && i>1)
{
vy -= (v - g*Clock.getElapsedTime().asSeconds());
if(vy == 0)
{
Clock.restart();
move = false;
}
ball.move(0,vy);
}
if(i>1 && move == false)
{
ball.move(0,g*(Clock.getElapsedTime().asSeconds()));
}
while(Game.pollEvent(Event))
{
}
Game.clear();
Game.draw(ball);
Game.display();
}
return 0;
}
You could easily simplify the code and instead of having many conditionals you just check if the sprite has reached either top or bottom of the window's y and have it move in a determined speed (positive or negative accordingly), instead of making mangles with clocks for something as simple as that.
-
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;
}