Domanda

I've read the article from here.

But it seems i can't translate that to Java, and by that I mean this:

double t = 0.0;
const double dt = 0.01;

double currentTime = hires_time_in_seconds();
double accumulator = 0.0;

State previous;
State current;

while ( !quit )
{
     double newTime = time();
     double frameTime = newTime - currentTime;
     if ( frameTime > 0.25 )
          frameTime = 0.25;   // note: max frame time to avoid spiral of death
     currentTime = newTime;

     accumulator += frameTime;

     while ( accumulator >= dt )
     {
          previousState = currentState;
          integrate( currentState, t, dt );
          t += dt;
          accumulator -= dt;
     }

     const double alpha = accumulator / dt;

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

     render( state );
} 

What is the State class he is using? I've downloaded the code and i couldn't find it's declaration? How would the code look like in Java?

È stato utile?

Soluzione

State is more an abstract idea. He's just interpolating a number. For example, state could be the x position of an entity.

An example for you:

float x = x*alpha+oldX*(1-alpha);

In my physics game, I passed the alpha value to all my entities during each render. They would use this during the render to calculate the best approximation of their position. I would suggest you do the same. Just have your render routines accept alpha, and have each object track its old state.

So every entity guesses where it really is at the time of rendering using its last position and its current position.

EDIT:

public class Entity{
    double oldX;
    double oldY;
    double x;
    double y;
    public void render(Graphics g, float alpha){
        float estimatedX = x*alpha+oldX*(1-alpha);
        float estimatedY = y*alpha+oldY*(1-alpha);
        g.drawRect((int)estimatedX,(int)estimatedY,1,1);
    }
}

Altri suggerimenti

It's a simple structure containing the current position and velocity before each integration step. It's defined in the previous tutorial, and also near the beginning of Timestep.cpp in the code you can download from that page:

struct State
{
     float x;          // position
     float v;          // velocity
};
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top