BVB Source Codes

CRYENGINE Show VehicleSeatActionOrientateBoneToView.cpp Source code

Return Download CRYENGINE: download VehicleSeatActionOrientateBoneToView.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 "VehicleSeatActionOrientateBoneToView.h"
  5. #include "IVehicleSystem.h"
  6. #include "Vehicle.h"
  7. #include "VehicleSeat.h"
  8. #include <CryExtension/CryCreateClassInstance.h>
  9.  
  10. CVehicleSeatActionOrientateBoneToView::CVehicleSeatActionOrientateBoneToView() : m_pSeat(NULL)
  11.         , m_pVehicle(NULL)
  12.         , m_MoveBoneId(-1)
  13.         , m_LookBoneId(-1)
  14.         , m_Sluggishness(0.f)
  15.         , m_BoneOrientationAngles(0.f, 0.f, 0.f)
  16.         , m_BoneSmoothingRate(0.f, 0.f, 0.f)
  17.         , m_pAnimatedCharacter(NULL)
  18. {
  19.         CryCreateClassInstance("AnimationPoseModifier_OperatorQueue", m_poseModifier);
  20. }
  21.  
  22. bool CVehicleSeatActionOrientateBoneToView::Init(IVehicle* pVehicle, IVehicleSeat* pSeat, const CVehicleParams& table)
  23. {
  24.         m_pVehicle = pVehicle;
  25.         m_pSeat = pSeat;
  26.  
  27.         IDefaultSkeleton& rIDefaultSkeleton = *GetCharacterModelSkeleton();
  28.         {
  29.                 if (table.haveAttr("MoveBone"))
  30.                 {
  31.                         const char* boneName = table.getAttr("MoveBone");
  32.                         m_MoveBoneId = rIDefaultSkeleton.GetJointIDByName(boneName);
  33.                 }
  34.  
  35.                 if (table.haveAttr("LookBone"))
  36.                 {
  37.                         const char* boneName = table.getAttr("LookBone");
  38.                         m_LookBoneId = rIDefaultSkeleton.GetJointIDByName(boneName);
  39.                 }
  40.         }
  41.  
  42.         if (table.haveAttr("Sluggishness"))
  43.         {
  44.                 table.getAttr("Sluggishness", m_Sluggishness);
  45.         }
  46.  
  47.         if (CVehicleParams baseOrientationTable = table.findChild("MoveBoneBaseOrientation"))
  48.         {
  49.                 float x, y, z;
  50.                 baseOrientationTable.getAttr("x", x);
  51.                 baseOrientationTable.getAttr("y", y);
  52.                 baseOrientationTable.getAttr("z", z);
  53.  
  54.                 m_BoneBaseOrientation = Quat::CreateRotationXYZ(Ang3(x, y, z));
  55.         }
  56.         else
  57.         {
  58.                 m_BoneBaseOrientation.SetIdentity();
  59.         }
  60.  
  61.         return true;
  62. }
  63.  
  64. void CVehicleSeatActionOrientateBoneToView::Reset()
  65. {
  66.         // Enable PrePhysUpdate.
  67.         m_pVehicle->GetGameObject()->EnablePrePhysicsUpdate(ePPU_Always);
  68. };
  69.  
  70. void CVehicleSeatActionOrientateBoneToView::StartUsing(EntityId passengerId)
  71. {
  72.         m_pVehicle->SetObjectUpdate(this, IVehicle::eVOU_AlwaysUpdate);
  73. }
  74.  
  75. void CVehicleSeatActionOrientateBoneToView::StopUsing()
  76. {
  77.         m_pVehicle->SetObjectUpdate(this, IVehicle::eVOU_NoUpdate);
  78. }
  79.  
  80. void CVehicleSeatActionOrientateBoneToView::PrePhysUpdate(const float deltaTime)
  81. {
  82.         Vec3 lookPos = GetCurrentLookPosition();
  83.         Vec3 aimPos = GetDesiredAimPosition();
  84.  
  85.         const Ang3 desiredViewAngles = GetDesiredViewAngles(lookPos, aimPos);
  86.  
  87.         //Rotating between -Pi and Pi and need to catch the cross over from one to the other to make sure smoothing is in correct direction
  88.         float yawDif = m_BoneOrientationAngles.z - desiredViewAngles.z;
  89.         if (fabs(yawDif) > gf_PI)
  90.         {
  91.                 m_BoneOrientationAngles.z += (float)__fsel(m_BoneOrientationAngles.z, -2.f * gf_PI, 2.f * gf_PI);
  92.         }
  93.  
  94.         SmoothCD<Ang3>(m_BoneOrientationAngles, m_BoneSmoothingRate, deltaTime, desiredViewAngles, m_Sluggishness);
  95.  
  96.         IAnimationPoseModifierPtr modPtr = m_poseModifier;
  97.         m_pVehicle->GetEntity()->GetCharacter(0)->GetISkeletonAnim()->PushPoseModifier(2, modPtr, "VehicleSeat");
  98.  
  99.         Quat vehicleRotation = m_pVehicle->GetEntity()->GetWorldRotation();
  100.  
  101.         Quat headOrientation;
  102.         headOrientation.SetRotationXYZ(m_BoneOrientationAngles);
  103.  
  104.         Quat finalheadOrientation = vehicleRotation.GetInverted() * headOrientation * m_BoneBaseOrientation;
  105.         m_poseModifier->PushOrientation(m_MoveBoneId, IAnimationOperatorQueue::eOp_Override, finalheadOrientation);
  106. }
  107.  
  108. Ang3 CVehicleSeatActionOrientateBoneToView::GetDesiredViewAngles(const Vec3& lookPos, const Vec3& aimPos) const
  109. {
  110.         Vec3 forwardDir = (aimPos - lookPos).GetNormalized();
  111.         Vec3 upDir = Vec3(0.f, 0.f, 1.f);
  112.         Vec3 sideDir = forwardDir.Cross(upDir);
  113.         sideDir.Normalize();
  114.         upDir = sideDir.Cross(forwardDir);
  115.         upDir.Normalize();
  116.  
  117.         Matrix34 matrix;
  118.         matrix.SetFromVectors(sideDir, forwardDir, upDir, Vec3(0.f, 0.f, 0.f));
  119.  
  120.         Ang3 lookAngles;
  121.         lookAngles.SetAnglesXYZ(matrix);
  122.  
  123.         return lookAngles;
  124. }
  125.  
  126. Vec3 CVehicleSeatActionOrientateBoneToView::GetDesiredAimPosition() const
  127. {
  128.         IActor* pActor = m_pVehicle->GetDriver();
  129.         if (pActor && pActor->IsClient())
  130.         {
  131.                 const CCamera& camera = gEnv->pSystem->GetViewCamera();
  132.                 return camera.GetPosition() + (camera.GetViewdir() * 100.f);
  133.         }
  134.  
  135.         return m_pVehicle->GetEntity()->GetWorldTM().GetColumn1();
  136. }
  137.  
  138. Vec3 CVehicleSeatActionOrientateBoneToView::GetCurrentLookPosition() const
  139. {
  140.         ISkeletonPose* pSkeleton = GetSkeleton();
  141.         CRY_ASSERT_MESSAGE(pSkeleton, "CVehicleSeatActionOrientateBoneToView::GetCurrentLookPosition - Couldn't get ISkeletonPose from vehicle entity");
  142.  
  143.         QuatT lookQuat = pSkeleton->GetAbsJointByID(m_LookBoneId);
  144.         return m_pVehicle->GetEntity()->GetWorldPos() + lookQuat.t;
  145. }
  146.  
  147. void CVehicleSeatActionOrientateBoneToView::GetMemoryUsage(ICrySizer* s) const
  148. {
  149.         s->AddObject(this, sizeof(*this));
  150. }
  151.  
  152. IDefaultSkeleton* CVehicleSeatActionOrientateBoneToView::GetCharacterModelSkeleton() const
  153. {
  154.         if (ICharacterInstance* pCharacterInstance = m_pVehicle->GetEntity()->GetCharacter(0))
  155.         {
  156.                 return &pCharacterInstance->GetIDefaultSkeleton();
  157.         }
  158.  
  159.         return NULL;
  160. }
  161. ISkeletonPose* CVehicleSeatActionOrientateBoneToView::GetSkeleton() const
  162. {
  163.         if (ICharacterInstance* pCharacterInstance = m_pVehicle->GetEntity()->GetCharacter(0))
  164.         {
  165.                 return pCharacterInstance->GetISkeletonPose();
  166.         }
  167.  
  168.         return NULL;
  169. }
  170.  
  171. DEFINE_VEHICLEOBJECT(CVehicleSeatActionOrientateBoneToView);
  172.  
downloadVehicleSeatActionOrientateBoneToView.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