nn::pia::reckoning::Initialize();
nn::pia::reckoning::BeginSetup(); nn::pia::reckoning::EndSetup(); |
nn::pia::reckoning::Finalize();
|
Instances of ReckoningCloneElement can be created and destroyed at any time (even before and after starting communications). Do not destroy instances of these objects while they are still in CloneProtocol, however. This can cause erroneous memory access issues.
After setting a ReckoningStrategy to ReckoningCloneElement, you can add instances of ReckoningCloneElement to the clone and use them. You can add or remove instances of ReckoningCloneElement the same way you would add or remove clone elements.
nn::pia::clone::SendClone sendClone;
nn::pia::clone::ReceiveClone receiveClone; nn::pia::reckoning::Simple3dReckoningStrategy reckoningStrategy(1.f); nn::pia::reckoning::ReckoningCloneElement<nn::pia::reckoning::Simple3dReckoningStrategy> elementForSend; nn::pia::reckoning::ReckoningCloneElement<nn::pia::reckoning::Simple3dReckoningStrategy> elementForReceive; elementForSend.SetReckoningStrategy(&reckoningStrategy); elementForReceive.SetReckoningStrategy(&reckoningStrategy); sendClone.RegisterElement(&elementForSend, 0x0001); receiveClone.RegisterElement(&elementForReceive, 0x0001); |
Before calling the SetValue() function, use the IsReadyToSetValue() function to determine whether values can be set to the clones that have been added. Note that this is the same procedure used for clone elements. Similarly, before calling the GetValue() function, use the IsValidValue() function to download values and determine whether they are valid. This check indicates whether clones can get these values correctly.
// Set value.
if(elementForSend.IsReadyToSetValue()) { nn::pia::reckoning::Vector3f pos_for_send; bool is_stop = false; elementForSend.SetValue(pos_for_send, &is_stop); } // Get the value. if(elementForReceive.IsValidValue()) { nn::pia::reckoning::Vector3f pos_for_receive = elementForReceive.GetValue(); } |
Vector3f is a class for 3D vectors defined in the reckoning namespace. You can replace this class with your own 3D vector class if you have one in your application.
You can approximate a 3D Vector3f vector in a 2D space by setting one of the three vector components to a fixed value (such as 0) when you call the SetValue() function. You can also extend this idea to approximate the vector in a 1D space.
When using a dead-reckoning algorithm that uses linear functions, the estimated values may fluctuate if a game object suddenly stops (without decelerating) after moving at a constant speed along a straight line, as shown in Figure 9-3. Oscillating Estimated Values . In the example in Figure 9-3. Oscillating Estimated Values, where the game object stops at time t , the algorithm using linear functions yields estimated values that fluctuate because the estimated values after time t are for estimated path 1.
The Simple3dReckoningStrategy library implements a stopped flag feature for improving oscillation. When a game object enters a stopped state, oscillation can be prevented by having the application pass the stopped flag to Simple3dReckoningStrategy. At this time, pass a flag to the system indicating that the game object is no longer moving when you call the SetValue() function. (The application determines when the object switches from a moving state to a stopped state.) Normally, a new sample value is sent when the estimated value and actual value differ by more than a specified threshold value. In this case, however, when the flag indicating the object has stopped is passed to the SetValue() function, the value at that time is force-set as the sample value and sent to the receiving station. This results in the value for the position of the stopped game object being sent immediately and used on the receiving end, so the value does not oscillate.