iphone time scaling

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

iphone time scaling

Postby mlfarrell » Wed Aug 25, 2010 7:33 pm

Okay so in my normal 3D games (without newton), I autorate the game like so:

Code: Select all
- (void)drawView:(id)sender
{
  glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
 
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glViewport(0, 0, backingWidth, backingHeight);
 
  CFAbsoluteTime time = CFAbsoluteTimeGetCurrent();
  float delta = (time - last_time) * 60.0f;
  last_time = time;
 
  fps += delta;
   
  //game->processing(delta);
 
  if(fps > 60)
  {
#ifdef DEBUG
    NSLog(@"FPS:  %d", a_fps);
#endif
    fps = 0;
    a_fps = 0;
  }
 
  //game->draw();
  a_fps++;


Where delta is in frames. I then use delta to scale my motion such as (x += delta*speed)

How the heck do I factor this into newton's code? My intuition tells me to do?
Code: Select all
- (void)drawView:(id)sender
{
  glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
 
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glViewport(0, 0, backingWidth, backingHeight);
 
  CFAbsoluteTime time = CFAbsoluteTimeGetCurrent();
  float delta = (time - last_time) * 60.0f;
  last_time = time;
 
  fps += delta;
   
  [GetWorld() Update: delta/60.0f];
  //   g_sceneManager->SetIntepolationParam(dFloat (g_timeAccumulator) / dFloat(DEMO_FPS_IN_MICROSECUNDS));
 
  if(fps > 60)
  {
#ifdef DEBUG
    NSLog(@"FPS:  %d", a_fps);
#endif
    fps = 0;
    a_fps = 0;
  }
 
  [g_sceneManager Render];
  a_fps++;


but when the game slows down, so does the physics. I want a constant rate of motion regardless of graphical performance.
mlfarrell
 
Posts: 14
Joined: Mon Aug 23, 2010 3:39 am

Re: iphone time scaling

Postby Julio Jerez » Wed Aug 25, 2010 7:36 pm

you commneted out the inteporlation,
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: iphone time scaling

Postby mlfarrell » Wed Aug 25, 2010 9:01 pm

Julio: actually this line was commented out in the iphone demo code that came with newton. I don't know what g_timeaccumulator should be, furthermore I'm not sure what it does. It seems like it'll interpolate between frames if the gfx FPS is HIGHER than that of the physics, but my problem is when the simulation slows down because of slow(er) graphics processing.
mlfarrell
 
Posts: 14
Joined: Mon Aug 23, 2010 3:39 am

Re: iphone time scaling

Postby Julio Jerez » Wed Aug 25, 2010 9:31 pm

that code wat taken from the tutorial in teh wiki. ther they do do the fix frame intepolatoions.
you can look who it is doen there.
whe the fps is slower it just do more physics updates.
Julio Jerez
Moderator
Moderator
 
Posts: 12452
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: iphone time scaling

Postby mlfarrell » Wed Aug 25, 2010 10:56 pm

Whew. I got it. But man that was rough. I set my max physics loops to 2 and did the following.

Code: Select all
static float g_timeAccumulator = 0;

- (void)drawView:(id)sender
{
  glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
 
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glViewport(0, 0, backingWidth, backingHeight);
 
  CFAbsoluteTime time = CFAbsoluteTimeGetCurrent();  //in floating point seconds
 
  // get the time step
  float deltaTime = (time - last_time)*1000000.0f; //in microseconds for physics engine
  last_time = time;
 
  g_timeAccumulator += deltaTime; 
  fps += delta;
   
  // advance the simulation at a fixed step
  int loops = 0;
 
  while((loops < MAX_PHYSICS_LOOPS) && (g_timeAccumulator >= FPS_IN_MICROSECUNDS))
  {           
    // run the newton update function
    [GetWorld() Update: 1.0f/PHYSICS_FPS]; 
           
    // subtract time from time accumulator
    g_timeAccumulator -= FPS_IN_MICROSECUNDS;
  }
 
  if(loops > MAX_PHYSICS_LOOPS)
  {
    g_timeAccumulator = FPS_IN_MICROSECUNDS;
  }
 
  [scene_manager set_interpolation: ((float)(g_timeAccumulator) / (float)(FPS_IN_MICROSECUNDS))];

  if(fps > 60)
  {
#ifdef DEBUG
    NSLog(@"FPS:  %d", a_fps);
#endif
    fps = 0;
    a_fps = 0;
  }
 
  [scene_manager render];
  a_fps++;
 
  glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
  [context presentRenderbuffer:GL_RENDERBUFFER_OES];   
}


mlfarrell
 
Posts: 14
Joined: Mon Aug 23, 2010 3:39 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 46 guests