Physics out of sync with rendering rotation

View previous topic View next topic Go down

Physics out of sync with rendering rotation

Post  DJM on Thu Jun 04, 2009 3:35 pm

I have 2 objects in my world: a ball, and a long rectangular box. Imagine it sort of like a catapult. The rectangular box lies flat originally, but using accelerometer values it rotates around the X and Y axes (with the box's center being almost the exact center of the box). The ball bounces on top of the box, but sometimes when rotating on the Y axis quickly, the ball just passes right through instead of colliding like it should. With smaller accelerometer values (more gradual movement), the ball correctly bounces off the faces of the box even while rotating.

I have tried different values and a number of other things mentioned in documentation and this forum. If anyone has any idea how my physics could better sync (I think this is the problem), please let me know Very Happy

The code is based off of tutorial6_02.

Code:
void templatePhysicCullingThread( void *_ptr )
{
   // If we do not have a start time
   // get the current elapsed time.
   if( !time_start )
   { time_start = sio2GetElapsedTime(); }

   if( refresh_thread )
   {
      // Request a new frame right away.
      refresh_thread = 0;

      // Retrieve back our userdata pointer that
      // is currently binded to the resource.
      SIO2resource *_SIO2resource = ( SIO2resource * )_ptr;

      // Set something here...   
      SIO2object * box = ( SIO2object * )sio2ResourceGet( _SIO2resource,SIO2_OBJECT,"object/Box");
      SIO2object *ball = ( SIO2object * )sio2ResourceGet( _SIO2resource,SIO2_OBJECT,"object/Ball");

      if( ball ){
         ball->_btRigidBody->setActivationState( ACTIVE_TAG );
      }
      if( box ){
         // Make sure that our rigid body is activated instead whatever we do would be ignored.
         box->_btRigidBody->setActivationState( ACTIVE_TAG );         
         // Affect this new matrix to the Bullet rigid body matrix.
         box->_btRigidBody->getWorldTransform().setFromOpenGLMatrix( box->_SIO2transform->mat );
         // Set the linear velocity of our physic object
         box->_btRigidBody->setLinearVelocity( btVector3( 0.0, 0.0, 2.0 ) );
      }
      
      sio2PhysicRender( sio2->_SIO2physic, 1.0f/60.0f, 2 );      

      // Refresh the current culling state of every objects.
      sio2ResourceCull( _SIO2resource, sio2->_SIO2camera );

      // Update the time
      time_base = sio2GetElapsedTime();
      
      // Check if we got a second elapsed.
      if( time_base - time_start >= 1000 )
      {
         physic_fps = current_fps;
         time_start = time_base;
         current_fps = 0;
      }
      else
      { ++current_fps; }      
   }
}

Code:

void templateRender( void )
{
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
   
   SIO2object *box = ( SIO2object * )sio2ResourceGet(sio2->_SIO2resource,SIO2_OBJECT,"object/Box");
   box->_SIO2transform->rot->x = ROT_X;
   box->_SIO2transform->rot->y = ROT_Y;
   sio2TransformBindMatrix( box->_SIO2transform );
   
   sio2CameraRender( sio2->_SIO2camera );
   
   glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
   
   sio2CameraUpdateFrustum( sio2->_SIO2camera );
   
   sio2ResourceCull( sio2->_SIO2resource, sio2->_SIO2camera );      
   
   sio2ResourceRender( sio2->_SIO2resource,
                  sio2->_SIO2window,
                  sio2->_SIO2camera,
                  SIO2_RENDER_SOLID_OBJECT | SIO2_RENDER_CLIPPED_OBJECT );
   
   // Reset back all OpenGL states required to draw properly the previous objects.
   sio2ObjectReset();
   sio2MaterialReset();
   
   if( !refresh_thread )
   { refresh_thread = 1; }   
}

Code:

void templateScreenAccelerometer( void *_ptr )
{
   #ifndef USE_SIMULATOR
   ROT_X = sio2->_SIO2window->accel->y * 100;
   ROT_Y = sio2->_SIO2window->accel->x * -100;
   #endif   
}

My physics and animation are both at intervals of 1.0/60.0. The accelerometer goes at 1/90, but that shouldn't matter. I even tried doing the SIO transformation inside templateScreenAccelerometer(), but the physics results were the same.

Thanks.
avatar
DJM

Posts : 1
Join date : 2009-06-03
Age : 34
Location : Rochester, NY

View user profile

Back to top Go down

Re: Physics out of sync with rendering rotation

Post  sio2interactive on Thu Jun 04, 2009 3:59 pm

Of course when you are using a second thread for the physic you will always have a small latency before the 2 threads are sync. Have you try to:

1. Use the sio2PhysicRender function in your main thread?

2. Set a higher number of physic pass: sio2PhysicRender( sio2->_SIO2physic, 1.0f/60.0f, 4 );

Lemme know...

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 37
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum