BVB Source Codes

CRYENGINE Show VehiclePartMassBox.cpp Source code

Return Download CRYENGINE: download VehiclePartMassBox.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 simple box-shaped part for mass distribution
  8.  
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 17:10:2005: Created by MichaelR
  12.  
  13. *************************************************************************/
  14. #include "StdAfx.h"
  15.  
  16. #include "IVehicleSystem.h"
  17. #include "VehiclePartMassBox.h"
  18. #include "CryAction.h"
  19. #include "Vehicle.h"
  20.  
  21. //------------------------------------------------------------------------
  22. CVehiclePartMassBox::CVehiclePartMassBox()
  23. {
  24.         m_drivingOffset = 0.f;
  25.         m_Driving = eMASSBOXDRIVING_DEFAULT;
  26.         m_hideCount = 1; // eg hidden
  27.         m_size.zero();
  28.  
  29.         m_localTM.SetIdentity();
  30. }
  31.  
  32. //------------------------------------------------------------------------
  33. CVehiclePartMassBox::~CVehiclePartMassBox()
  34. {
  35. }
  36.  
  37. //------------------------------------------------------------------------
  38. bool CVehiclePartMassBox::Init(IVehicle* pVehicle, const CVehicleParams& table, IVehiclePart* parent, CVehicle::SPartInitInfo& initInfo, int partType)
  39. {
  40.         if (!CVehiclePartBase::Init(pVehicle, table, parent, initInfo, eVPT_Massbox))
  41.                 return false;
  42.  
  43.         m_size.Set(1.f, 2.f, 0.75f); // default
  44.  
  45.         CVehicleParams subTable = table.findChild("MassBox");
  46.         if (subTable)
  47.         {
  48.                 subTable.getAttr("size", m_size);
  49.                 subTable.getAttr("drivingOffset", m_drivingOffset);
  50.         }
  51.  
  52.         m_localTM.SetTranslation(m_pSharedParameters->m_position);
  53.  
  54.         m_bounds.min = -m_size;
  55.         m_bounds.max = m_size;
  56.         m_bounds.SetTransformedAABB(m_localTM, m_bounds);
  57.  
  58.         if (m_pSharedParameters->m_mass == 0 && m_pSharedParameters->m_density > 0)
  59.                 m_pVehicle->m_mass += m_pSharedParameters->m_density * 8 * m_size.x * m_size.y * m_size.z;
  60.  
  61.         m_state = eVGS_Default;
  62.  
  63.         if (m_drivingOffset != 0.f && m_pVehicle->GetWheelCount())
  64.                 m_pVehicle->SetObjectUpdate(this, IVehicle::eVOU_AlwaysUpdate);
  65.  
  66.         return true;
  67. }
  68.  
  69. //------------------------------------------------------------------------
  70. void CVehiclePartMassBox::Physicalize()
  71. {
  72.         if (!m_pSharedParameters->m_isPhysicalized)
  73.                 return;
  74.  
  75.         IPhysicalEntity* pPhysics = GetEntity()->GetPhysics();
  76.         if (!pPhysics)
  77.                 return;
  78.  
  79.         // create geometry
  80.         primitives::box box;
  81.         box.Basis.SetIdentity();
  82.         box.center.zero();
  83.         box.size = m_size;
  84.  
  85.         IGeomManager* pGeomMan = gEnv->pPhysicalWorld->GetGeomManager();
  86.         IGeometry* pGeom = pGeomMan->CreatePrimitive(primitives::box::type, &box);
  87.         phys_geometry* pPhysGeom = pGeomMan->RegisterGeometry(pGeom);
  88.         pGeom->Release();
  89.         pPhysGeom->nRefCount = 0;
  90.  
  91.         pe_geomparams pg;
  92.         pg.mass = m_pSharedParameters->m_mass;
  93.         pg.density = m_pSharedParameters->m_density;
  94.         pg.pos = m_pSharedParameters->m_position;
  95.         pg.flags &= ~(geom_collides | geom_floats);
  96.         pg.flagsCollider = 0;
  97.  
  98.         m_physId = pPhysics->AddGeometry(pPhysGeom, &pg, m_pVehicle->GetNextPhysicsSlot(true));
  99.         SetupDriving(eMASSBOXDRIVING_DEFAULT);
  100.  
  101.         if (-1 == m_physId)
  102.                 CryLog("[CVehiclePartMassBox]: AddGeometry failed! <%s>", m_pVehicle->GetEntity()->GetName());
  103. }
  104.  
  105. //------------------------------------------------------------------------
  106. void CVehiclePartMassBox::Reset()
  107. {
  108.         SetupDriving(eMASSBOXDRIVING_DEFAULT);
  109.         CVehiclePartBase::Reset();
  110. }
  111.  
  112. //------------------------------------------------------------------------
  113. void CVehiclePartMassBox::SetupDriving(EMassBoxDrivingType driving)
  114. {
  115.         if (m_Driving == driving)
  116.                 return;
  117.  
  118.         if (m_drivingOffset == 0.f || m_physId == -1)
  119.                 return;
  120.  
  121.         //CryLog("%s: setup driving (%i)", GetEntity()->GetName(), drive);
  122.  
  123.         IPhysicalEntity* pPhysics = GetEntity()->GetPhysics();
  124.  
  125.         pe_params_part params;
  126.         params.partid = m_physId;
  127.         if (pPhysics->GetParams(&params))
  128.         {
  129.  
  130.                 pe_params_part newparams;
  131.                 newparams.partid = m_physId;
  132.                 newparams.pos = m_pSharedParameters->m_position;
  133.  
  134.                 if (driving == eMASSBOXDRIVING_NORMAL || driving == eMASSBOXDRIVING_INTHEWATER)
  135.                         newparams.pos.z += m_drivingOffset;
  136.                 else if (driving == eMASSBOXDRIVING_INTHEAIR)
  137.                         newparams.pos.z -= m_drivingOffset;
  138.  
  139.                 pPhysics->SetParams(&newparams);
  140.                 m_Driving = driving;
  141.         }
  142. }
  143.  
  144. //------------------------------------------------------------------------
  145. void CVehiclePartMassBox::Update(const float frameTime)
  146. {
  147.         IVehicleMovement* pMovement = m_pVehicle->GetMovement();
  148.         const SVehicleStatus& status = m_pVehicle->GetStatus();
  149.         EMassBoxDrivingType driving;
  150.  
  151.         if (status.health == 0.0f)
  152.         {
  153.                 driving = eMASSBOXDRIVING_DESTROYED;
  154.         }
  155.         else if (status.speed > 0.5f && status.passengerCount > 0)
  156.         {
  157.                 int wheelContacts = pMovement ? pMovement->GetWheelContacts() : 0;
  158.                 bool contact = wheelContacts >= 0.5f * m_pVehicle->GetWheelCount() - 1;
  159.                 if (status.submergedRatio > 0.0f)
  160.                         driving = eMASSBOXDRIVING_INTHEWATER;
  161.                 else if (contact)
  162.                         driving = eMASSBOXDRIVING_NORMAL;
  163.                 else
  164.                         driving = eMASSBOXDRIVING_INTHEAIR;
  165.         }
  166.         else
  167.         {
  168.                 driving = eMASSBOXDRIVING_DEFAULT;
  169.         }
  170.  
  171.         SetupDriving(driving);
  172. }
  173.  
  174. //------------------------------------------------------------------------
  175. const Matrix34& CVehiclePartMassBox::GetLocalTM(bool relativeToParentPart, bool forced)
  176. {
  177.         return m_localTM;
  178. }
  179.  
  180. //------------------------------------------------------------------------
  181. const Matrix34& CVehiclePartMassBox::GetWorldTM()
  182. {
  183.         m_worldTM = GetEntity()->GetWorldTM() * GetLocalTM(false);
  184.         return m_worldTM;
  185. }
  186.  
  187. //------------------------------------------------------------------------
  188. const AABB& CVehiclePartMassBox::GetLocalBounds()
  189. {
  190.         // relative to entity
  191.         return m_bounds;
  192. }
  193.  
  194. //------------------------------------------------------------------------
  195. void CVehiclePartMassBox::Serialize(TSerialize ser, EEntityAspects aspects)
  196. {
  197.         CVehiclePartBase::Serialize(ser, aspects);
  198.  
  199.         if (ser.GetSerializationTarget() != eST_Network)
  200.         {
  201.                 int driving = (int)m_Driving;
  202.                 ser.Value("driving", driving);
  203.                 SetupDriving((EMassBoxDrivingType)driving);
  204.         }
  205. }
  206.  
  207. DEFINE_VEHICLEOBJECT(CVehiclePartMassBox);
  208.  
downloadVehiclePartMassBox.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