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

Author Topic: Vertical falling ball. need help.  (Read 3697 times)

0 Members and 1 Guest are viewing this topic.

meistertigran

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Vertical falling ball. need help.
« on: January 02, 2013, 05:15:50 pm »
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;
}
 
« Last Edit: January 02, 2013, 05:45:28 pm by meistertigran »

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Vertical falling ball. need help.
« Reply #1 on: January 02, 2013, 05:26:20 pm »
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.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

meistertigran

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Vertical falling ball. need help.
« Reply #2 on: January 02, 2013, 05:32:28 pm »
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.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Vertical falling ball. need help.
« Reply #3 on: January 02, 2013, 05:44:54 pm »
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

Cite anything that's not been specified already.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

meistertigran

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Vertical falling ball. need help.
« Reply #4 on: January 02, 2013, 05:48:04 pm »
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.

meistertigran

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Vertical falling ball. need help.
« Reply #5 on: January 02, 2013, 05:52:32 pm »
I think it is better for me to install SFML 2.0 and write the same code for it.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Vertical falling ball. need help.
« Reply #6 on: January 02, 2013, 06:17:34 pm »
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.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

meistertigran

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Vertical falling ball. need help.
« Reply #7 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;
}