Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:33:58+00:00 2026-05-15T02:33:58+00:00

So in my game I need to simulate the physics in a hardware independent

  • 0

So in my game I need to simulate the physics in a hardware independent manner.

I’m going to use a fixed time-step simulation, but I need to be able to calculate how much time is passing between calls.

I tried this and got nowhere:

#include <time.h>
double time_elapsed;
clock_t last_clocks = clock();
while (true) {
  time_elapsed = ( (double) (clock() - last_clocks) / CLOCKS_PER_SEC);
  last_clocks = clock();
  printf("%f\n", time_elapsed);
}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:33:58+00:00Added an answer on May 15, 2026 at 2:33 am

    You can use gettimeofday to get the number of seconds plus the number of microseconds since the Epoch. If you want the number of seconds, you can do something like this:

    #include <sys/time.h>
    float getTime()
    {
        struct timeval time;
        gettimeofday(&time, 0);
        return (float)time.tv_sec + 0.000001 * (float)time.tv_usec;
    }
    

    I misunderstood your question at first, but you might find the following method of making a fixed-timestep physics loop useful.

    There are two things you need to do to make a fixed-timestep physics loop.

    First, you need to calculate the time between now and the last time you ran physics.

    last = getTime();
    while (running)
    {
        now = getTime();
        // Do other game stuff
        simulatePhysics(now - last);
        last = now;
    }
    

    Then, inside of the physics simulation, you need to calculate a fixed timestep.

    void simulatePhysics(float dt)
    {
        static float timeStepRemainder; // fractional timestep from last loop
        dt += timeStepRemainder * SIZE_OF_TIMESTEP;
    
        float desiredTimeSteps = dt / SIZE_OF_TIMESTEP;
        int nSteps = floorf(desiredTimeSteps); // need integer # of timesteps
    
        timeStepRemainder = desiredTimeSteps - nSteps;
    
        for (int i = 0; i < nSteps; i++)
            doPhysics(SIZE_OF_TIMESTEP);
    
    }
    

    Using this method, you can give whatever is doing physics (doPhysics in my example) a fixed timestep, while keeping a synchronization between real time and game time by calculating the right number of timesteps to simulate since the last time physics was run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need physics engine to simulate game world on server. I googled for .Net
The idea is that a game might need something fast but also simplistic. Mainly
I need to simulate a keypress in game window. I try to send key
I'm making a game and I need to be able to ship some data
I am working on making a simple Javascript game and need to be able
I'm currently developing a flash game and I need to be able to test
I have a cooperative PC game, but the second player need Xbox joystick to
I am making game and need to prepare view for level selection. Could you
For the needs of my game I need to have 2D sound. That means,
I'm creating a text-based browser game and need some advice for django model structure.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.