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

Author Topic: Delay in SFML  (Read 5257 times)

0 Members and 1 Guest are viewing this topic.

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Delay in SFML
« on: March 29, 2014, 12:07:49 pm »
This is my code:
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Paupav ball game");
    sf::Clock clock;
    sf::Clock clock2;
    sf::Clock clock3;
    sf::Time borderBounce = clock3.getElapsedTime();
    bool returning = false;
    bool userControls = true;
    float positionx = 10;
    float positiony = 16;
        sf::CircleShape shape(30, 999);
        sf::Texture texture, texture2;

        if(!texture.loadFromFile("texture.jpg"))
        {
            std::cout << "Could not load ball texture" << std::endl;
        }
        shape.setTexture(&texture);
        //floor
        sf::RectangleShape floor(sf::Vector2f(800, 60));

        if(!texture2.loadFromFile("brick.png"))
        {
            std::cout << "Could not load floor texture" << std::endl;
        }
        floor.setTexture(&texture2);
        floor.setPosition(0, 540);

    while(window.isOpen())
    {

        sf::Time time = clock.getElapsedTime();
        sf::Time physic = clock2.getElapsedTime();
        shape.setPosition(positionx, positiony);
        if(!returning)
        positiony = time.asMicroseconds() / 2319 + physic.asMilliseconds()/ 30;


       if(positiony > 480)
            returning = true;

        if(returning )
        {
            positiony -=8;
            clock.restart();
        }
        if(positiony < 15 + physic.asMilliseconds()/ 30)
        returning = false;


        if(physic.asMilliseconds() > 17000.00)
        {
            clock.restart();
            clock2.restart();
        }

        sf::Event event;
        while(window.pollEvent(event))
        {

            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
            }

        }

        if(userControls)
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                positionx += 10;
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                positionx -= 10;

        }
        if(positionx < 9)
        {
            while(positionx < 40)
            positionx += 0.001;
        }




    std::cout << "time: " << borderBounce.asMilliseconds() << std::endl;

    window.clear(sf::Color::White);
    window.draw(shape);
    window.draw(floor);
    window.display();
    }
}
 

I know it's a bit messy, those booleans aren't needed etc. I made it using only SFML documentation.  So I want to create that when ball hits borders it bounces back. I wanted to do it like this:
while(positionx < 40)
{increase positionx
delay}
Any suggestions, except to burn this code. The best way of doing this that I can remember is to set positionx every few milli seconds
« Last Edit: March 30, 2014, 11:23:15 pm by paupav »

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Hello, I'm new to SFML
« Reply #1 on: March 30, 2014, 10:45:22 pm »
Please make a complete and minimal example code and use a more descriptive title if you want more people to help. There are a couple of things to note here. In any way, I think the tutorial will still help you a lot.

For the bouncing: give your object a velocity, then you can simply add another velocity or reverse the velocity when colliding.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Delay in SFML
« Reply #2 on: March 31, 2014, 08:22:00 pm »
I'm just guessing here, but I think that what you are looking for is:
For the "delay/timekeeping"; a fixed time step, independent of frame rate - read this:
http://gafferongames.com/game-physics/fix-your-timestep/
For the bounce; some proper physics based collision response - read this:
http://www.gamasutra.com/view/feature/131424/pool_hall_lessons_fast_accurate_.php?print=1