BVB Source Codes

CRYENGINE Show AnimatedCharacterPPS_ParamCalculation.cpp Source code

Return Download CRYENGINE: download AnimatedCharacterPPS_ParamCalculation.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "AnimatedCharacter.h"
  5. #include "AnimationGraphCVars.h"
  6. #include "PersistantDebug.h"
  7.  
  8. //--------------------------------------------------------------------------------
  9.  
  10. QuatT CAnimatedCharacter::CalculateDesiredLocation() const
  11. {
  12.         ANIMCHAR_PROFILE;
  13.  
  14.         // Disabled this optimization because it leads to improper calculation of
  15.         // parameters, which leads to improper footstep sounds (and wrong animevents
  16.         // timing in general)
  17.         //
  18.         //if (m_simplifyMovement)
  19.         //{
  20.         //      return QuatT(IDENTITY);
  21.         //}
  22.  
  23.         if (m_curFrameTime <= 0.0f)
  24.         {
  25.                 return QuatT(IDENTITY);
  26.         }
  27.  
  28.         // NOTE: This whole function was optimized to ignore xy rotations and only consider z rotations!
  29.  
  30.         // ANIM-UNSUPPORTED 2+ Not sure we really need this, so disable it for now
  31.         //
  32.         // Use zero/identity prediction while anim target is active (prevents some jittering from PMC in end of smart objects).
  33.         //if ((m_pAnimTarget != NULL) && m_pAnimTarget->activated)
  34.         //{
  35.         //      return QuatT(IDENTITY);
  36.         //}
  37.  
  38.         QuatT invEntLocation = m_entLocation.GetInverted();
  39.  
  40.         // NOTE: prediction doesn't take animation motion into account, this is a pure game-based entity motion prediction
  41.  
  42. #ifdef _DEBUG
  43.         /*
  44.            if (DebugFilter())
  45.            {
  46.             DebugHistory_AddValue("eDH_TEMP00", m_smoothedActualAnimVelocity);
  47.             DebugHistory_AddValue("eDH_TEMP01", RecentCollision() ? 1.0f : 0.0f);
  48.             DebugHistory_AddValue("eDH_TEMP02", (float)(m_curFrameID - m_collisionFrameID));
  49.            }
  50.            /**/
  51. #endif
  52.  
  53.         QuatT desiredLocalLocation;
  54.         if (RecentCollision())
  55.         {
  56.                 QuatT predictedWorldLocation = ApplyWorldOffset(m_entLocation, m_requestedEntityMovement);
  57.  
  58.                 Vec3 predictionWorldLocationOffset = predictedWorldLocation.t - m_entLocation.t;
  59.                 predictionWorldLocationOffset = RemovePenetratingComponent(predictionWorldLocationOffset, m_collisionNormal[0]);
  60.                 predictionWorldLocationOffset = RemovePenetratingComponent(predictionWorldLocationOffset, m_collisionNormal[1]);
  61.                 predictionWorldLocationOffset = RemovePenetratingComponent(predictionWorldLocationOffset, m_collisionNormal[2]);
  62.                 predictionWorldLocationOffset = RemovePenetratingComponent(predictionWorldLocationOffset, m_collisionNormal[3]);
  63.  
  64.                 desiredLocalLocation.t = invEntLocation.q * predictionWorldLocationOffset;
  65.         }
  66.         else
  67.         {
  68.                 desiredLocalLocation.t = invEntLocation.q * m_requestedEntityMovement.t;
  69.         }
  70.  
  71.         desiredLocalLocation.q.SetRotationZ(m_requestedEntityMovement.q.GetRotZ());
  72.  
  73.         return desiredLocalLocation;
  74. }
  75.  
  76. //--------------------------------------------------------------------------------
  77.  
  78. void CAnimatedCharacter::CalculateParamsForCurrentMotions()
  79. {
  80.         if (m_doMotionParams)
  81.         {
  82.                 ANIMCHAR_PROFILE;
  83.  
  84.                 IEntity* pEntity = GetEntity();
  85.                 CRY_ASSERT(pEntity);
  86.  
  87.                 ICharacterInstance* pCharacterInstance = pEntity->GetCharacter(0);
  88.                 if (pCharacterInstance == NULL)
  89.                         return;
  90.  
  91.                 ISkeletonAnim* pSkeletonAnim = pCharacterInstance->GetISkeletonAnim();
  92.                 if (pSkeletonAnim == NULL)
  93.                         return;
  94.  
  95.                 SetDesiredLocalLocation(pSkeletonAnim, m_desiredLocalLocation, (float)m_curFrameTime);
  96.                 if (m_pShadowSkeletonAnim)
  97.                         SetDesiredLocalLocation(m_pShadowSkeletonAnim, m_desiredLocalLocation, (float)m_curFrameTime);
  98.  
  99. #ifdef _DEBUG
  100.                 DebugGraphQT(m_desiredLocalLocation, "eDH_DesiredLocalLocationTX", "eDH_DesiredLocalLocationTY", "eDH_DesiredLocalLocationRZ");
  101.                 //DebugGraphMotionParams(pSkeletonAnim);
  102. #endif
  103.         }
  104. }
  105.  
  106. //--------------------------------------------------------------------------------
  107.  
  108. #define MAX_EXEC_QUEUE (0x8u);
  109.  
  110. void CAnimatedCharacter::SetDesiredLocalLocation(ISkeletonAnim* pSkeletonAnim, const QuatT& desiredLocalLocation, float fDeltaTime)
  111. {
  112.         CRY_ASSERT(desiredLocalLocation.IsValid());
  113.  
  114.         {
  115.                 uint32 NumAnimsInQueue = pSkeletonAnim->GetNumAnimsInFIFO(0);
  116.                 uint32 nMaxActiveInQueue = MAX_EXEC_QUEUE;
  117.                 if (nMaxActiveInQueue > NumAnimsInQueue)
  118.                         nMaxActiveInQueue = NumAnimsInQueue;
  119.                 if (NumAnimsInQueue > nMaxActiveInQueue)
  120.                         NumAnimsInQueue = nMaxActiveInQueue;
  121.  
  122.                 uint32 active = 0;
  123.                 for (uint32 a = 0; a < NumAnimsInQueue; a++)
  124.                 {
  125.                         const CAnimation& anim = pSkeletonAnim->GetAnimFromFIFO(0, a);
  126.                         active += anim.IsActivated() ? 1 : 0;
  127.                 }
  128.  
  129.                 if (active > nMaxActiveInQueue)
  130.                         active = nMaxActiveInQueue;
  131.                 nMaxActiveInQueue = active;
  132.  
  133.                 const SParametricSampler* pLMG = NULL;
  134.                 for (int32 a = nMaxActiveInQueue - 1; (a >= 0) && !pLMG; --a)
  135.                 {
  136.                         const CAnimation& anim = pSkeletonAnim->GetAnimFromFIFO(0, a);
  137.                         pLMG = anim.GetParametricSampler();
  138.                 }
  139.         }
  140.  
  141.         const Vec3 dir = desiredLocalLocation.GetColumn1();
  142.         float turnAngle = atan2f(-dir.x, dir.y);
  143.  
  144.         float turnSpeed = turnAngle / fDeltaTime;
  145.  
  146.         const Vec2 deltaVector(desiredLocalLocation.t.x, desiredLocalLocation.t.y);
  147.         const float travelDist = deltaVector.GetLength();
  148.  
  149.         const Vec2 deltaDir = (travelDist > 0.0f) ? (deltaVector / travelDist) : Vec2(0, 0);
  150.         float travelAngle = (deltaDir.x == 0.0f && deltaDir.y == 0.0f ? 0.0f : atan2f(-deltaDir.x, deltaDir.y));
  151.  
  152.         float travelSpeed;
  153.         if (gEnv->bMultiplayer)
  154.         {
  155.                 travelSpeed = travelDist / fDeltaTime;
  156.         }
  157.         else
  158.         {
  159.                 const float cosGroundSlope = fabsf(cosf(m_fGroundSlopeMoveDirSmooth));
  160.                 travelSpeed = (cosGroundSlope > FLT_EPSILON) ? (travelDist / (fDeltaTime * cosGroundSlope)) : (travelDist / fDeltaTime);
  161.         }
  162.  
  163.         // TravelAngle smoothing
  164.         {
  165.                 const Vec2 newStrafe = Vec2(-sin_tpl(travelAngle), cos_tpl(travelAngle));
  166.                 if (CAnimationGraphCVars::Get().m_enableTurnAngleSmoothing)
  167.                 {
  168.                         SmoothCD(m_fDesiredStrafeSmoothQTX, m_fDesiredStrafeSmoothRateQTX, fDeltaTime, newStrafe, 0.10f);
  169.                 }
  170.                 else
  171.                 {
  172.                         m_fDesiredStrafeSmoothQTX = newStrafe;
  173.                         m_fDesiredStrafeSmoothRateQTX.zero();
  174.                 }
  175.                 travelAngle = Ang3::CreateRadZ(Vec2(0, 1), m_fDesiredStrafeSmoothQTX);
  176.         }
  177.  
  178.         const bool modulateTurnSpeedByTravelAngle = true;
  179.         if (modulateTurnSpeedByTravelAngle)
  180.         {
  181.                 static float minimum = DEG2RAD(10.0f); // do not change turnSpeed when travelAngle is below this
  182.                 static float maximum = DEG2RAD(40.0f); // force turnspeed to zero when travelAngle is above this
  183.                 const float turnSpeedScale = 1.0f - clamp_tpl(fabsf(travelAngle) - minimum, 0.0f, maximum - minimum) / (maximum - minimum);
  184.                 turnSpeed *= turnSpeedScale;
  185.         }
  186.  
  187.         // TurnSpeed smoothing
  188.         {
  189.                 if (CAnimationGraphCVars::Get().m_enableTurnSpeedSmoothing)
  190.                 {
  191.                         SmoothCD(m_fDesiredTurnSpeedSmoothQTX, m_fDesiredTurnSpeedSmoothRateQTX, fDeltaTime, turnSpeed, 0.40f);
  192.                 }
  193.                 else
  194.                 {
  195.                         m_fDesiredTurnSpeedSmoothQTX = turnSpeed;
  196.                         m_fDesiredTurnSpeedSmoothRateQTX = 0.0f;
  197.                 }
  198.         }
  199.  
  200.         // TravelSpeed smoothing
  201.         {
  202.                 if (CAnimationGraphCVars::Get().m_enableTravelSpeedSmoothing)
  203.                 {
  204.                         SmoothCD(m_fDesiredMoveSpeedSmoothQTX, m_fDesiredMoveSpeedSmoothRateQTX, fDeltaTime, travelSpeed, 0.04f);
  205.                 }
  206.                 else
  207.                 {
  208.                         m_fDesiredMoveSpeedSmoothQTX = travelSpeed;
  209.                         m_fDesiredMoveSpeedSmoothRateQTX = 0.0f;
  210.                 }
  211.         }
  212.  
  213.         SetMotionParam(pSkeletonAnim, eMotionParamID_TravelSpeed, m_fDesiredMoveSpeedSmoothQTX);
  214.         SetMotionParam(pSkeletonAnim, eMotionParamID_TurnSpeed, m_fDesiredTurnSpeedSmoothQTX * CAnimationGraphCVars::Get().m_turnSpeedParamScale);
  215.         SetMotionParam(pSkeletonAnim, eMotionParamID_TravelAngle, travelAngle);
  216.         // eMotionParamID_TravelSlope
  217.         SetMotionParam(pSkeletonAnim, eMotionParamID_TurnAngle, turnAngle);
  218.         // eMotionParamID_TravelDist
  219.         SetMotionParam(pSkeletonAnim, eMotionParamID_StopLeg, 0);
  220. }
  221.  
  222. //--------------------------------------------------------------------------------
  223.  
  224. void CAnimatedCharacter::SetMotionParam(ISkeletonAnim* const pSkeletonAnim, const EMotionParamID motionParamID, const float value) const
  225. {
  226.         // Allow parameters to be overridden by the movement request
  227.         float overriddenValue(value);
  228.         m_moveRequest.prediction.GetParam(motionParamID, overriddenValue);
  229.  
  230.         // Set desired parameter (note: the last parameter is not used anymore so put to 0.0f)
  231.         pSkeletonAnim->SetDesiredMotionParam(motionParamID, overriddenValue, 0.0f);
  232. }
  233.  
  234. void CAnimatedCharacter::SetBlendWeightParam(const EMotionParamID motionParamID, const float value, const uint8 targetFlags /* = eBWPT_All*/)
  235. {
  236.         if (motionParamID >= eMotionParamID_BlendWeight && motionParamID <= eMotionParamID_BlendWeight_Last)
  237.         {
  238.                 //only allowed on the generic blend weights params
  239.  
  240.                 IEntity* pEntity = GetEntity();
  241.                 CRY_ASSERT(pEntity);
  242.  
  243.                 if (targetFlags & eBWPT_FirstPersonSkeleton)
  244.                 {
  245.                         if (ICharacterInstance* pCharacterInstance = pEntity->GetCharacter(0))
  246.                         {
  247.                                 if (ISkeletonAnim* pSkeletonAnim = pCharacterInstance->GetISkeletonAnim())
  248.                                 {
  249.                                         pSkeletonAnim->SetDesiredMotionParam(motionParamID, value, 0.0f);
  250.                                 }
  251.                         }
  252.                 }
  253.  
  254.                 if (targetFlags & eBWPT_ShadowSkeleton)
  255.                 {
  256.                         if (m_hasShadowCharacter)
  257.                         {
  258.                                 if (ICharacterInstance* pShadowCharacter = pEntity->GetCharacter(m_shadowCharacterSlot))
  259.                                 {
  260.                                         if (ISkeletonAnim* pShadowSkeletonAnim = pShadowCharacter->GetISkeletonAnim())
  261.                                         {
  262.                                                 pShadowSkeletonAnim->SetDesiredMotionParam(motionParamID, value, 0.0f);
  263.                                         }
  264.                                 }
  265.                         }
  266.                 }
  267.  
  268.         }
  269.         else
  270.         {
  271.                 CryLogAlways("[CryAnimation] ERROR: motionParam that was sent: %d is not within allowed range of blendweights %d to %d", motionParamID, eMotionParamID_BlendWeight, eMotionParamID_BlendWeight_Last);
  272.         }
  273. }
  274.  
downloadAnimatedCharacterPPS_ParamCalculation.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top