BVB Source Codes

CRYENGINE Show FeatureCollision.cpp Source code

Return Download CRYENGINE: download FeatureCollision.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 "FeatureCollision.h"
  5.  
  6. CRY_PFX2_DBG
  7.  
  8. namespace pfx2
  9. {
  10.  
  11. EParticleDataType PDT(EPDT_ContactPoint, SContactPoint, 1);
  12.  
  13. //////////////////////////////////////////////////////////////////////////
  14. // CFeatureCollision
  15.  
  16. CFeatureCollision::CFeatureCollision()
  17.         : m_collisionsLimitMode(ECollisionLimitMode::Unlimited)
  18.         , m_terrain(true), m_staticObjects(true), m_dynamicObjects(false)
  19. {
  20. }
  21.  
  22. void CFeatureCollision::AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams)
  23. {
  24.         if (m_collisionsLimitMode != ECollisionLimitMode::Unlimited)
  25.                 pComponent->AddToUpdateList(EUL_Update, this);
  26. }
  27.  
  28. void CFeatureCollision::Serialize(Serialization::IArchive& ar)
  29. {
  30.         ar(m_terrain, "Terrain", "Terrain");
  31.         ar(m_staticObjects, "StaticObjects", "Static Objects");
  32.         ar(m_dynamicObjects, "DynamicObjects", "Dynamic Objects");
  33.         ar(m_elasticity, "Elasticity", "Elasticity");
  34.         ar(m_collisionsLimitMode, "CollisionsLimitMode", "Collision Limit");
  35.         if (m_collisionsLimitMode != ECollisionLimitMode::Unlimited)
  36.                 ar(m_maxCollisions, "MaxCollisions", "Maximum Collisions");
  37. }
  38.  
  39. int CFeatureCollision::GetRayTraceFilter() const
  40. {
  41.         int filter = ent_no_ondemand_activation;
  42.         if (m_terrain)
  43.                 filter |= ent_terrain;
  44.         if (m_staticObjects)
  45.                 filter |= ent_static;
  46.         if (m_dynamicObjects)
  47.                 filter |= ent_rigid | ent_sleeping_rigid | ent_living | ent_independent;
  48.         return filter;
  49. }
  50.  
  51. struct SCollisionLimitIgnore
  52. {
  53.         SCollisionLimitIgnore(CParticleContainer& container)
  54.                 : m_contactPoints(container.GetTIOStream<SContactPoint>(EPDT_ContactPoint)) {}
  55.         void CollisionLimit(TParticleId particleId)
  56.         {
  57.                 SContactPoint contact = m_contactPoints.Load(particleId);
  58.                 contact.m_flags |= uint(EContactPointsFlags::Ignore);
  59.                 m_contactPoints.Store(particleId, contact);
  60.         }
  61.         TIOStream<SContactPoint> m_contactPoints;
  62. };
  63.  
  64. struct SCollisionLimitStop
  65. {
  66.         SCollisionLimitStop(CParticleContainer& container)
  67.                 : m_contactPoints(container.GetTIOStream<SContactPoint>(EPDT_ContactPoint))
  68.                 , m_positions(container.GetIOVec3Stream(EPVF_Position))
  69.                 , m_velocities(container.GetIOVec3Stream(EPVF_Velocity)) {}
  70.         void CollisionLimit(TParticleId particleId)
  71.         {
  72.                 SContactPoint contact = m_contactPoints.Load(particleId);
  73.                 contact.m_flags |= uint(EContactPointsFlags::Ignore);
  74.                 m_positions.Store(particleId, contact.m_point);
  75.                 m_velocities.Store(particleId, Vec3(ZERO));
  76.                 m_contactPoints.Store(particleId, contact);
  77.         }
  78.         TIOStream<SContactPoint> m_contactPoints;
  79.         IOVec3Stream             m_positions;
  80.         IOVec3Stream             m_velocities;
  81. };
  82.  
  83. struct SCollisionLimitKill
  84. {
  85.         SCollisionLimitKill(CParticleContainer& container)
  86.                 : m_ages(container.GetIOFStream(EPDT_NormalAge)) {}
  87.         void CollisionLimit(TParticleId particleId)
  88.         {
  89.                 m_ages.Store(particleId, 1.0f);
  90.         }
  91.         IOFStream m_ages;
  92. };
  93.  
  94. void CFeatureCollision::Update(const SUpdateContext& context)
  95. {
  96.         CRY_PFX2_PROFILE_DETAIL;
  97.  
  98.         switch (m_collisionsLimitMode)
  99.         {
  100.         case ECollisionLimitMode::Ignore:
  101.                 UpdateCollisionLimit<SCollisionLimitIgnore>(context);
  102.                 break;
  103.         case ECollisionLimitMode::Stop:
  104.                 UpdateCollisionLimit<SCollisionLimitStop>(context);
  105.                 break;
  106.         case ECollisionLimitMode::Kill:
  107.                 UpdateCollisionLimit<SCollisionLimitKill>(context);
  108.                 break;
  109.         }
  110. }
  111.  
  112. template<typename TCollisionLimit>
  113. void CFeatureCollision::UpdateCollisionLimit(const SUpdateContext& context)
  114. {
  115.         CParticleContainer& container = context.m_container;
  116.         TCollisionLimit limiter(container);
  117.         const TIStream<SContactPoint> contactPoints = container.GetTIStream<SContactPoint>(EPDT_ContactPoint);
  118.  
  119.         CRY_PFX2_FOR_ACTIVE_PARTICLES(context)
  120.         {
  121.                 const SContactPoint contact = contactPoints.Load(particleId);
  122.                 if (contact.m_totalCollisions >= m_maxCollisions)
  123.                         limiter.CollisionLimit(particleId);
  124.         }
  125.         CRY_PFX2_FOR_END;
  126. }
  127.  
  128. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureCollision, "Motion", "Collisions", colorMotion);
  129.  
  130. //////////////////////////////////////////////////////////////////////////
  131. // CFeatureGPUCollision
  132.  
  133. void CFeatureGPUCollision::AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams)
  134. {
  135.         pComponent->AddToUpdateList(EUL_Update, this);
  136.  
  137.         if (auto pInt = GetGpuInterface())
  138.         {
  139.                 gpu_pfx2::SFeatureParametersCollision params;
  140.                 params.offset = m_offset;
  141.                 params.radius = m_radius;
  142.                 params.restitution = m_restitution;
  143.                 pInt->SetParameters(params);
  144.         }
  145. }
  146.  
  147. void CFeatureGPUCollision::Serialize(Serialization::IArchive& ar)
  148. {
  149.         ar(m_offset, "Offset", "Offset");
  150.         ar(m_radius, "Radius", "Radius");
  151.         ar(m_restitution, "Restitution", "Restitution");
  152. }
  153.  
  154. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureGPUCollision, "GPU Particles", "Collision", colorGPU);
  155. CRY_PFX2_LEGACY_FEATURE(CParticleFeature, CFeatureGPUCollision, "MotionCollision");
  156.  
  157. }
  158.  
downloadFeatureCollision.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