Search Unity

NetworkRigidbody sorted States

Discussion in 'Multiplayer' started by Der Dude, Jun 2, 2008.

  1. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Hello everyone.

    Since I only ask and seldom answer on this board, I decided to "give something back".

    This is the script in the Unity-Networking Example for synchronizing Rigidbodies over the Network, except it uses a variant of the Insertion-Sort to make sure the states are all in order.

    When the states were not in order I sometimes got strange behaviour of the rigidbodies, where they would dis- and reappear constantly. After using this version I havn't observed anything similar yet.

    It is also not more costly in performance than the older version, since the timestamps were compared at the end to check for inconsistencies anyway.

    It is not much, but its something.

    Here is the inserting function:

    Code (csharp):
    1. //This Function inserts the new State in the proper slot.
    2.     //This is the Insertion-Sort
    3.     private void NewState( State newState )
    4.     {
    5.         //If no States are present, put in first slot.
    6.         if( m_TimestampCount == 0 )
    7.         {
    8.             m_BufferedState[0] = newState;
    9.         }
    10.         else
    11.         {
    12.        
    13.             //First find proper place in buffer. If no place is found, state can be dropped (newState is too old)
    14.             for( int i = 0; i < m_TimestampCount; i ++ )
    15.             {
    16.                 //If the state in slot i is older than our new state, we found our slot.   
    17.                 if( m_BufferedState[i].timestamp < newState.timestamp )
    18.                 {
    19.                     // Shift the buffer sideways, to make room in slot i. possibly deleting state 20
    20.                     for (int k=m_BufferedState.Length-1;k>i;k--)
    21.                     {
    22.                         m_BufferedState[k] = m_BufferedState[k-1];
    23.                     }
    24.                    
    25.                     //insert state
    26.                     m_BufferedState[i] = newState;
    27.                    
    28.                     //We are done, exit loop
    29.                     break;
    30.                 }
    31.                
    32.             }
    33.        
    34.         }
    35.        
    36.         //Update TimestampCount
    37.         m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);
    38.        
    39.     }
     

    Attached Files: