BVB Source Codes

CRYENGINE Show VehicleSeatActionSteeringWheel.cpp Source code

Return Download CRYENGINE: download VehicleSeatActionSteeringWheel.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 seat action for the steering wheel
  8.  
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 28:11:2005: Created by Mathieu Pinard
  12.    - 14:06:2012: Re-factored to work with CryMannequin by Benito Gangoso Rodriguez
  13.  
  14. *************************************************************************/
  15. #include "StdAfx.h"
  16. #include "CryAction.h"
  17. #include "IActorSystem.h"
  18. #include "IVehicleSystem.h"
  19. #include "Vehicle.h"
  20. #include "VehiclePartBase.h"
  21. #include "VehiclePartAnimatedJoint.h"
  22. #include "VehiclePartSubPartWheel.h"
  23. #include "VehicleSeatActionSteeringWheel.h"
  24.  
  25. DEFINE_SHARED_PARAMS_TYPE_INFO(CVehicleSeatActionSteeringWheel::SSharedParams);
  26.  
  27. // Suppressed uninitMemberVar warnings due to use of BEGIN_SHARED_PARAMS macro
  28. // cppcheck-suppress uninitMemberVar
  29. CVehicleSeatActionSteeringWheel::CVehicleSeatActionSteeringWheel()
  30.         : m_isBeingUsed(false)
  31.         , m_isUsedSerialization(false)
  32.         , m_steeringActions(ZERO)
  33.         , m_steeringValues(ZERO)
  34.         , m_wheelInvRotationMax(0.0f)
  35.         , m_currentSteering(0.0f)
  36.         , m_jitterOffset(0.0f)
  37.         , m_jitterSuspensionResponse(0.0f)
  38. {
  39. }
  40.  
  41. //------------------------------------------------------------------------
  42. bool CVehicleSeatActionSteeringWheel::Init(IVehicle* pVehicle, IVehicleSeat* pSeat, const CVehicleParams& table)
  43. {
  44.         m_pVehicle = pVehicle;
  45.  
  46.         m_userId = 0;
  47.  
  48.         CVehicleParams steeringWheelTable = table.findChild("SteeringWheel");
  49.         if (!steeringWheelTable)
  50.                 return false;
  51.  
  52.         CryFixedStringT<256> sharedParamsName;
  53.         IEntity* pEntity = pVehicle->GetEntity();
  54.         ISharedParamsManager* pSharedParamsManager = gEnv->pGameFramework->GetISharedParamsManager();
  55.         CRY_ASSERT(pSharedParamsManager);
  56.         sharedParamsName.Format("%s::%s::CVehicleSeatActionSteeringWheel", pEntity->GetClass()->GetName(), pVehicle->GetModification());
  57.         m_pSharedParams = CastSharedParamsPtr<SSharedParams>(pSharedParamsManager->Get(sharedParamsName));
  58.  
  59.         if (!m_pSharedParams)
  60.         {
  61.                 SSharedParams params;
  62.  
  63.                 params.steeringForce = 1.0f;
  64.                 params.steeringRelaxMult = 1.0f;
  65.  
  66.                 params.jitterFreqLow = 0.f;
  67.                 params.jitterFreqHi = 0.f;
  68.                 params.jitterAmpLow = 0.f;
  69.                 params.jitterAmpHi = 0.f;
  70.                 params.jitterSuspAmp = 0.f;
  71.                 params.jitterSuspResponse = 0.f;
  72.  
  73.                 if (CVehicleParams subTable = steeringWheelTable.findChild("Actions"))
  74.                 {
  75.                         params.steeringClass = eSC_Generic;
  76.  
  77.                         subTable.getAttr("steeringRelaxMult", params.steeringRelaxMult);
  78.                         subTable.getAttr("steeringForce", params.steeringForce);
  79.                 }
  80.                 else if (CVehicleParams subTableCar = steeringWheelTable.findChild("Car"))
  81.                 {
  82.                         params.steeringClass = eSC_Car;
  83.  
  84.                         subTableCar.getAttr("jitterFreqLow", params.jitterFreqLow);
  85.                         subTableCar.getAttr("jitterFreqHi", params.jitterFreqHi);
  86.                         subTableCar.getAttr("jitterAmpLow", params.jitterAmpLow);
  87.                         subTableCar.getAttr("jitterAmpHi", params.jitterAmpHi);
  88.                         subTableCar.getAttr("jitterSuspAmp", params.jitterSuspAmp);
  89.                         subTableCar.getAttr("jitterSuspResponse", params.jitterSuspResponse);
  90.                 }
  91.                 else
  92.                 {
  93.                         GameWarning("Vehicle, SeatActionSteeringWheel needs to have either a <Car> or <Actions> node");
  94.                         return false;
  95.                 }
  96.  
  97.                 m_pSharedParams = CastSharedParamsPtr<SSharedParams>(pSharedParamsManager->Register(sharedParamsName, params));
  98.         }
  99.  
  100.         return true;
  101. }
  102.  
  103. //------------------------------------------------------------------------
  104. void CVehicleSeatActionSteeringWheel::Reset()
  105. {
  106.         if (m_userId != 0)
  107.         {
  108.                 StopUsing();
  109.         }
  110.         m_jitter.Setup(0.f, 0.f, 0x152f3);
  111.         m_jitterOffset = 0.f;
  112.         m_jitterSuspensionResponse = 0.f;
  113. }
  114.  
  115. //------------------------------------------------------------------------
  116. void CVehicleSeatActionSteeringWheel::StartUsing(EntityId passengerId)
  117. {
  118.         m_userId = passengerId;
  119.         m_isBeingUsed = true;
  120.  
  121.         if (m_pSharedParams->steeringClass == eSC_Generic)
  122.         {
  123.                 m_steeringValues.zero();
  124.                 m_steeringActions.zero();
  125.         }
  126.         else if (m_pSharedParams->steeringClass == eSC_Car)
  127.         {
  128.                 pe_params_car ppc;
  129.                 m_wheelInvRotationMax = 0.f;
  130.                 IPhysicalEntity* pPhysics = m_pVehicle->GetEntity()->GetPhysics();
  131.                 if (pPhysics && pPhysics->GetParams(&ppc) && ppc.maxSteer > DEG2RAD(1.f))
  132.                         m_wheelInvRotationMax = 1.f / ppc.maxSteer;
  133.         }
  134.  
  135.         m_pVehicle->SetObjectUpdate(this, IVehicle::eVOU_PassengerUpdate);
  136. }
  137.  
  138. //------------------------------------------------------------------------
  139. void CVehicleSeatActionSteeringWheel::StopUsing()
  140. {
  141.         if (m_isBeingUsed)
  142.         {
  143.                 m_pVehicle->SetObjectUpdate(this, IVehicle::eVOU_NoUpdate);
  144.         }
  145.  
  146.         m_userId = 0;
  147.         m_isBeingUsed = false;
  148. }
  149.  
  150. //------------------------------------------------------------------------
  151. void CVehicleSeatActionSteeringWheel::OnAction(const TVehicleActionId actionId, int activationMode, float value)
  152. {
  153.         if (m_pSharedParams->steeringClass == eSC_Generic)
  154.         {
  155.                 if (actionId == eVAI_TurnLeft)
  156.                 {
  157.                         m_steeringActions.y = -value;
  158.                 }
  159.                 else if (actionId == eVAI_TurnRight)
  160.                 {
  161.                         m_steeringActions.y = value;
  162.                 }
  163.         }
  164. }
  165.  
  166. //------------------------------------------------------------------------
  167. void CVehicleSeatActionSteeringWheel::Update(float frameTime)
  168. {
  169.         IActionController* pActionController = m_pVehicle->GetAnimationComponent().GetActionController();
  170.         const bool doUpdate = (m_userId != 0) && (m_isBeingUsed) && (pActionController != NULL);
  171.  
  172.         if (doUpdate)
  173.         {
  174.                 // NB: the 'steeringTime' param is set on the controller which will pass it onto the active actions
  175.                 // As the seat already has an idle action it will pass it to that
  176.  
  177.                 const SSharedParams* params = m_pSharedParams.get();
  178.  
  179.                 if (params->steeringClass == eSC_Car)
  180.                 {
  181.                         float offset = 0.f;
  182.  
  183.                         if (IVehicleMovement* pMovement = m_pVehicle->GetMovement())
  184.                         {
  185.                                 // The vehicle status, speed, accel etc
  186.                                 SVehicleMovementStatusGeneral status;
  187.                                 pMovement->GetStatus(&status);
  188.                                 SVehicleMovementStatusWheeled wheeledStatus;
  189.                                 pMovement->GetStatus(&wheeledStatus);
  190.  
  191.                                 if (status.speed > 2.f)
  192.                                 {
  193.                                         // Smooth the response, when increasing let it increase fast, when decreasing, decrease slowly
  194.                                         float suspensionResponse = clamp_tpl(wheeledStatus.suspensionCompressionRate * params->jitterSuspResponse, -1.f, +1.f);
  195.                                         float suspensionChange = (float)__fsel(fabsf(suspensionResponse) - fabsf(m_jitterSuspensionResponse), 30.f, 3.f);
  196.                                         m_jitterSuspensionResponse += (suspensionResponse - m_jitterSuspensionResponse) * approxOneExp(frameTime * suspensionChange);
  197.  
  198.                                         offset = params->jitterSuspAmp * m_jitterSuspensionResponse;
  199.  
  200.                                         float invTopSpeed = 1.f / (status.topSpeed + 1.f);
  201.                                         float speedNorm = min(1.f, status.speed * invTopSpeed);
  202.                                         float accelNorm = min(1.f, status.localAccel.GetLengthSquared() * sqr(invTopSpeed));
  203.  
  204.                                         float jitterAmp = params->jitterAmpLow + accelNorm * (params->jitterAmpHi - params->jitterAmpLow);
  205.                                         float jitterFreq = params->jitterFreqLow + m_jitterSuspensionResponse * (params->jitterFreqHi - params->jitterFreqLow);
  206.  
  207.                                         m_jitter.SetAmpFreq(jitterAmp, jitterFreq);
  208.                                         offset += m_jitter.Update(frameTime);
  209.                                 }
  210.                         }
  211.  
  212.                         m_jitterOffset += (offset - m_jitterOffset) * approxOneExp(frameTime * 30.f);
  213.  
  214.                         // Get the steering from the physics and add a small amount of filtering
  215.                         pe_status_vehicle psv;
  216.                         IPhysicalEntity* pPhysics = m_pVehicle->GetEntity()->GetPhysics();
  217.                         if (pPhysics && pPhysics->GetStatus(&psv))
  218.                                 m_currentSteering += (psv.steer * m_wheelInvRotationMax + m_jitterOffset - m_currentSteering) * approxOneExp(frameTime * 30.f);
  219.  
  220.                         float animationTime = clamp_tpl(m_currentSteering * 0.5f + 0.5f, 0.f, 1.f);
  221.                         pActionController->SetParam("steeringTime", animationTime);
  222.  
  223.                         //float color[] = {1,1,1,1};
  224.                         //IRenderAuxText::Draw2dLabel(100,230,1.5,color,false,"animationTime: %.3f", animationTime);
  225.                         //IRenderAuxText::Draw2dLabel(100,250,1.5,color,false,"angle: %.2f", RAD2DEG(psv.steer));
  226.                 }
  227.                 else if (params->steeringClass == eSC_Generic)
  228.                 {
  229.                         m_steeringValues -= m_steeringValues * params->steeringRelaxMult * frameTime;
  230.                         m_steeringValues += m_steeringActions * params->steeringForce * frameTime;
  231.  
  232.                         m_steeringValues.y = min(1.0f, max(-1.0f, m_steeringValues.y));
  233.  
  234.                         const float animationTime = (m_steeringValues.y + 1.0f) * 0.5f;
  235.  
  236.                         pActionController->SetParam("steeringTime", animationTime);
  237.  
  238.                         //float color[] = {1,1,1,1};
  239.                         //IRenderAuxText::Draw2dLabel(100,230,1.5,color,false,"action: %.2f", m_steeringActions.y);
  240.                         //IRenderAuxText::Draw2dLabel(100,250,1.5,color,false,"value: %.2f", value);
  241.                 }
  242.         }
  243. }
  244.  
  245. //------------------------------------------------------------------------
  246. void CVehicleSeatActionSteeringWheel::Serialize(TSerialize ser, EEntityAspects aspects)
  247. {
  248.         if (ser.GetSerializationTarget() != eST_Network)
  249.         {
  250.                 ser.Value("steeringValues", m_steeringValues);
  251.                 ser.Value("steeringActions", m_steeringActions);
  252.  
  253.                 m_isUsedSerialization = m_isBeingUsed;
  254.                 ser.Value("isUsed", m_isUsedSerialization);
  255.         }
  256. }
  257.  
  258. //------------------------------------------------------------------------
  259. void CVehicleSeatActionSteeringWheel::PostSerialize()
  260. {
  261.         if (m_isUsedSerialization != m_isBeingUsed)
  262.         {
  263.                 m_pVehicle->SetObjectUpdate(this, m_isUsedSerialization ? IVehicle::eVOU_PassengerUpdate : IVehicle::eVOU_NoUpdate);
  264.                 m_isBeingUsed = m_isUsedSerialization;
  265.         }
  266. }
  267.  
  268. DEFINE_VEHICLEOBJECT(CVehicleSeatActionSteeringWheel);
  269.  
downloadVehicleSeatActionSteeringWheel.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