Newton Dynamics  4.00
ndBodyPlayerCapsule.h
1 /* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __ND_BODY_PLAYER_CAPSULE_H__
23 #define __ND_BODY_PLAYER_CAPSULE_H__
24 
25 #include "ndCollisionStdafx.h"
26 #include "ndBodyKinematicBase.h"
27 
30 
31 D_MSV_NEWTON_ALIGN_32
33 {
34  public:
35  D_CLASS_REFLECTION(ndBodyPlayerCapsule);
36  D_COLLISION_API ndBodyPlayerCapsule(const ndLoadSaveBase::ndLoadDescriptor& desc);
37  D_COLLISION_API ndBodyPlayerCapsule(const ndMatrix& localAxis, ndFloat32 mass, ndFloat32 radius, ndFloat32 height, ndFloat32 stepHeight);
38  D_COLLISION_API virtual ~ndBodyPlayerCapsule();
39 
40  ndBodyPlayerCapsule* GetAsBodyPlayerCapsule();
41 
42  ndFloat32 GetForwardSpeed() const;
43  void SetForwardSpeed(ndFloat32 speed);
44 
45  ndFloat32 GetLateralSpeed() const;
46  void SetLateralSpeed(ndFloat32 speed);
47 
48  ndFloat32 GetHeadingAngle() const;
49  void SetHeadingAngle(ndFloat32 angle);
50 
51  bool IsOnFloor() const;
52 
53  virtual void ApplyInputs(ndFloat32 timestep);
54  virtual ndFloat32 ContactFrictionCallback(const ndVector& position, const ndVector& normal, ndInt32 contactId, const ndBodyKinematic* const otherbody) const;
55 
56  private:
57  enum dCollisionState
58  {
59  m_colliding,
60  m_freeMovement,
61  m_deepPenetration,
62  };
63 
64  virtual void IntegrateExternalForce(ndFloat32 timestep);
65  D_COLLISION_API virtual void SpecialUpdate(ndFloat32 timestep);
66  virtual void SetCollisionShape(const ndShapeInstance& shapeInstance);
67 
68  void UpdatePlayerStatus(ndBodyPlayerCapsuleContactSolver& contactSolver);
69  void ResolveStep(ndBodyPlayerCapsuleContactSolver& contactSolver, ndFloat32 timestep);
70  void ResolveCollision(ndBodyPlayerCapsuleContactSolver& contactSolver, ndFloat32 timestep);
71  ndFloat32 PredictTimestep(ndBodyPlayerCapsuleContactSolver& contactSolver, ndFloat32 timestep);
72  dCollisionState TestPredictCollision(const ndBodyPlayerCapsuleContactSolver& contactSolver, const ndVector& veloc) const;
73  void ResolveInterpenetrations(ndBodyPlayerCapsuleContactSolver& contactSolver, ndBodyPlayerCapsuleImpulseSolver& impulseSolver);
74 
75  void IntegrateVelocity(ndFloat32 timestep);
76 
77  protected:
78  D_COLLISION_API void Save(const ndLoadSaveBase::ndSaveDescriptor& desc) const;
79 
80  ndMatrix m_localFrame;
81  ndVector m_impulse;
82  ndFloat32 m_mass;
83  ndFloat32 m_invMass;
84  ndFloat32 m_headingAngle;
85  ndFloat32 m_forwardSpeed;
86  ndFloat32 m_lateralSpeed;
87  ndFloat32 m_stepHeight;
88  ndFloat32 m_contactPatch;
89  ndFloat32 m_height;
90  ndFloat32 m_radius;
91  ndFloat32 m_weistScale;
92  ndFloat32 m_crouchScale;
93  bool m_isAirbone;
94  bool m_isOnFloor;
95  bool m_isCrouched;
96 } D_GCC_NEWTON_ALIGN_32;
97 
98 inline ndBodyPlayerCapsule* ndBodyPlayerCapsule::GetAsBodyPlayerCapsule()
99 {
100  return this;
101 }
102 
103 inline void ndBodyPlayerCapsule::SetCollisionShape(const ndShapeInstance&)
104 {
105  // ignore the changing collision shape;
106 }
107 
108 inline void ndBodyPlayerCapsule::ApplyInputs(ndFloat32)
109 {
110 }
111 
112 inline ndFloat32 ndBodyPlayerCapsule::ContactFrictionCallback(const ndVector&, const ndVector&, ndInt32, const ndBodyKinematic* const) const
113 {
114  return ndFloat32 (2.0f);
115 }
116 
117 inline ndFloat32 ndBodyPlayerCapsule::GetForwardSpeed() const
118 {
119  return -m_forwardSpeed;
120 }
121 
122 inline void ndBodyPlayerCapsule::SetForwardSpeed(ndFloat32 speed)
123 {
124  m_forwardSpeed = -speed;
125 }
126 
127 inline ndFloat32 ndBodyPlayerCapsule::GetLateralSpeed() const
128 {
129  return -m_lateralSpeed;
130 }
131 
132 inline void ndBodyPlayerCapsule::SetLateralSpeed(ndFloat32 speed)
133 {
134  m_lateralSpeed = -speed;
135 }
136 
137 inline ndFloat32 ndBodyPlayerCapsule::GetHeadingAngle() const
138 {
139  return m_headingAngle;
140 }
141 
142 inline void ndBodyPlayerCapsule::SetHeadingAngle(ndFloat32 angle)
143 {
144  //m_headingAngle = dClamp(angle, ndFloat32(-dPi), ndFloat32(dPi));
145  const ndFloat32 interpolation = ndFloat32(0.3f);
146  ndFloat32 deltaAngle = ndAnglesAdd(angle, -m_headingAngle) * interpolation;
147  ndFloat32 headingAngle = ndAnglesAdd(m_headingAngle, deltaAngle);
148  //dTrace(("%f %f %f\n", angle * dRadToDegree, m_headingAngle * dRadToDegree, headingAngle * dRadToDegree));
149  m_headingAngle = headingAngle;
150 }
151 
152 inline void ndBodyPlayerCapsule::IntegrateVelocity(ndFloat32)
153 {
154  m_accel = ndVector::m_zero;
155  m_alpha = ndVector::m_zero;
156 }
157 
158 inline bool ndBodyPlayerCapsule::IsOnFloor() const
159 {
160  return m_isOnFloor;
161 }
162 
163 inline void ndBodyPlayerCapsule::IntegrateExternalForce(ndFloat32)
164 {
165  // do nothing
166 }
167 
168 #endif
ndBodyPlayerCapsuleImpulseSolver
Definition: ndBodyPlayerCapsule.cpp:55
ndBodyPlayerCapsuleContactSolver
Definition: ndBodyPlayerCapsule.cpp:43
ndBodyKinematic
Definition: ndBodyKinematic.h:40
ndMatrix
Definition: ndMatrix.h:42
ndBodyPlayerCapsule
Definition: ndBodyPlayerCapsule.h:33
ndBodyKinematicBase
Definition: ndBodyKinematicBase.h:30
ndShapeInstance
Definition: ndShapeInstance.h:62
ndLoadSaveBase::ndLoadDescriptor
Definition: ndSaveLoadSytem.h:59
ndLoadSaveBase::ndSaveDescriptor
Definition: ndSaveLoadSytem.h:93
ndVector
Definition: ndVectorArmNeon.h:41