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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - buymespam

Pages: [1]
1
General / Re: Game loop becomes unstable after ~5 seconds
« on: August 12, 2014, 05:55:40 pm »
So you already know its wrong to use float for making time calculations, but still ask whats wrong? Never ever use float for times. If you cant help it use 64 bit ints or doubles, but the right way with SFML is to just only use sf::Time for all calculations and retrieve a number only at the last moment before you need one.
Reading the tutorial may be helpful: http://sfml-dev.org/tutorials/2.1/system-time.php

I replaced all  float with  long double a while ago, but this didn't help, as I still get a little jerkyness / speedups after some time.

2
General / Game loop becomes unstable after ~5 seconds
« on: August 12, 2014, 10:06:33 am »
Hey, I've been playing around with trying to make a game in my spare time. I've unfortunately just realized that my game loop is just terrible after five seconds of execution, and I'm not sure what to do to fix it. I've been following gaffer's game loop tutorial, for what it's worth. If you compile the below code, you'll see that initially everything is fine, but eventually the time becomes "jerky," and then everything just stops.

I know that this is (likely) due to a poor game loop, specifically the use of floats to store the time. But I don't know exactly what I'm doing wrong. I provided an attempted minimal example of my problem.

On a slightly related note, if there's something fundamentally wrong with my game loop structure, or I'm using poor coding practices somewhere, please let me know! Thanks!  :)

#include <SFML/Graphics.hpp>
void update(sf::CircleShape & s , float t ,float dt){
        s.setPosition(250+(float)(50*std::sin(t/10000.0)),250+(float)(50*std::cos(t/10000.0)));
}

int main(){
        sf::RenderWindow window(sf::VideoMode(500,500),"Testing!");

        sf::Clock clock;
        clock.restart();

        sf::CircleShape s;
        s.setRadius(50);
        s.setFillColor(sf::Color::Red);

        float t = 0.0f;
        const float dt = 0.01f;
        float currentTime = clock.getElapsedTime().asSeconds();
        float accumulatedTime = 0.0f;

        //Window Loop
        while(window.isOpen()){

                //Event processing.
                sf::Event event;
                while (window.pollEvent(event)){
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                //Timestep Updating.
                float  newTime = clock.getElapsedTime().asSeconds();
                float  frameTime = newTime - currentTime;
                if(frameTime > 0.25)
                        frameTime = 0.25;
                currentTime = newTime;
                accumulatedTime+=newTime;
               
                //Update loop.
                while(accumulatedTime > dt){
                        update(s,t,dt);
                        accumulatedTime-=dt;
                        t+=dt;
                }

                //Draw items, Linear interpolation.
                const float  alpha = accumulatedTime / dt;
                window.clear(sf::Color::Black);
        window.draw(s);
                window.display();
        }
}
 

3
General / Re: How to "blend" two states?
« on: July 11, 2014, 09:50:43 am »
Oh, I see. I had incorrectly connected state and frame in my head.

So in this example, while the velocity of an object may be changing in a non-linear fashion, we can use a linear approximation to get the position at time 1.01 in my case? Since it's the best we can (efficiently) do?

How do you (or what is a good manner to) keep a copy of "old" and "new" state for each time step?

Is there a more convenient way of doing this than having two containers, one designated "old" and one designated "new," and then having a "todraw" container that does the aforementioned linear approximation for position?

I'm having trouble thinking of how I would deal with the "new" container having more or fewer elements than the "old" one. For instance in the case of object creation or destruction, how would I properly estimate the position of an object that ceases to exist in the next state?

4
General / Re: How to "blend" two states?
« on: July 11, 2014, 09:12:28 am »
Simply put, it means that you should apply that calculation to all variables.

The "state" in this case is just what all the values are at that point. This "alpha blending" requires the previous state as well as the current one which means that you need access to all of the values that were "current" in the previous cycle. You then calculate them against the new current ones to blend them into an inbetween value.

Yeah, I understand that, I was just wondering if there was an easy, built in way to manage these.

For instance (and I don't know how everything works on the inside, so I can't speak to how feasible this is):

Instead of drawing everything on window.draw(...), I would instead have an intermediary: newframe and oldframe.

Where I would normally call window.draw(...), I would instead call newframe.draw(...). When a new frame is done drawing, the old frame is assigned to the new frame, and newframe.draw(...) is called again.

Now when it's time to do window.display() in my example, I just do window.display(0.8 * oldframe + 0.2 * newframe) ,or something with similar results and different syntax. Then I would call window.display(), and it would properly merge the two images such that they are now the combination of the two frames.

Is this easily done, or would I have to manually keep track of the "old" and "new" for each object I deal with?

Thank you for your help.

EDIT:

Are you certain that in the link provided, he is not implying that it's not a "visual blending" from one value to its successor? That's the impression I got from the article. I'm a little more confused then, if it's going backwards in time by the as-of-yet unprocessed time, because it seems like that would take rendering an entire frame all over again!

5
General / Re: How to "blend" two states?
« on: July 11, 2014, 09:00:26 am »
The simplest way is
Entity.move(horizontal speed * deltatime,vertical speed * deltatime);
 
Movement speed equals something like 200 pixels per second so that when multiplied by a number usually around 15 to 30-60 milliseconds you'll have a directly proportional movement. You must use a sf::Clock and call clock.restart().asSeconds() each frame for your deltatime.

Thanks, but that's unrelated to my issue.

If I know what the screen looks like at time T1 = 1.00 seconds, and I know what the screen looks like at time T2 = 1.05 seconds, I want to be able to draw the game at a time T3 = 1.01 seconds as if it were a blend of 80% T1 and 20% T2. I want to know how to do do that.

6
General / How to "blend" two states?
« on: July 11, 2014, 08:46:15 am »
So I was learning how to use SFML on my own for fun, but I've realized when things are moving at a medium speed, my screen gets a little choppy. Poking around the forums (which I should've done a long time ago...) I found countless links to http://gafferongames.com/game-physics/fix-your-timestep/ .

I just don't know how to implement this! My home-made loop is almost identical, except I don't have the last step of "combining" two different states into one in order to draw an "in-between" representative. (I also don't have a fancy state-updating function a la integrate-- but everything moves linearly in what I've made so that's okay.)

I currently just have a sf::RenderWindow that I draw everything on, and then display that window, and repeat. How would I do the following in SFML?

State state = currentState * alpha + previousState * ( 1.0 - alpha );

I'm sorry if this is a common question, as it probably is. I tried to search, but I don't think I know the proper terminology, so searching was fruitless.

Thank you!

Pages: [1]
anything