BVB Source Codes

CRYENGINE Show VehicleViewThirdPerson.cpp Source code

Return Download CRYENGINE: download VehicleViewThirdPerson.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 third person view for vehicles
  8.  
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 02:05:2005: Created by Mathieu Pinard
  12.  
  13. *************************************************************************/
  14. #include "StdAfx.h"
  15.  
  16. #include "IViewSystem.h"
  17. #include "IVehicleSystem.h"
  18. #include "VehicleSeat.h"
  19. #include "VehicleViewThirdPerson.h"
  20.  
  21. #include <CryMath/Cry_GeoIntersect.h>
  22.  
  23. const char* CVehicleViewThirdPerson::m_name = "ThirdPerson";
  24. float CVehicleViewThirdPerson::m_defaultDistance = 0.f;
  25. float CVehicleViewThirdPerson::m_defaultHeight = 0.f;
  26.  
  27. static float g_CameraRadius = 0.5f;
  28.  
  29. //------------------------------------------------------------------------
  30. CVehicleViewThirdPerson::CVehicleViewThirdPerson()
  31.         : m_pEntitySystem(nullptr)
  32.         , m_p3DEngine(nullptr)
  33.         , m_pAimPart(nullptr)
  34.         , m_targetEntityId(0)
  35.         , m_heightOffset(0.0f)
  36.         , m_height(0.0f)
  37.         , m_distance(0.0f)
  38.         , m_boundSwitchAngle(0.0f)
  39.         , m_rot(0.0f)
  40.         , m_interpolationSpeed(5.0f) // default speed
  41.         , m_zoom(1.0f)
  42.         , m_zoomMult(0.0f)
  43.         , m_actionZoom(0.0f)
  44.         , m_actionZoomSpeed(1.0f)
  45.         , m_position(ZERO)
  46.         , m_worldPos(ZERO)
  47.         , m_isUpdatingPos(false)
  48. {
  49. }
  50.  
  51. //------------------------------------------------------------------------
  52. CVehicleViewThirdPerson::~CVehicleViewThirdPerson()
  53. {
  54. }
  55.  
  56. //------------------------------------------------------------------------
  57. bool CVehicleViewThirdPerson::Init(IVehicleSeat* pISeat, const CVehicleParams& table)
  58. {
  59.         CVehicleSeat* pSeat = static_cast<CVehicleSeat*>(pISeat);
  60.         if (!CVehicleViewBase::Init(pSeat, table))
  61.                 return false;
  62.  
  63.         m_p3DEngine = gEnv->p3DEngine;
  64.         m_pEntitySystem = gEnv->pEntitySystem;
  65.  
  66.         m_pSeat = pSeat;
  67.         m_pVehicle = pSeat->GetVehicle();
  68.         m_targetEntityId = 0;
  69.         m_pAimPart = pSeat->GetAimPart();
  70.  
  71.         CVehicleParams params = table.findChild(m_name);
  72.         if (!params)
  73.                 return false;
  74.  
  75.         params.getAttr("distance", m_distance);
  76.         params.getAttr("speed", m_interpolationSpeed);
  77.  
  78.         if (!params.getAttr("heightOffset", m_heightOffset))
  79.                 m_heightOffset = 1.5f; // added default offset (suggested by designers)
  80.  
  81.         Reset();
  82.         return true;
  83. }
  84.  
  85. //------------------------------------------------------------------------
  86. bool CVehicleViewThirdPerson::Init(CVehicleSeat* pSeat)
  87. {
  88.         if (!CVehicleViewBase::Init(pSeat))
  89.                 return false;
  90.  
  91.         m_p3DEngine = gEnv->p3DEngine;
  92.         m_pEntitySystem = gEnv->pEntitySystem;
  93.  
  94.         m_pSeat = pSeat;
  95.         m_pVehicle = pSeat->GetVehicle();
  96.         m_targetEntityId = 0;
  97.         m_pAimPart = NULL;
  98.  
  99.         m_name = "ThirdPerson";
  100.  
  101.         AABB vehicleBounds;
  102.         pSeat->GetVehicle()->GetEntity()->GetLocalBounds(vehicleBounds);
  103.  
  104.         m_distance = (vehicleBounds.max - vehicleBounds.min).GetLength();
  105.         m_interpolationSpeed = 5.0f;
  106.         m_heightOffset = 1.5f;
  107.  
  108.         Reset();
  109.         return true;
  110. }
  111.  
  112. //------------------------------------------------------------------------
  113. void CVehicleViewThirdPerson::Reset()
  114. {
  115.         CVehicleViewBase::Reset();
  116.  
  117.         m_actionZoom = 0.0f;
  118.         m_actionZoomSpeed = 1.0f;
  119.  
  120.         m_rot = 0.0f;
  121.         m_zoom = 1.0f;
  122.         m_zoomMult = 0.0f;
  123.  
  124.         m_isUpdatingPos = true;
  125. }
  126.  
  127. //------------------------------------------------------------------------
  128. void CVehicleViewThirdPerson::OnAction(const TVehicleActionId actionId, int activationMode, float value)
  129. {
  130.         CVehicleViewBase::OnAction(actionId, activationMode, value);
  131.  
  132.         if (actionId == eVAI_ZoomIn)
  133.                 m_actionZoom -= value;
  134.         else if (actionId == eVAI_ZoomOut)
  135.                 m_actionZoom += value;
  136. }
  137.  
  138. //------------------------------------------------------------------------
  139. void CVehicleViewThirdPerson::OnStartUsing(EntityId passengerId)
  140. {
  141.         CVehicleViewBase::OnStartUsing(passengerId);
  142.         m_position = gEnv->pSystem->GetViewCamera().GetPosition();
  143.         //m_position.z -= (m_defaultHeight != 0) ? m_defaultHeight : m_heightOffset;
  144.         m_zoomMult = 0.0f;
  145. }
  146.  
  147. //------------------------------------------------------------------------
  148. void CVehicleViewThirdPerson::Update(float frameTime)
  149. {
  150.         CVehicleViewBase::Update(frameTime);
  151.  
  152.         // apply any zoom changes
  153.         Interpolate(m_zoomMult, m_actionZoom, 2.5f, frameTime);
  154.         if (!iszero(m_zoomMult))
  155.                 m_zoom = min(3.5f, max(0.75f, m_zoom + m_zoomMult));
  156.         m_actionZoom = 0.0f;
  157.  
  158.         Interpolate(m_rot, m_rotation.z, m_interpolationSpeed, frameTime);
  159.  
  160.         Matrix34 worldTM;
  161.         if (m_pAimPart)
  162.                 worldTM = m_pAimPart->GetWorldTM();
  163.         else if (IEntity* pEntity = gEnv->pEntitySystem->GetEntity(m_targetEntityId))
  164.                 worldTM = pEntity->GetWorldTM();
  165.         else
  166.                 worldTM = m_pVehicle->GetEntity()->GetWorldTM();
  167.  
  168.         if (m_isUpdatingPos)
  169.                 m_worldPos = worldTM.GetTranslation();
  170.  
  171.         Ang3 worldAngles = Ang3::GetAnglesXYZ(Matrix33(worldTM));
  172.         float rot = worldAngles.z + m_rot;
  173.         float distance = (m_defaultDistance != 0) ? m_defaultDistance : m_distance;
  174.  
  175.         Vec3 goal;
  176.         goal.x = distance * m_zoom * cosf(rot + gf_PI * 1.5f) + m_worldPos.x;
  177.         goal.y = distance * m_zoom * sinf(rot - gf_PI / 2.0f) + m_worldPos.y;
  178.  
  179.         AABB vehicleBounds;
  180.         m_pVehicle->GetEntity()->GetLocalBounds(vehicleBounds);
  181.         goal.z = vehicleBounds.max.z;
  182.         goal.z += m_pVehicle->GetEntity()->GetWorldPos().z;
  183.  
  184.         // intersection check...
  185.         {
  186.                 IPhysicalEntity* pSkipEntities[10];
  187.                 int nSkip = 0;
  188.                 if (m_pVehicle)
  189.                 {
  190.                         nSkip = m_pVehicle->GetSkipEntities(pSkipEntities, 10);
  191.                 }
  192.  
  193.                 primitives::sphere sphere;
  194.                 sphere.center = m_worldPos;
  195.                 sphere.r = g_CameraRadius;
  196.                 Vec3 dir = goal - m_worldPos;
  197.  
  198.                 geom_contact* pContact = 0;
  199.                 float hitDist = gEnv->pPhysicalWorld->PrimitiveWorldIntersection(sphere.type, &sphere, dir, ent_static | ent_terrain | ent_rigid | ent_sleeping_rigid,
  200.                                                                                  &pContact, 0, (geom_colltype_player << rwi_colltype_bit) | rwi_stop_at_pierceable, 0, 0, 0, pSkipEntities, nSkip);
  201.                 if (hitDist > 0.0f)
  202.                 {
  203.                         goal = m_worldPos + hitDist * dir.GetNormalizedSafe();
  204.                 }
  205.         }
  206.  
  207.         Interpolate(m_position, goal, 5.0f, frameTime);
  208. }
  209.  
  210. //------------------------------------------------------------------------
  211. void CVehicleViewThirdPerson::UpdateView(SViewParams& viewParams, EntityId playerId)
  212. {
  213.         viewParams.position = m_position;
  214.         viewParams.position.z += (m_defaultHeight != 0) ? m_defaultHeight : m_heightOffset;
  215.  
  216.         Matrix33 rotation = Matrix33::CreateRotationVDir((m_worldPos - m_position).GetNormalizedSafe());
  217.         viewParams.rotation = Quat(rotation);
  218.  
  219.         // set view direction on actor
  220.         IActor* pActor = CCryAction::GetCryAction()->GetIActorSystem()->GetActor(playerId);
  221.         if (pActor && pActor->IsClient())
  222.         {
  223.                 pActor->SetViewInVehicle(viewParams.rotation);
  224.         }
  225.  
  226.         viewParams.nearplane = g_CameraRadius + 0.1f;
  227. }
  228.  
  229. //------------------------------------------------------------------------
  230. void CVehicleViewThirdPerson::Serialize(TSerialize serialize, EEntityAspects aspects)
  231. {
  232.         CVehicleViewBase::Serialize(serialize, aspects);
  233.  
  234.         if (serialize.GetSerializationTarget() != eST_Network)
  235.         {
  236.                 serialize.Value("zoom", m_zoom);
  237.                 serialize.Value("position", m_position);
  238.                 serialize.Value("worldPosition", m_worldPos);
  239.         }
  240. }
  241.  
  242. //------------------------------------------------------------------------
  243. void CVehicleViewThirdPerson::SetDefaultDistance(float dist)
  244. {
  245.         m_defaultDistance = dist;
  246. }
  247.  
  248. //------------------------------------------------------------------------
  249. void CVehicleViewThirdPerson::SetDefaultHeight(float height)
  250. {
  251.         m_defaultHeight = height;
  252. }
  253.  
  254. //------------------------------------------------------------------------
  255. void CVehicleViewThirdPerson::OffsetPosition(const Vec3& delta)
  256. {
  257. #ifdef SEG_WORLD
  258.         m_position += delta;
  259.         m_worldPos += delta;
  260. #endif
  261. }
  262.  
  263. DEFINE_VEHICLEOBJECT(CVehicleViewThirdPerson);
  264.  
downloadVehicleViewThirdPerson.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