Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
CharacterCamera3rdPersonVehicle.c
Go to the documentation of this file.
1 // *************************************************************************************
2 // ! CharacterCamera3rdPersonVehicle - 3rd person camera when character is in vehicle
3 // *************************************************************************************
4 class CharacterCamera3rdPersonVehicle extends CharacterCameraBase
5 {
6  //-----------------------------------------------------------------------------
7  static const float CONST_UD_MIN = -89.0;
8  static const float CONST_UD_MAX = 89.0;
9 
10  static const float CONST_LR_MIN = -180.0;
11  static const float CONST_LR_MAX = 180.0;
12 
13  static const float STEERING_DEGREES = 5;
14  static const float ANGULAR_INERTIA = 5;
15  protected float m_fSteeringAngle;
16  protected float m_fInertiaAngle;
17 
18  //------------------------------------------------------------------------------------------------
19  protected float m_fHeight;
20  protected float m_fDist_Desired;
21  protected float m_fDist_Min;
22  protected float m_fDist_Max;
23  protected float m_fFOV_SpeedAdjustMax;
24  protected float m_fBobScale;
25  protected float m_fShakeScale;
26  protected float m_fSpeedMax;
27 
28  protected IEntity m_OwnerVehicle;
29  protected BaseCompartmentSlot m_pCompartment;
31  protected vector m_vCenter;
32 
33  protected vector m_vLastVel;
34  protected vector m_vLastAngVel;
35  protected vector m_vAcceleration;
36 
37  protected float m_f3rd_TraceClipPct;
38  protected float m_fBob_FastUp;
39  protected float m_fBob_FastRight;
40  protected float m_fBob_SlowUp;
41  protected float m_fBob_SlowRight;
42  protected float m_fBob_ScaleFast;
43  protected float m_fBob_ScaleSlow;
44  protected float m_fBob_Acceleration;
45 
46  //rad
47  protected float m_fAngleThirdPerson;
48 
49  protected SCR_VehicleCameraAimpoint m_pCameraAimpointData;
50  protected SCR_VehicleCameraAlignment m_pCameraAlignData;
51 
52  bool m_bCharacterWasJustEjected = false;
53 
54  //-----------------------------------------------------------------------------
55  void CharacterCamera3rdPersonVehicle(CameraHandlerComponent pCameraHandler)
56  {
57  m_fFOV = GetBaseFOV();
58  m_bLRAngleNoLimit = true;
59  }
60 
61  //-----------------------------------------------------------------------------
62  void InitCameraData()
63  {
64  CompartmentAccessComponent compartmentAccess = m_OwnerCharacter.GetCompartmentAccessComponent();
65  if (compartmentAccess && compartmentAccess.IsInCompartment())
66  {
67  m_pCompartment = compartmentAccess.GetCompartment();
68  IEntity vehicle = m_pCompartment.GetOwner().GetRootParent();
69  if (vehicle)
70  {
71  m_OwnerVehicle = vehicle;
72  m_TurretController = TurretControllerComponent.Cast(m_pCompartment.GetController());
73 
75  // Can owner replace Vehicle's camera data?
76  SCR_VehicleCameraDataComponent ownerCamData = SCR_VehicleCameraDataComponent.Cast(m_pCompartment.GetOwner().FindComponent(SCR_VehicleCameraDataComponent));
77  if (ownerCamData && ownerCamData.m_bOverrideVehicleSettings)
78  vehicleCamData = ownerCamData;
79 
80  if( vehicleCamData )
81  {
82  m_fHeight = vehicleCamData.m_fHeight;
83  m_fDist_Desired = vehicleCamData.m_fDist_Desired;
84  m_fDist_Min = vehicleCamData.m_fDist_Min;
85  m_fDist_Max = vehicleCamData.m_fDist_Max;
86  //m_fFOV = vehicleCamData.m_fFOV;
87  m_fFOV_SpeedAdjustMax = vehicleCamData.m_fFOV_SpeedAdjustMax;
88  m_fBobScale = vehicleCamData.m_fBobScale;
89  m_fShakeScale = vehicleCamData.m_fShakeScale;
90  m_fSpeedMax = vehicleCamData.m_fSpeedMax;
91  m_pCameraAimpointData = vehicleCamData.m_pCameraAimpointData;
92  m_pCameraAlignData = vehicleCamData.m_pCameraAlignData;
93  m_fRollFactor = vehicleCamData.m_fRollFactor;
94  m_fPitchFactor = vehicleCamData.m_fPitchFactor;
95  m_fAngleThirdPerson = vehicleCamData.m_fAngleThirdPerson * Math.DEG2RAD;
96  }
97 
98  Physics physics = vehicle.GetPhysics();
99  if (physics)
100  {
101  m_vCenter = physics.GetCenterOfMass();
102  }
103  else
104  {
105  vector mins, maxs;
106  vehicle.GetBounds(mins, maxs);
107  m_vCenter = (maxs - mins) * 0.5 + mins;
108  }
109  }
110  }
111  }
112 
113  //-----------------------------------------------------------------------------
114  override void OnActivate(ScriptedCameraItem pPrevCamera, ScriptedCameraItemResult pPrevCameraResult)
115  {
116  super.OnActivate(pPrevCamera, pPrevCameraResult);
117 
118  if (pPrevCamera)
119  {
120  vector f = pPrevCamera.GetBaseAngles();
121  m_fUpDownAngle = f[0];
122  m_fLeftRightAngle = f[1];
123  }
124 
125  CharacterCamera1stPersonVehicle characterCamera1stPersonVehicle = CharacterCamera1stPersonVehicle.Cast(pPrevCamera);
126  if (characterCamera1stPersonVehicle)
127  {
128  m_fRollSmooth = characterCamera1stPersonVehicle.m_fRollSmooth;
129  m_fRollSmoothVel = characterCamera1stPersonVehicle.m_fRollSmoothVel;
130  m_fPitchSmooth = characterCamera1stPersonVehicle.m_fPitchSmooth;
131  m_fPitchSmoothVel = characterCamera1stPersonVehicle.m_fPitchSmoothVel;
132  }
133 
134  InitCameraData();
135  }
136 
137  override void OnBlendOut()
138  {
139  CompartmentAccessComponent compartmentAccess = m_OwnerCharacter.GetCompartmentAccessComponent();
140  if (compartmentAccess && compartmentAccess.IsInCompartment())
141  m_bCharacterWasJustEjected = compartmentAccess.WasLastGettingOutTeleportation();
142  }
143 
144  //-----------------------------------------------------------------------------
145  override void OnUpdate(float pDt, out ScriptedCameraItemResult pOutResult)
146  {
147  CompartmentAccessComponent compartmentAccess = m_OwnerCharacter.GetCompartmentAccessComponent();
148  if (compartmentAccess && compartmentAccess.IsInCompartment())
149  {
150  m_pCompartment = compartmentAccess.GetCompartment();
151  IEntity vehicle = m_pCompartment.GetOwner().GetRootParent();
152  if (vehicle && vehicle != m_OwnerVehicle)
153  {
154  InitCameraData();
155  }
156  }
157 
159  float udAngle = UpdateUDAngle(m_fUpDownAngle, CONST_UD_MIN, CONST_UD_MAX, pDt);
160  m_fLeftRightAngle = UpdateLRAngle(m_fLeftRightAngle, CONST_LR_MIN, CONST_LR_MAX, pDt);
161 
162  pOutResult.m_vBaseAngles = GetBaseAngles();
163 
165  m_fFOV = GetBaseFOV();
166 
167  // phx speed
168  vector characterOffset = vector.Zero;
169  vector localVelocity = vector.Zero;
170  vector localAngVelocity = vector.Zero;
171  vector vehMat[4];
172 
173  bool bCharacterAttached = true;
174 
175  float steeringAngle;
176  if (m_OwnerVehicle)
177  {
178  m_OwnerVehicle.GetTransform(vehMat);
179 
180  vector vehToCharLocalMat[4];
181  if (!m_bCharacterWasJustEjected && m_OwnerCharacter.GetAncestorToLocalTransform(m_OwnerVehicle, vehToCharLocalMat))
182  characterOffset = m_vCenter.Multiply4(vehToCharLocalMat);
183  else
184  {
185  vector charMat[4];
186  if (m_bCharacterWasJustEjected && compartmentAccess)
187  {
188  compartmentAccess.GetTeleportTarget(charMat);
189  }
190  else
191  m_OwnerCharacter.GetTransform(charMat);
192  characterOffset = m_vCenter.Multiply4(vehMat).InvMultiply4(charMat);
193  bCharacterAttached = false;
194  }
195 
196  Physics physics = m_OwnerVehicle.GetPhysics();
197  if (physics)
198  {
199  localVelocity = physics.GetVelocity().InvMultiply3(vehMat);
200  localAngVelocity = physics.GetAngularVelocity().InvMultiply3(vehMat);
201  }
202 
203  //CompartmentAccessComponent compartmentAccess = m_OwnerCharacter.GetCompartmentAccessComponent();
204  if (compartmentAccess && PilotCompartmentSlot.Cast(compartmentAccess.GetCompartment()))
205  {
206  VehicleWheeledSimulation simulation = VehicleWheeledSimulation.Cast(m_OwnerVehicle.FindComponent(VehicleWheeledSimulation));
207  if (simulation)
208  steeringAngle = simulation.GetSteering();
209  }
210  }
211  else
212  {
213  Math3D.MatrixIdentity4(vehMat);
214  }
215 
216  // To smoothen out jittering a bit
217  vector smoothVelocity = vector.Lerp(m_vLastVel, localVelocity, pDt);
218  m_fInertiaAngle = Math.Lerp(m_fInertiaAngle, localAngVelocity[1] * ANGULAR_INERTIA, pDt);
219  m_fSteeringAngle = Math.Lerp(m_fSteeringAngle, steeringAngle * STEERING_DEGREES, pDt);
220 
221  // store phx values
222  m_vLastVel = localVelocity;
223  m_vLastAngVel = localAngVelocity;
224 
225  vector yawPitchRoll = Math3D.MatrixToAngles(vehMat);
226 
228  vector lookAngles;
229  lookAngles[0] = m_fLeftRightAngle + m_fInertiaAngle + m_fSteeringAngle;
230  lookAngles[1] = udAngle - yawPitchRoll[1] * m_fPitchFactor;
231  lookAngles[2] = 0.0;
232 
234  Math3D.AnglesToMatrix(lookAngles, pOutResult.m_CameraTM);
235 
237  if (bCharacterAttached)
238  {
240  vector orientation[3];
241  Math3D.AnglesToMatrix(Vector(0, 0, -yawPitchRoll[2]), orientation);
242  Math3D.MatrixMultiply3(orientation, pOutResult.m_CameraTM, pOutResult.m_CameraTM);
243 
245  float angle = yawPitchRoll[2] * m_fRollFactor * Math.DEG2RAD * pOutResult.m_CameraTM[2][2];
247  float rollMask = Math.Max(0, vehMat[1][1]); // Do not apply roll factor when the vehicle is upside-down.
248  SCR_Math3D.RotateAround(pOutResult.m_CameraTM, vector.Zero, pOutResult.m_CameraTM[2], -angle * rollMask, pOutResult.m_CameraTM);
249 
250  // Camera offset
251  float heightSign = vehMat[1][1]; // Adjust the sign of the height direction as the vehicle rolls.
252  pOutResult.m_CameraTM[3] = characterOffset + Vector(0, heightSign * m_fHeight, 0);
253  }
254  else
255  {
256  pOutResult.m_CameraTM[3] = characterOffset + Vector(0, m_fHeight, 0);
257  }
258 
259  // viewbob update
260  UpdateViewBob(pDt, localVelocity, localAngVelocity);
261 
262  // offset based on speed
263  float speed = localVelocity.Length();
264  float speedScale = Math.Clamp(speed / (m_fSpeedMax * KILOMETERS_PER_HOUR_TO_METERS_PER_SEC), 0, 1);
265  float camDist = Math.Clamp((m_fDist_Max - m_fDist_Min) * speedScale + m_fDist_Desired, m_fDist_Min, m_fDist_Max);
266 
267  // update auto align
268  /*if (m_pCameraAlignData && m_OwnerVehicle)
269  {
270  bool isFocused = false;
271  if (m_CharacterCameraHandler && m_CharacterCameraHandler.GetFocusMode() > 0)
272  isFocused = true;
273 
274  vector aimChange = m_Input.GetAimChange();
275  vector newAngles;
276  if (m_pCameraAlignData.Update(aimChange, Vector(m_fLeftRightAngle, lookAngles[1], 0.0), smoothVelocity, isFocused, pDt, newAngles))
277  {
278  m_fLeftRightAngle = newAngles[0];
279  m_fUpDownAngle = newAngles[1];
280  }
281  }*/
282 
283  // Translate camera aimpoint based on aimpoing curve data
284  if (m_pCameraAimpointData)
285  {
286  vector translation = m_pCameraAimpointData.Sample(smoothVelocity[2] * METERS_PER_SEC_TO_KILOMETERS_PER_HOUR);
287  translation[0] = m_pCameraAimpointData.SampleAside(localAngVelocity[1] * METERS_PER_SEC_TO_KILOMETERS_PER_HOUR);
288  pOutResult.m_CameraTM[3] = pOutResult.m_CameraTM[3] + translation;
289  }
290 
291  if (m_OwnerVehicle)
292  {
293  bool applyCharAngle = true;
294  if (sm_TagLyingCamera != -1)
295  {
296  CharacterAnimationComponent characterAnimationComponent = m_OwnerCharacter.GetAnimationComponent();
297  if (characterAnimationComponent && characterAnimationComponent.IsPrimaryTag(sm_TagLyingCamera))
298  applyCharAngle = false;
299  }
300  if (applyCharAngle)
301  SCR_Math3D.RotateAround(pOutResult.m_CameraTM, pOutResult.m_CameraTM[3], pOutResult.m_CameraTM[0], m_fAngleThirdPerson, pOutResult.m_CameraTM);
302  else
303  SCR_Math3D.RotateAround(pOutResult.m_CameraTM, pOutResult.m_CameraTM[3], pOutResult.m_CameraTM[0], 0, pOutResult.m_CameraTM);
304  }
305 
306  // other parameters
307  pOutResult.m_fUseHeading = 0.0;
308  pOutResult.m_fFOV = m_fFOV + speedScale * m_fFOV_SpeedAdjustMax;
309  pOutResult.m_fDistance = camDist;
310  pOutResult.m_pWSAttachmentReference = null;
311  pOutResult.m_pOwner = m_OwnerCharacter;
312  pOutResult.m_bAllowCollisionSolver = bCharacterAttached;
313 
314  pOutResult.m_bAllowInterpolation = !m_bCharacterWasJustEjected;
315  m_bCharacterWasJustEjected = false;
316 
317  // Apply shake
318  if (m_CharacterCameraHandler)
319  m_CharacterCameraHandler.AddShakeToToTransform(pOutResult.m_CameraTM, pOutResult.m_fFOV);
320  }
321 
322  //-----------------------------------------------------------------------------
323  override void OnAfterCameraUpdate(float pDt, bool pIsKeyframe, inout vector transformMS[4])
324  {
325  //AddVehiclePitchRoll(m_OwnerVehicle, pDt, pOutResult.m_CameraTM);
326  }
327 
328  //-----------------------------------------------------------------------------
329  private void UpdateViewBob(float pDt, vector localVelocity, vector localAngularVelocity)
330  {
331  if (pDt <= 0)
332  return;
333 
334  vector velDiff = localVelocity - m_vLastVel;
335  vector angVelDiff = localAngularVelocity - m_vLastAngVel;
336  float speed = localVelocity.Length();
337  float accel = velDiff.Length() * (1 / pDt) + angVelDiff.Length() * (1 / pDt);
338  float timeScale;
339  if (accel > m_fBob_Acceleration)
340  m_fBob_Acceleration += (accel - m_fBob_Acceleration) * Math.Clamp(pDt * 12, 0, 1);
341  else
342  m_fBob_Acceleration += (accel - m_fBob_Acceleration) * Math.Clamp(pDt * 7, 0, 1);
343 
344  // Add bobbing based on speed
345  m_fBob_ScaleSlow += (Math.Clamp(speed / (m_fSpeedMax * KILOMETERS_PER_HOUR_TO_METERS_PER_SEC), 0, 1) - m_fBob_ScaleSlow) * (pDt * 4);
346  float slowTimeSlice = (m_fBob_ScaleSlow * m_fBob_ScaleSlow * 1.5 + 1) * pDt;
347  m_fBob_SlowUp += Math.Clamp(slowTimeSlice, 0, 1);
348  if (m_fBob_SlowUp >= 1)
349  m_fBob_SlowUp -= 1;
350  m_fBob_SlowRight += Math.Clamp(slowTimeSlice * 0.75, 0, 1);
351  if (m_fBob_SlowRight >= 1)
352  m_fBob_SlowRight -= 1;
353 
354  // Add bobbing based on acceleration
355  m_fBob_ScaleFast = Math.Clamp(m_fBob_Acceleration * 0.03, 0, 1);
356  float fastTimeSlice = m_fBob_ScaleFast * 5 * pDt;
357  if (m_fBob_ScaleFast < 0.01)
358  {
359  m_fBob_FastUp *= 1 - pDt;
360  m_fBob_FastRight *= 1 - pDt;
361  }
362  else
363  {
364  m_fBob_FastUp += Math.Clamp(fastTimeSlice, 0, 1);
365  if (m_fBob_FastUp >= 1)
366  m_fBob_FastUp -= 1;
367  m_fBob_FastRight += Math.Clamp(fastTimeSlice * 1.4, 0, 1);
368  if (m_fBob_FastRight >= 1)
369  m_fBob_FastRight -= 1;
370  }
371  }
372 
373  //------------------------------------------------------------------------------------------------
374  private void AddViewBobToTransform(inout vector outTransform[4])
375  {
376  float bobFastScale = m_fBob_ScaleFast * m_fShakeScale;
377  float bobSlowScale = m_fBob_ScaleSlow * m_fBob_ScaleSlow * m_fBobScale;
378 
379  float bobFw = Math.Sin(m_fBob_FastUp * 360 * Math.DEG2RAD) * -0.05 * bobFastScale;
380  float bobUp = Math.Sin(m_fBob_FastUp * 360 * Math.DEG2RAD) * 0.05 * bobFastScale;
381  float bobRt = Math.Sin(m_fBob_FastRight * 360 * Math.DEG2RAD) * 0.05 * bobFastScale;
382  float bobYaw = Math.Sin(m_fBob_FastRight * 360 * Math.DEG2RAD) * 0.3 * bobFastScale;
383  float bobPitch = Math.Sin(m_fBob_FastUp * 360 * Math.DEG2RAD) * 0.3 * bobFastScale;
384  float bobRoll = Math.Sin(m_fBob_FastRight * 360 * Math.DEG2RAD) * 0.4 * bobFastScale;
385  bobFw += Math.Sin(m_fBob_SlowUp * 360 * Math.DEG2RAD) * -0.025 * bobSlowScale;
386  bobUp += Math.Sin(m_fBob_SlowUp * 360 * Math.DEG2RAD) * 0.05 * bobSlowScale;
387  bobRt += Math.Sin(m_fBob_SlowRight * 360 * Math.DEG2RAD) * 0.05 * bobSlowScale;
388  bobYaw += Math.Sin(m_fBob_SlowRight * 360 * Math.DEG2RAD) * 0.2 * bobSlowScale;
389  bobPitch += Math.Sin(m_fBob_SlowUp * 360 * Math.DEG2RAD) * 0.2 * bobSlowScale;
390  bobRoll += Math.Sin(m_fBob_SlowRight * 360 * Math.DEG2RAD) * 0.45 * bobSlowScale;
391 
392  vector angBobMat[3], endBobmat[3];
393  Math3D.AnglesToMatrix(Vector(bobPitch, bobYaw, bobRoll), angBobMat);
394  Math3D.MatrixMultiply3(outTransform, angBobMat, endBobmat);
395  outTransform[0] = endBobmat[0];
396  outTransform[1] = endBobmat[1];
397  outTransform[2] = endBobmat[2];
398  outTransform[3] = Vector(bobRt, bobUp, bobFw).Multiply3(outTransform) + outTransform[3];
399  }
400 
401  //-----------------------------------------------------------------------------
402  override float GetBaseFOV()
403  {
404  CameraManager cameraManager = GetGame().GetCameraManager();
405  if (!cameraManager)
406  return 0;
407 
408  return cameraManager.GetVehicleFOV();
409  }
410 }
m_fHeight
protected float m_fHeight
Definition: SCR_BaseAreaMeshComponent.c:15
AddViewBobToTransform
float AddViewBobToTransform(inout vector pOutTransform[4], float pBobScale, bool pAllowTranslation)
Definition: CharacterCameraHandlerComponent.c:906
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
PilotCompartmentSlot
Definition: PilotCompartmentSlot.c:12
CharacterCamera1stPersonVehicle
Definition: CharacterCamera1stPersonVehicle.c:163
OnUpdate
override void OnUpdate(BaseWorld world, float timeslice)
Definition: game.c:910
SCR_Math3D
Contains various scripted 3D math functions.
Definition: SCR_Math3D.c:2
OnActivate
override void OnActivate()
Definition: SCR_CharacterCommandLoiter.c:23
m_fBob_ScaleFast
private float m_fBob_ScaleFast
Definition: CharacterCameraHandlerComponent.c:1004
CharacterCameraBase
Definition: CharacterCameraADS.c:4
SCR_VehicleCameraAlignment
Definition: SCR_VehicleCameraAlignment.c:10
m_OwnerCharacter
private SCR_ChimeraCharacter m_OwnerCharacter
Definition: CharacterCameraHandlerComponent.c:984
OnAfterCameraUpdate
override void OnAfterCameraUpdate(float pDt, bool pIsKeyframe, inout vector transformMS[4], inout vector transformWS[4])
after camera update
Definition: CharacterCameraHandlerComponent.c:535
TurretControllerComponent
Definition: TurretControllerComponent.c:12
m_fBob_ScaleSlow
private float m_fBob_ScaleSlow
Definition: CharacterCameraHandlerComponent.c:1003
m_TurretController
protected TurretControllerComponent m_TurretController
Definition: SCR_2DSightsComponent.c:35
METERS_PER_SEC_TO_KILOMETERS_PER_HOUR
const float METERS_PER_SEC_TO_KILOMETERS_PER_HOUR
Contains various global constants.
Definition: Constants.c:5
UpdateViewBob
private void UpdateViewBob(float pDt)
Definition: CharacterCameraHandlerComponent.c:859
SCR_VehicleCameraAimpoint
Definition: SCR_VehicleCameraAimpoint.c:10
KILOMETERS_PER_HOUR_TO_METERS_PER_SEC
const float KILOMETERS_PER_HOUR_TO_METERS_PER_SEC
Definition: Constants.c:6
SCR_VehicleCameraDataComponent
Definition: SCR_VehicleCameraDataComponent.c:7