BVB Source Codes

CRYENGINE Show VehiclePartSuspensionPart.cpp Source code

Return Download CRYENGINE: download VehiclePartSuspensionPart.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.    -------------------------------------------------------------------------
  5.    $Id$
  6.    $DateTime$
  7.    Description: Implements a suspension part
  8.  
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 16:11:2011: Created by Stan Fichele
  12.  
  13. *************************************************************************/
  14. #include "StdAfx.h"
  15.  
  16. #include <CryAnimation/ICryAnimation.h>
  17. #include "IVehicleSystem.h"
  18.  
  19. #include "Vehicle.h"
  20. #include "VehiclePartBase.h"
  21. #include "VehiclePartAnimated.h"
  22. #include "VehiclePartSubPart.h"
  23. #include "VehiclePartSuspensionPart.h"
  24. #include <CryRenderer/IRenderer.h>
  25. #include <CryRenderer/IRenderAuxGeom.h>
  26.  
  27. CVehiclePartSuspensionPart::CVehiclePartSuspensionPart()
  28.         : m_animatedRoot(nullptr)
  29.         , m_targetPart(nullptr)
  30.         , m_initialRot(ZERO)
  31.         , m_targetOffset(ZERO)
  32.         , m_pos0(ZERO)
  33.         , m_direction0(ZERO)
  34.         , m_invLength0(0.0f)
  35.         , m_jointId(0)
  36.         , m_mode(0)
  37.         , m_ikFlags(0)
  38. {
  39. }
  40.  
  41. CVehiclePartSuspensionPart::~CVehiclePartSuspensionPart()
  42. {
  43. }
  44.  
  45. bool CVehiclePartSuspensionPart::Init(IVehicle* pVehicle, const CVehicleParams& table, IVehiclePart* pParent, CVehicle::SPartInitInfo& initInfo, int partType)
  46. {
  47.         if (!inherited::Init(pVehicle, table, pParent, initInfo, partType))
  48.                 return false;
  49.  
  50.         m_animatedRoot = CAST_VEHICLEOBJECT(CVehiclePartAnimated, GetParent(true));
  51.         m_jointId = -1;
  52.         m_ikFlags = 0;
  53.  
  54.         if (m_animatedRoot)
  55.         {
  56.                 if (CVehicleParams subPartTable = table.findChild("SubPart"))
  57.                 {
  58.                         // We need to remove this part from the animated root, otherwise we have two parts
  59.                         // NB: for now we are not doing anything with the physics - infact its preferable
  60.                         // if we dont have physics on suspension arms!
  61.                         const char* geoName = subPartTable.getAttr("geometryname");
  62.                         ICharacterInstance* pCharInstance = m_animatedRoot->GetEntity()->GetCharacter(m_animatedRoot->GetSlot());
  63.                         if (pCharInstance)
  64.                         {
  65.                                 IDefaultSkeleton& rIDefaultSkeleton = pCharInstance->GetIDefaultSkeleton();
  66.                                 ISkeletonPose* pSkeletonPose = pCharInstance->GetISkeletonPose();
  67.                                 m_jointId = rIDefaultSkeleton.GetJointIDByName(geoName);
  68.                                 pSkeletonPose->SetStatObjOnJoint(m_jointId, NULL);
  69.                         }
  70.                 }
  71.         }
  72.         else
  73.         {
  74.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "CVehiclePartSuspensionPart: needs to have an AnimatedPart as a parent!");
  75.                 return false;
  76.         }
  77.  
  78.         CVehicleParams ikTable = table.findChild("IK");
  79.         if (!ikTable)
  80.                 return false;
  81.  
  82.         const char* targetPartName = ikTable.getAttr("target");
  83.         m_targetPart = static_cast<CVehiclePartBase*>(pVehicle->GetPart(targetPartName));
  84.         if (m_targetPart == NULL)
  85.         {
  86.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "CVehiclePartSuspensionPart: couldn't find target part: '%s'", targetPartName);
  87.                 return false;
  88.         }
  89.  
  90.         // Set up the target
  91.         m_targetOffset.zero();
  92.         const char* targetHelper = ikTable.getAttr("targetHelper");
  93.         if (targetHelper && targetHelper[0])
  94.         {
  95.                 if (IVehicleHelper* pHelper = m_pVehicle->GetHelper(targetHelper))
  96.                 {
  97.                         // NB: this is in vehicle space, and needs translating in PostInit()
  98.                         m_targetOffset = pHelper->GetLocalTM().GetTranslation();
  99.                         m_ikFlags |= k_flagTargetHelper;
  100.                 }
  101.         }
  102.         Vec3 offset(0);
  103.         ikTable.getAttr("offset", offset);
  104.         m_targetOffset += offset;
  105.  
  106.         m_mode = k_modeStretch;
  107.         const char* mode = ikTable.getAttr("mode");
  108.         if (strcmp(mode, "rotate") == 0)
  109.                 m_mode = k_modeRotate;
  110.         if (strcmp(mode, "stretch") == 0)
  111.                 m_mode = k_modeStretch;
  112.         if (strcmp(mode, "snap") == 0)
  113.                 m_mode = k_modeSnapToEF;
  114.  
  115.         bool bIgnoreTargetRotation = 0;
  116.         ikTable.getAttr("ignoreTargetRotation", bIgnoreTargetRotation);
  117.         if (bIgnoreTargetRotation)
  118.                 m_ikFlags |= k_flagIgnoreTargetRotation;
  119.  
  120.         return true;
  121. }
  122.  
  123. void CVehiclePartSuspensionPart::PostInit()
  124. {
  125.         const Matrix34& tm = GetLocalTM(false);
  126.         const Matrix34& targetTm = m_targetPart->GetLocalTM(false);
  127.         if (m_ikFlags & k_flagTargetHelper)
  128.                 m_targetOffset = targetTm.GetInverted() * m_targetOffset;
  129.         m_pos0 = tm.GetTranslation();
  130.         Vec3 targetPos = targetTm * m_targetOffset;
  131.         m_direction0 = targetPos - m_pos0;
  132.         float length = m_direction0.GetLength();
  133.         if (length > 1e-2f)
  134.         {
  135.                 m_invLength0 = 1.f / length;
  136.                 m_direction0 = m_direction0 / length;
  137.         }
  138.         else
  139.         {
  140.                 m_invLength0 = 0.f;
  141.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "CVehiclePartSuspensionPart: inavlid suspension IK setup target is TOO close");
  142.         }
  143.         m_initialRot = Quat(tm);
  144. }
  145.  
  146. void CVehiclePartSuspensionPart::OnEvent(const SVehiclePartEvent& event)
  147. {
  148.         inherited::OnEvent(event);
  149.         m_pVehicle->SetObjectUpdate(this, IVehicle::eVOU_AlwaysUpdate);
  150. }
  151.  
  152. void CVehiclePartSuspensionPart::Reset()
  153. {
  154.         inherited::Reset();
  155. }
  156.  
  157. void CVehiclePartSuspensionPart::Release()
  158. {
  159.         inherited::Release();
  160. }
  161.  
  162. bool CVehiclePartSuspensionPart::ChangeState(EVehiclePartState state, int flags)
  163. {
  164.         if (!inherited::ChangeState(state, flags))
  165.                 return false;
  166.  
  167.         // Remove the statobj from the cga joint (again)
  168.         if (m_animatedRoot && m_jointId >= 0)
  169.         {
  170.                 ICharacterInstance* pCharInstance = m_animatedRoot->GetEntity()->GetCharacter(m_animatedRoot->GetSlot());
  171.                 if (pCharInstance)
  172.                 {
  173.                         ISkeletonPose* pSkeletonPose = pCharInstance->GetISkeletonPose();
  174.                         pSkeletonPose->SetStatObjOnJoint(m_jointId, NULL);
  175.                 }
  176.         }
  177.         return true;
  178. }
  179.  
  180. void CVehiclePartSuspensionPart::Physicalize()
  181. {
  182.         inherited::Physicalize();
  183. }
  184.  
  185. void CVehiclePartSuspensionPart::Update(const float frameTime)
  186. {
  187.         inherited::Update(frameTime);
  188.  
  189.         const Matrix34& parentTm = m_pParentPart->GetLocalTM(false);
  190.         const Matrix34& targetTm = m_targetPart->GetLocalTM(false);
  191.  
  192.         Vec3 pos = parentTm * m_pos0;
  193.         Vec3 targetPos = (m_ikFlags & k_flagIgnoreTargetRotation) ? (targetTm.GetColumn3() + m_targetOffset) : (targetTm * m_targetOffset);
  194.         Vec3 dir = targetPos - pos;
  195.         float length = dir.GetLength();
  196.         if (length > 1e-2f)
  197.         {
  198.                 Matrix33 rot = Matrix33::CreateRotationV0V1(m_direction0, dir * (1.f / length));
  199.                 Matrix33 partRot = rot * Matrix33(m_initialRot);
  200.  
  201.                 if (m_mode == k_modeRotate || m_mode == k_modeSnapToEF)
  202.                 {
  203.                         if (m_mode == k_modeSnapToEF)
  204.                         {
  205.                                 pos = targetPos - rot * m_direction0;
  206.                         }
  207.                         Matrix34 tm(partRot, pos);
  208.                         SetLocalTM(tm);
  209.                 }
  210.                 else if (m_mode == k_modeStretch)
  211.                 {
  212.                         const float scale = length * m_invLength0;
  213.                         const Vec3 z = m_direction0;
  214.                         const Vec3 sz = m_direction0 * (scale - 1.f);
  215.                         Matrix33 scaleM;
  216.                         scaleM.m00 = 1.f + sz.x * z.x;
  217.                         scaleM.m01 = sz.y * z.x;
  218.                         scaleM.m02 = sz.z * z.x;
  219.                         scaleM.m10 = sz.x * z.y;
  220.                         scaleM.m11 = 1.f + sz.y * z.y;
  221.                         scaleM.m12 = sz.z * z.y;
  222.                         scaleM.m20 = sz.x * z.z;
  223.                         scaleM.m21 = sz.y * z.z;
  224.                         scaleM.m22 = 1.f + sz.z * z.z;
  225.                         Matrix34 tm(partRot * scaleM, pos);
  226.                         SetLocalTM(tm);
  227.                 }
  228.         }
  229.  
  230. #if !defined(_RELEASE)
  231.         if (VehicleCVars().v_debugSuspensionIK)
  232.         {
  233.                 IRenderAuxGeom* pAuxGeom = gEnv->pRenderer->GetIRenderAuxGeom();
  234.                 SAuxGeomRenderFlags flags = pAuxGeom->GetRenderFlags();
  235.                 SAuxGeomRenderFlags oldFlags = pAuxGeom->GetRenderFlags();
  236.                 flags.SetDepthWriteFlag(e_DepthWriteOff);
  237.                 flags.SetDepthTestFlag(e_DepthTestOff);
  238.                 pAuxGeom->SetRenderFlags(flags);
  239.  
  240.                 ColorB colRed(255, 0, 0, 255);
  241.                 ColorB colBlue(0, 0, 255, 255);
  242.                 ColorB colWhite(255, 255, 255, 255);
  243.                 ColorB colGreen(0, 255, 0, 255);
  244.  
  245.                 pos = m_pVehicle->GetEntity()->GetWorldTM() * pos;
  246.                 targetPos = m_pVehicle->GetEntity()->GetWorldTM() * targetPos;
  247.                 pAuxGeom->DrawSphere(pos, 0.02f, colGreen);
  248.                 pAuxGeom->DrawSphere(targetPos, 0.02f, colBlue);
  249.                 pAuxGeom->DrawLine(pos, colWhite, targetPos, colWhite);
  250.         }
  251. #endif
  252. }
  253.  
  254. DEFINE_VEHICLEOBJECT(CVehiclePartSuspensionPart);
  255.  
downloadVehiclePartSuspensionPart.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