Jump to content

  • Log In with Google      Sign In   
  • Create Account


Semi-fixed timestep with horizontal accel/deceleration


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 mr_sad   Members   -  Reputation: 100

Like
0Likes
Like

Posted 20 February 2012 - 09:27 PM

I would like to implement the code as featured at the end of the Fix Your Timestep article (found here: http://gafferongames...-your-timestep/).

I would like to apply that implementation to a sprite which can move left or right, and which accelerates from a standing position to its maximum speed when the right arrow or left arrow is held down (in the corresponding direction) and decelerates when no arrow key is held down.

Could someone please provide the XNA code that would enable me to do this? I don't ordinarily ask for things like this, but I've been banging away at it for days and I'm just going in circles.

The semi-fixed timestep code from the article is displayed here for your convenience:

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 );
}

Sponsor:

#2 mrhyperpenguin   Members   -  Reputation: 300

Like
0Likes
Like

Posted 21 February 2012 - 12:13 AM

Try this and see if this works.

In Main, delete the "using (Game game1...) { ... }" block, create your own GraphicsDevice (you have to use WinForms), and copy and paste the code from the article.

static void Main(string[] args)
{
	 GraphicsDevice device = new GraphicsDevice();

	 <code from the article>
}

Then make sure you call GraphicsDevice.Present() in your render() function

void render(state, GraphicsDevice)
{
	 GraphicsDevice.Clear();

	 <render using state>

	 GraphicsDevice.Present();
}

Hopefully that should work. If you want your gameloop to integrate better with the Windows message loop you can read this (link).

Let me know how it works out.
Victor




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS