BVB Source Codes

CRYENGINE Show VehicleDamageBehaviorImpulse.cpp Source code

Return Download CRYENGINE: download VehicleDamageBehaviorImpulse.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 "VehicleDamageBehaviorImpulse.h"
  5.  
  6. #include "IVehicleSystem.h"
  7. #include "Vehicle.h"
  8.  
  9. //------------------------------------------------------------------------
  10. CVehicleDamageBehaviorImpulse::CVehicleDamageBehaviorImpulse()
  11.         : m_pVehicle(nullptr)
  12.         , m_forceMin(0.0f)
  13.         , m_forceMax(0.0f)
  14.         , m_worldSpace(false)
  15.         , m_pImpulseLocation(nullptr)
  16. {
  17.         m_impulseDir.zero();
  18.         m_angImpulse.zero();
  19. }
  20.  
  21. //------------------------------------------------------------------------
  22. bool CVehicleDamageBehaviorImpulse::Init(IVehicle* pVehicle, const CVehicleParams& table)
  23. {
  24.         m_pVehicle = (CVehicle*) pVehicle;
  25.  
  26.         CVehicleParams impulseParams = table.findChild("Impulse");
  27.         if (!impulseParams)
  28.                 return false;
  29.  
  30.         impulseParams.getAttr("worldSpace", m_worldSpace);
  31.         impulseParams.getAttr("forceMin", m_forceMin);
  32.         impulseParams.getAttr("forceMax", m_forceMax);
  33.  
  34.         impulseParams.getAttr("direction", m_impulseDir);
  35.         m_impulseDir.NormalizeSafe(Vec3(0, 0, 1));
  36.  
  37.         impulseParams.getAttr("momentum", m_angImpulse);
  38.  
  39.         if (impulseParams.haveAttr("helper"))
  40.                 m_pImpulseLocation = m_pVehicle->GetHelper(impulseParams.getAttr("helper"));
  41.  
  42.         return true;
  43. }
  44.  
  45. //------------------------------------------------------------------------
  46. void CVehicleDamageBehaviorImpulse::Reset()
  47. {
  48. }
  49.  
  50. //------------------------------------------------------------------------
  51. void CVehicleDamageBehaviorImpulse::OnDamageEvent(EVehicleDamageBehaviorEvent event, const SVehicleDamageBehaviorEventParams& behaviorParams)
  52. {
  53.         if (event == eVDBE_Hit || event == eVDBE_VehicleDestroyed || event == eVDBE_ComponentDestroyed)
  54.         {
  55.                 IEntity* pEntity = m_pVehicle->GetEntity();
  56.                 CRY_ASSERT(pEntity);
  57.  
  58.                 IPhysicalEntity* pPhysEntity = pEntity->GetPhysics();
  59.                 if (!pPhysEntity)
  60.                         return;
  61.  
  62.                 pe_status_dynamics dyn;
  63.                 pPhysEntity->GetStatus(&dyn);
  64.                 float vehicleMass = dyn.mass;
  65.  
  66.                 float r = cry_random(0.0f, 2.f);
  67.                 float impulseForce = cry_random(m_forceMin, m_forceMax) * vehicleMass;
  68.  
  69.                 Vec3 impulseDir(m_impulseDir);
  70.                 if (!m_worldSpace)
  71.                         impulseDir = m_pVehicle->GetEntity()->GetWorldTM().TransformVector(impulseDir);
  72.  
  73.                 pe_action_impulse actionImpulse;
  74.                 Vec3& impulse = actionImpulse.impulse;
  75.                 Vec3& angImpulse = actionImpulse.angImpulse;
  76.  
  77.                 impulse = impulseDir * impulseForce;
  78.                 if (!m_worldSpace)
  79.                         angImpulse = m_pVehicle->GetEntity()->GetWorldTM().TransformVector(m_angImpulse);
  80.  
  81.                 if (r <= 0.75f)
  82.                 {
  83.                         float r1 = cry_random(-0.35f, 0.35f);
  84.  
  85.                         angImpulse += dyn.v * r1 * max(1.0f, dyn.w.GetLength());
  86.                         angImpulse *= vehicleMass;
  87.                 }
  88.                 else
  89.                 {
  90.                         float r1 = cry_random(-0.25f, 0.25f);
  91.                         float r2 = cry_random(-0.5f, 0.5f);
  92.  
  93.                         impulse.z += abs(dyn.v.y) * r1 * vehicleMass;
  94.                         angImpulse.x += dyn.v.y * r2 * vehicleMass * max(1.0f, dyn.w.GetLength() * 1.5f);
  95.                 }
  96.  
  97.                 if (m_pImpulseLocation)
  98.                         actionImpulse.point = m_pImpulseLocation->GetWorldSpaceTranslation();
  99.  
  100.                 pPhysEntity->Action(&actionImpulse);
  101.         }
  102. }
  103.  
  104. DEFINE_VEHICLEOBJECT(CVehicleDamageBehaviorImpulse);
  105.  
downloadVehicleDamageBehaviorImpulse.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