Newton update in the background?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Newton update in the background?

Postby Leadwerks » Sun Apr 24, 2011 10:21 pm

Is it supported to run newtonupdate in the background on another thread while rendering or game logic simultaneously takes place? Will calls to newton ray cast or other functions in the main thread interfere with the update running in the background? It would be nice if the physics update could run while other threads render graphics and update the game.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Newton update in the background?

Postby JernejL » Mon Apr 25, 2011 7:26 am

I think your raycasting should have been in the physics thread.. but let's wait for Julio's answer.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Newton update in the background?

Postby Julio Jerez » Mon Apr 25, 2011 8:15 am

all of the demos run the engine asyncronous on a separate threads.
they is even a menu option to turn that on or off.

This is not a property of Newton, you have to do it on your side,
but the set of libraries that comes with Newton have a class that dThread that implement a Abstat asyncronus Thread.

this is how the class looks like

Code: Select all
class dThread
{
   public:
   dThread(void);
   virtual ~dThread(void);

   virtual unsigned RunMyTask () { return 1;}

   void ContinueExecution ();
   void StopsExecution ();
   void TerminateTask ();

   void YieldTime();
   void Lock(unsigned& lockVar);
   void Unlock(unsigned& lockVar);

   private:
   void ExcuteTask();

#if defined (_MSC_VER)
   static unsigned _stdcall TaskCallback(void *param);
#endif

#if (defined (_POSIX_VER) || defined (_MAC_VER))
   static void* TaskCallback(void *Param);
#endif


   unsigned m_taskSuspendedCount;
   unsigned m_taskExecuting;
   unsigned m_terminated;

#if defined (_MSC_VER)
   uintptr_t m_threadhandle;
#endif

#if (defined (_POSIX_VER) || defined (_MAC_VER))
   pthread_t m_threadhandle;
#endif
};



so basically all you have to do is derive form that class and implemnet teh function RunMyTask ()
this is the one that I use in teh demos

Code: Select all
class dSimulationThread: public dThread
{
   public:
   dSimulationThread();
   ~dSimulationThread();

   virtual unsigned RunMyTask ();
   bool m_asycronousUpdate;
   dFloat m_physicsTime;
};



the function looks like this

Code: Select all
unsigned dSimulationThread::RunMyTask ()
{
   if (m_asycronousUpdate) {
      DemoEntityManager& me = *((DemoEntityManager*)this);
      me.UpdatePhysics();
   }
   return 1;
}



Code: Select all
void DemoEntityManager::UpdatePhysics()
{
   // read the controls


   // update the physics
   if (m_world) {
      dFloat timestepInSecunds = 1.0f / MAX_PHYSICS_FPS;
      unsigned64 timestepMicrosecunds = unsigned64 (timestepInSecunds * 1000000.0f);

      unsigned64 currentTime = dGetTimeInMicrosenconds ();
      unsigned64 nextTime = currentTime - m_microsecunds;
      int loops = 0;
      while ((nextTime >= timestepMicrosecunds) && (loops < MAX_PHYSICS_LOOPS)) {
         loops ++;

         // run the newton update function
         if (!m_reEntrantUpdate) {
            m_reEntrantUpdate = true;
            if (m_physicsUpdate && m_world) {
               // update the camera;
               UpdateCamera (timestepInSecunds);
               // update teh world
               NewtonUpdate (m_world, timestepInSecunds);

               dListNode* nextNode;
               for (dListNode* node = GetFirst(); node; node = nextNode) {
                  nextNode = node->GetNext();

                  DemoEntity* const entity = node->GetInfo();
                  entity->AddRef();
                  entity->SimulationLister(this, node, timestepInSecunds);
                  entity->Release();
               }

               
            }
            m_reEntrantUpdate = false;
         }

         nextTime -= timestepMicrosecunds;
         m_microsecunds += timestepMicrosecunds;
      }

      if (loops) {
         m_physicsTime = dFloat (dGetTimeInMicrosenconds () - currentTime) / 1000000.0f;

         if (m_physicsTime >= MAX_PHYSICS_LOOPS * (1.0f / MAX_PHYSICS_FPS)) {
            m_microsecunds = currentTime;
         }
      }
   }
}


It run Newton on a Separate Thread at a Fix Rate, and you can supend it and re started any time you want.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton update in the background?

Postby Julio Jerez » Mon Apr 25, 2011 8:19 am

Raycast and almost all funtionality in teh engien is asycronous,
the only thing it is not data paraalelt is the creation and destrution of objects tha use the memory manage, but use NewtonLock and Unlock can deal wih that issue.

Like Delfi say if you are going that route, you will also have to implement stuff like Raycast, Camera movement, Imput collection on the simulation thread.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests