|

楼主 |
发表于 2008-7-18 16:34:00
|
显示全部楼层
Re:如何采用导航推测法实现网络同步?
贴上自己的代码
- //Update every frame
- void MoveEventProcess::ExecuteEvent(float fElapsedTime;Mx_ICharacter* pCharacter)
- {
- //The Prediction position
- MxPoint3 ptPredictionPos(0,0,0);
-
- m_bMovingPrediction = false;
-
- //Receive new package
- if(!m_queMovePath.empty() && !m_bStartSmooth)
- {
-
- m_bStartSmooth = true;
-
- //pop the package from the queue
- MovePath* path = m_queMovePath.front();
- m_queMovePath.pop_front();
-
- //get the velocity and position from package
- ptVelocityNew = path->m_ptVelocity;
- ptPosNew = path->oCurve.m_ptDest;
-
- //get current position and velocity
- ptPosOld = pCharacter->GetTranslate();
- ptVelocityOld = m_ptLastVelocity;
-
- //get the orientation
- fDestAngle = path->oCurve.m_fDestAngle;
-
- //Safe Realse the path
- Mx_Safe_Delete(path);
- }
-
- else if (m_bStartSmooth)
- {
- //predict for 1 second
- if (m_fPredictionTime < 1)
- {
-
- ptPredictionPos = CubicSplines(ptPosOld,ptVelocityOld,ptPosNew,ptVelocityNew,m_fPredictionTime);
-
- //Change the Character
- pCharacter->SetTranslate(ptPredictionPos);
- pCharacter->SetAnimationEvent(m_eLastTrigger);
- pCharacter->SetOrient(fDestAngle);
-
- //add the fElapsedTime
- m_fPredictionTime = m_fPredictionTime + fElapsedTime;
-
- }
- else
- {
- m_bStartSmooth = false;
- m_fPredictionTime = 0.0f;
-
- //save the new velocity to be a last veloctity
- m_ptLastVelocity = ptVelocityNew;
- }
- }
-
- //Make the character physical movement
- else if (m_queMovePath.empty() && !m_bStartSmooth)
- {
-
- //If current state isn't stop
- if (!m_ptLastVelocity.AlmostEqual(MxPoint3::ZERO))
- {
- m_bMovingPrediction = true;
-
- //Calculated the position by velocity
- MxPoint3 ptDisplace = m_ptLastVelocity * fElapsedTime;
- MxPoint3 ptDest = pCharacter->GetTranslate() + ptDisplace;
-
- //Update the Character
- pCharacter->SetTranslate(ptDest);
- pCharacter->SetAnimationEvent(m_eLastTrigger);
- }
-
- }
- }
-
- //Cubic Splines Processing
- //
- MxPoint3 MoveEventProcess::CubicSplines(MxPoint3 ptPosOld,MxPoint3 ptVelocityOld,MxPoint3 ptPosNew,MxPoint3 ptVelocityNew,float fTime)
- {
- float t = fTime;
-
- //新的加速度
- //float fAccelerationNew = 0.f;
-
- MxPoint3 pos0 = ptPosOld;
- MxPoint3 pos1 = ptPosOld + ptVelocityOld;
- MxPoint3 pos2 = ptPosNew - ptVelocityNew ;
- MxPoint3 pos3 = ptPosNew;
-
- float A = pos3.x - 3 * pos2.x + 3 * pos1.x - pos0.x;
- float B = 3 * pos2.x - 6 * pos1.x + 3 * pos0.x;
- float C = 3 * pos1.x - 3 * pos0.x;
- float D = pos0.x;
-
- float E = pos3.y - 3 * pos2.y + 3 * pos1.y - pos0.y;
- float F = 3 * pos2.y - 6 * pos1.y + 3 * pos0.y;
- float G = 3 * pos1.y - 3 * pos0.y;
- float H = pos0.y;
-
- float I = pos3.z - 3 * pos2.z + 3 * pos1.z - pos0.z;
- float J = 3 * pos2.z - 6 * pos1.z + 3 * pos0.z;
- float K = 3 * pos1.z - 3 * pos0.z;
- float L = pos0.z;
-
- float t1 = t;
- float t2 = t * t;
- float t3 = t * t * t;
-
- float x = A * t3 + B * t2 + C * t1 + D;
- float y = E * t3 + F * t2 + G * t1 + H;
- float z = I * t3 + J * t2 + K * t1 + L;
-
- MxPoint3 pt(x,y,z);
- return pt;
- }
-
复制代码 |
|