BVB Source Codes

CRYENGINE Show VehicleSeatActionRotateBone.cpp Source code

Return Download CRYENGINE: download VehicleSeatActionRotateBone.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 "VehicleSeatActionRotateBone.h"
  5. #include "IVehicleSystem.h"
  6. #include "Vehicle.h"
  7. #include "VehicleSeat.h"
  8. #include <CryExtension/CryCreateClassInstance.h>
  9.  
  10. CVehicleSeatActionRotateBone::CVehicleSeatActionRotateBone() : m_pSeat(NULL)
  11.         , m_pVehicle(NULL)
  12.         , m_MoveBoneId(-1)
  13.         , m_boneRotation(0.f, 0.f, 0.f)
  14.         , m_prevBoneRotation(0.f, 0.f, 0.f)
  15.         , m_currentMovementRotation(IDENTITY)
  16.         , m_desiredBoneRotation(0.f, 0.f, 0.f)
  17.         , m_rotationSmoothingRate(0.f, 0.f, 0.f)
  18.         , m_soundRotSpeed(0.f)
  19.         , m_soundRotLastAppliedSpeed(-1.f)
  20.         , m_soundRotSpeedSmoothRate(0.f)
  21.         , m_soundRotSpeedSmoothTime(0.f)
  22.         , m_soundRotSpeedScalar(1.0f)
  23.         , m_pitchLimitMin(-1.f)
  24.         , m_pitchLimitMax(1.f)
  25.         , m_settlePitch(0.f)
  26.         , m_settleDelay(0.f)
  27.         , m_settleTime(1.f)
  28.         , m_noDriverTime(0.f)
  29.         , m_pAnimatedCharacter(NULL)
  30.         , m_autoAimStrengthMultiplier(1.0f)
  31.         , m_networkSluggishness(0.f)
  32.         , m_speedMultiplier(0.1f)
  33.         , m_rotationMultiplier(1.f)
  34.         //,m_rotatingSoundID(INVALID_SOUNDID)
  35.         , m_hasDriver(0)
  36.         , m_driverIsLocal(0)
  37.         , m_settleBoneOnExit(0)
  38.         , m_useRotationSounds(0)
  39.         , m_stopCurrentSound(0)
  40.         , m_rotationLockCount(0)
  41. {
  42.         CryCreateClassInstance("AnimationPoseModifier_OperatorQueue", m_poseModifier);
  43. }
  44.  
  45. CVehicleSeatActionRotateBone::~CVehicleSeatActionRotateBone()
  46. {
  47.         m_pVehicle->UnregisterVehicleEventListener(this);
  48. }
  49.  
  50. bool CVehicleSeatActionRotateBone::Init(IVehicle* pVehicle, IVehicleSeat* pSeat, const CVehicleParams& table)
  51. {
  52.         m_pVehicle = pVehicle;
  53.         m_pSeat = pSeat;
  54.  
  55.         IDefaultSkeleton* pIDefaultSkeleton = GetCharacterModelSkeleton();
  56.         if (pIDefaultSkeleton)
  57.         {
  58.                 if (table.haveAttr("MoveBone"))
  59.                 {
  60.                         const char* boneName = table.getAttr("MoveBone");
  61.                         m_MoveBoneId = pIDefaultSkeleton->GetJointIDByName(boneName);
  62.                 }
  63.         }
  64.  
  65.         if (CVehicleParams baseOrientationTable = table.findChild("MoveBoneBaseOrientation"))
  66.         {
  67.                 float x, y, z;
  68.                 baseOrientationTable.getAttr("x", x);
  69.                 baseOrientationTable.getAttr("y", y);
  70.                 baseOrientationTable.getAttr("z", z);
  71.                 m_boneBaseOrientation.SetRotationXYZ(Ang3(x, y, z));
  72.         }
  73.         else
  74.         {
  75.                 m_boneBaseOrientation.SetIdentity();
  76.         }
  77.  
  78.         // Set the PitchLimits.
  79.         float degMin = -40.0f, degMax = 40.0f;
  80.         if (CVehicleParams pitchLimitParams = table.findChild("PitchLimit"))
  81.         {
  82.                 pitchLimitParams.getAttr("min", degMin);
  83.                 pitchLimitParams.getAttr("max", degMax);
  84.         }
  85.         m_pitchLimitMin = DEG2RAD(degMin);
  86.         m_pitchLimitMax = DEG2RAD(degMax);
  87.  
  88.         if (table.getAttr("AutoAimStrengthMultiplier", m_autoAimStrengthMultiplier)) { m_autoAimStrengthMultiplier = -fabs_tpl(m_autoAimStrengthMultiplier); }
  89.         table.getAttr("NetworkSluggishness", m_networkSluggishness);
  90.         table.getAttr("SpeedMultiplier", m_speedMultiplier);
  91.  
  92.         // Rotation Audio
  93.         m_useRotationSounds = 1;
  94.         const char* soundFP = NULL;
  95.         if (m_useRotationSounds &= table.getAttr("soundFP", &soundFP))
  96.         {
  97.                 m_soundNameFP = soundFP;
  98.         }
  99.         const char* soundTP = NULL;
  100.         if (m_useRotationSounds &= table.getAttr("soundTP", &soundTP))
  101.         {
  102.                 m_soundNameTP = soundTP;
  103.         }
  104.         const char* soundParam = NULL;
  105.         if (m_useRotationSounds &= table.getAttr("soundParam", &soundParam))
  106.         {
  107.                 m_soundParamRotSpeed = soundParam;
  108.         }
  109.  
  110.         float idealSpeed = 180.f;
  111.         table.getAttr("idealRotationSpeedForSound", idealSpeed);
  112.         m_soundRotSpeedScalar = (180.0f / gf_PI) * __fres((float)__fsel(-idealSpeed, 180.f, idealSpeed));
  113.         table.getAttr("soundRotSpeedSmoothTime", m_soundRotSpeedSmoothTime);
  114.  
  115.         if (table.getAttr("settlePitch", m_settlePitch))
  116.         {
  117.                 m_settleBoneOnExit = 1;
  118.                 m_settlePitch = DEG2RAD(m_settlePitch);
  119.         }
  120.         m_settleBoneOnExit |= table.getAttr("settleDelay", m_settleDelay);
  121.         m_settleBoneOnExit |= table.getAttr("settleTime", m_settleTime);
  122.  
  123.         //Rotate the bone rotation to the initial direction of the pinger, as the rotation for the bone is in world space
  124.         m_boneRotation = m_boneRotation + Ang3(pVehicle->GetEntity()->GetWorldRotation());
  125.         m_prevBoneRotation = m_boneRotation;
  126.  
  127.         m_pVehicle->SetObjectUpdate(this, IVehicle::eVOU_AlwaysUpdate);
  128.  
  129.         return true;
  130. }
  131.  
  132. void CVehicleSeatActionRotateBone::Reset()
  133. {
  134.         // Enable PrePhysUpdate.
  135.         m_pVehicle->GetGameObject()->EnablePrePhysicsUpdate(ePPU_Always);
  136. }
  137.  
  138. void CVehicleSeatActionRotateBone::OnAction(const TVehicleActionId actionId, int activationMode, float value)
  139. {
  140.         bool aspectChanged = false;
  141.         if (actionId == eVAI_RotateYaw || actionId == eVAI_RotateYawAimAssist)
  142.         {
  143.                 value *= actionId == eVAI_RotateYawAimAssist ? m_autoAimStrengthMultiplier * m_rotationMultiplier * m_speedMultiplier : m_rotationMultiplier * m_speedMultiplier;
  144.  
  145.                 m_boneRotation.z -= value;
  146.  
  147.                 const static float kThreshold = DEG2RAD(360.f);
  148.                 const float fWrappedRotation = (float)__fsel(m_boneRotation.z, m_boneRotation.z - kThreshold, m_boneRotation.z + kThreshold);
  149.                 const float fNewRot = (float)__fsel(fabsf(m_boneRotation.z) - kThreshold, fWrappedRotation, m_boneRotation.z);
  150.                 aspectChanged = fabs_tpl(m_boneRotation.z - fNewRot) > 0.f;
  151.                 m_boneRotation.z = fNewRot;
  152.         }
  153.         else if (actionId == eVAI_RotatePitch || actionId == eVAI_RotatePitchAimAssist)
  154.         {
  155.                 value *= actionId == eVAI_RotatePitchAimAssist ? m_autoAimStrengthMultiplier * m_rotationMultiplier * m_speedMultiplier : m_rotationMultiplier * m_speedMultiplier;
  156.  
  157.                 float fNewRot = clamp_tpl(m_boneRotation.x - value, m_pitchLimitMin, m_pitchLimitMax);
  158.                 aspectChanged = fabs_tpl(m_boneRotation.x - fNewRot) > 0.f;
  159.                 m_boneRotation.x = fNewRot;
  160.         }
  161.  
  162.         if (aspectChanged)
  163.         {
  164.                 static_cast<CVehicleSeat*>(m_pSeat)->ChangedNetworkState(CVehicle::ASPECT_SEAT_ACTION);
  165.         }
  166. }
  167.  
  168. void CVehicleSeatActionRotateBone::StartUsing(EntityId passengerId)
  169. {
  170.         m_pVehicle->RegisterVehicleEventListener(this, "CVehicleSeatActionRotateBone");
  171.         m_hasDriver = 1;
  172.         m_driverIsLocal = (gEnv->pGameFramework->GetClientActorId() == passengerId);
  173.         m_rotationMultiplier = 1.f;
  174.         m_rotationLockCount = 0;
  175.         m_stopCurrentSound = 1;
  176. }
  177.  
  178. void CVehicleSeatActionRotateBone::StopUsing()
  179. {
  180.         m_pVehicle->UnregisterVehicleEventListener(this);
  181.         m_hasDriver = 0;
  182.         m_driverIsLocal = 0;
  183.         m_noDriverTime = 0.f;
  184.         m_stopCurrentSound = 1;
  185. }
  186.  
  187. void CVehicleSeatActionRotateBone::PrePhysUpdate(const float dt)
  188. {
  189.         IEntity* pEntity = m_pVehicle->GetEntity();
  190.         if (!pEntity)
  191.         {
  192.                 return;
  193.         }
  194.  
  195.         if (pEntity->IsHidden() || pEntity->IsInvisible())
  196.         {
  197.                 m_boneRotation.x = m_settlePitch;
  198.                 m_desiredBoneRotation = m_boneRotation;
  199.                 return;
  200.         }
  201.  
  202.         if (m_hasDriver)
  203.         {
  204.                 if (!m_driverIsLocal)
  205.                 {
  206.                         // Smooth the networked rotation.
  207.                         m_boneRotation.z += (float)__fsel((m_desiredBoneRotation.z - m_boneRotation.z) - gf_PI, gf_PI2, 0.f);
  208.                         m_boneRotation.z -= (float)__fsel((m_boneRotation.z - m_desiredBoneRotation.z) - gf_PI, gf_PI2, 0.f);
  209.                         SmoothCD<Ang3>(m_boneRotation, m_rotationSmoothingRate, dt, m_desiredBoneRotation, m_networkSluggishness);
  210.                 }
  211.                 else
  212.                 {
  213.                         m_desiredBoneRotation = m_boneRotation;
  214.                 }
  215.         }
  216.         else
  217.         {
  218.                 if (!m_rotationLockCount)
  219.                 {
  220.                         m_noDriverTime += dt;
  221.                         if (m_settleBoneOnExit && m_noDriverTime > m_settleDelay)
  222.                         {
  223.                                 // When there is no Driver, locally smooth to specified pitch.
  224.                                 Ang3 target(m_settlePitch, m_boneRotation.y, m_boneRotation.z);
  225.                                 SmoothCD<Ang3>(m_boneRotation, m_rotationSmoothingRate, dt, target, m_settleTime);
  226.                         }
  227.                 }
  228.                 m_desiredBoneRotation = m_boneRotation;
  229.         }
  230.  
  231.         IAnimationPoseModifierPtr modPtr = m_poseModifier;
  232.         pEntity->GetCharacter(0)->GetISkeletonAnim()->PushPoseModifier(2, modPtr, "VehicleBoneRotate");
  233.  
  234.         const Quat vehicleRotation(pEntity->GetWorldRotation());
  235.         const Quat headOrientation(Quat::CreateRotationXYZ(m_boneRotation));
  236.  
  237.         Quat finalheadOrientation = vehicleRotation.GetInverted() * m_currentMovementRotation.GetInverted() * headOrientation * m_boneBaseOrientation;
  238.         m_poseModifier->PushOrientation(m_MoveBoneId, IAnimationOperatorQueue::eOp_Override, finalheadOrientation);
  239.         m_currentMovementRotation.SetIdentity();
  240.  
  241.         // Update Sounds.
  242.         UpdateSound(dt);
  243.  
  244.         // Store Prev Rotation.
  245.         m_prevBoneRotation = m_boneRotation;
  246. }
  247.  
  248. void CVehicleSeatActionRotateBone::UpdateSound(const float dt)
  249. {
  250.         if (!m_useRotationSounds)
  251.                 return;
  252.  
  253.         IEntity* pEntity = m_pVehicle->GetEntity();
  254.         if (IEntityAudioComponent* pIEntityAudioComponent = pEntity->GetComponent<IEntityAudioComponent>())
  255.         {
  256.                 if (m_stopCurrentSound)
  257.                 {
  258.                         m_stopCurrentSound = 0;
  259.                         //StopSound(*pIEntityAudioComponent, m_rotatingSoundID);
  260.                 }
  261.  
  262.                 // Calculate Rotation Speed for the Audio.
  263.                 if (dt > FLT_EPSILON)
  264.                 {
  265.                         const float deltaPitch = (m_boneRotation.x - m_prevBoneRotation.x);
  266.                         float deltaYaw = fabs_tpl(m_boneRotation.z - m_prevBoneRotation.z);
  267.                         deltaYaw = (float)__fsel(deltaYaw - gf_PI, gf_PI2 - deltaYaw, deltaYaw);
  268.                         const float rotDist = sqrt_tpl((deltaYaw * deltaYaw) + (deltaPitch * deltaPitch));
  269.                         const float invTime = __fres(dt);
  270.                         const float rotSpeed = invTime * rotDist;
  271.                         const float prevSpeed = m_soundRotLastAppliedSpeed;
  272.                         m_soundRotSpeed = (float)__fsel(rotSpeed - prevSpeed, rotSpeed, m_soundRotSpeed);
  273.                         SmoothCD(m_soundRotSpeed, m_soundRotSpeedSmoothRate, dt, rotSpeed, m_soundRotSpeedSmoothTime);
  274.                         if (fabs_tpl(prevSpeed - m_soundRotSpeed) > 0.001f)
  275.                         {
  276.                                 const float rotationRate = clamp_tpl(m_soundRotSpeed * m_soundRotSpeedScalar, 0.f, 10.f);
  277.                                 //SetSoundParam(*pIEntityAudioComponent, m_rotatingSoundID, m_soundParamRotSpeed.c_str(), rotationRate);
  278.                                 m_soundRotLastAppliedSpeed = m_soundRotSpeed;
  279.                         }
  280.                 }
  281.  
  282.                 REINST("vehicle sounds");
  283.                 //if(m_soundRotSpeed>0.001f)
  284.                 //{
  285.                 //      bool isNotPlaying = m_rotatingSoundID==INVALID_SOUNDID;
  286.                 //      if(!isNotPlaying)
  287.                 //      {
  288.                 //              if(ISound* pSound = pIEntityAudioComponent->GetSound(m_rotatingSoundID))
  289.                 //              {
  290.                 //                      isNotPlaying = !pSound->IsPlaying();
  291.                 //              }
  292.                 //              else
  293.                 //              {
  294.                 //                      isNotPlaying = true;
  295.                 //              }
  296.                 //      }
  297.  
  298.                 //      // Play the sound...
  299.                 //      if(isNotPlaying)
  300.                 //      {
  301.                 //              IActor* pDriver = m_pVehicle->GetDriver();
  302.                 //              const bool firstPerson = pDriver && pDriver->IsClient();
  303.                 //              m_rotatingSoundID=PlaySound(*pIEntityAudioComponent, firstPerson?m_soundNameFP.c_str():m_soundNameTP.c_str());
  304.                 //      }
  305.                 //}
  306.         }
  307. }
  308.  
  309. void CVehicleSeatActionRotateBone::GetMemoryUsage(ICrySizer* s) const
  310. {
  311.         s->AddObject(this, sizeof(*this));
  312. }
  313.  
  314. IDefaultSkeleton* CVehicleSeatActionRotateBone::GetCharacterModelSkeleton() const
  315. {
  316.         if (ICharacterInstance* pCharacterInstance = m_pVehicle->GetEntity()->GetCharacter(0))
  317.         {
  318.                 return &pCharacterInstance->GetIDefaultSkeleton();
  319.         }
  320.  
  321.         return NULL;
  322. }
  323.  
  324. void CVehicleSeatActionRotateBone::Serialize(TSerialize ser, EEntityAspects aspects)
  325. {
  326.         if (ser.GetSerializationTarget() == eST_Network && (aspects & CVehicle::ASPECT_SEAT_ACTION))
  327.         {
  328.                 NET_PROFILE_SCOPE("SeatAction_RotateBone", ser.IsReading());
  329.  
  330.                 Vec3 rotation(m_boneRotation.x, m_boneRotation.y, m_boneRotation.z);
  331.  
  332.                 ser.Value("m_boneRotation", rotation, 'jmpv');
  333.  
  334.                 if (ser.IsReading())
  335.                 {
  336.                         m_desiredBoneRotation.Set(rotation.x, rotation.y, rotation.z);
  337.                 }
  338.         }
  339. }
  340.  
  341. void CVehicleSeatActionRotateBone::OnVehicleEvent(EVehicleEvent event, const SVehicleEventParams& params)
  342. {
  343.         if (event == eVE_LockBoneRotation)
  344.         {
  345.                 const bool isLocking = params.bParam;
  346.                 const float pitch = params.fParam;
  347.                 if (isLocking)
  348.                 {
  349.                         if (pitch >= m_pitchLimitMin && pitch <= m_pitchLimitMax)
  350.                         {
  351.                                 m_boneRotation.x = params.fParam;
  352.                         }
  353.                         if (m_rotationLockCount < 3)
  354.                                 m_rotationLockCount++;
  355.                         m_rotationMultiplier = 0.f;
  356.                 }
  357.                 else
  358.                 {
  359.                         if (m_rotationLockCount > 0)
  360.                                 m_rotationLockCount--;
  361.                         m_rotationMultiplier = m_rotationLockCount ? 0.f : 1.f;
  362.                 }
  363.         }
  364.         else if (event == eVE_VehicleMovementRotation)
  365.         {
  366.                 const Quat currentRotation(params.fParam, params.vParam);
  367.                 m_currentMovementRotation = currentRotation;
  368.         }
  369.         else if (event == eVE_Destroyed || (event == eVE_Hide && params.iParam))
  370.         {
  371.                 IEntity* pEntity = m_pVehicle->GetEntity();
  372.                 if (IEntityAudioComponent* pIEntityAudioComponent = pEntity->GetComponent<IEntityAudioComponent>())
  373.                 {
  374.                         m_stopCurrentSound = 0;
  375.                         //StopSound(*pIEntityAudioComponent, m_rotatingSoundID);
  376.                 }
  377.         }
  378.  
  379. }
  380.  
  381. ///*static*/ tSoundID CVehicleSeatActionRotateBone::PlaySound( IEntityAudioComponent& rIEntityAudioComponent, const char * soundName )
  382. //{
  383. //      return rIEntityAudioComponent.PlaySoundEx(soundName, ZERO, FORWARD_DIRECTION, FLAG_SOUND_DEFAULT_3D, 0, 1.0f, 0, 0, eSoundSemantic_Weapon);
  384. //}
  385. //
  386. ///*static*/ void CVehicleSeatActionRotateBone::SetSoundParam(IEntityAudioComponent& rIEntityAudioComponent, tSoundID soundID, const char* param, float value)
  387. //{
  388. //      if(ISound* pSound = rIEntityAudioComponent.GetSound(soundID))
  389. //      {
  390. //              pSound->SetParam(param, value, false);
  391. //      }
  392. //}
  393. //
  394. ///*static*/ void CVehicleSeatActionRotateBone::StopSound( IEntityAudioComponent& rIEntityAudioComponent, tSoundID soundID )
  395. //{
  396. //      rIEntityAudioComponent.StopSound(soundID);
  397. //}
  398.  
  399. DEFINE_VEHICLEOBJECT(CVehicleSeatActionRotateBone);
  400.  
downloadVehicleSeatActionRotateBone.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