Newton Dynamics  4.00
ndMultiBodyVehicle.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_MULTIBODY_VEHICLE_H__
23 #define __ND_MULTIBODY_VEHICLE_H__
24 
25 #include "ndNewtonStdafx.h"
26 #include "ndModel.h"
27 #include "ndIkSolver.h"
28 
29 class ndWorld;
37 
38 #define dRadPerSecToRpm ndFloat32(9.55f)
39 
41 {
42  public:
44  {
45  public:
46  ndContact* m_contact;
47  ndMultiBodyVehicleTireJoint* m_tireJoint;
48  };
49 
51  {
52  public:
54  {
55  public:
56  ndFloat32 m_speed;
57  ndFloat32 m_forceFactor;
58  ndFloat32 m_aerodynamicDownforceConstant;
59  friend class ndDownForce;
60  };
61 
62  ndDownForce();
63  ndFloat32 GetDownforceFactor(ndFloat32 speed) const;
64 
65  void Load(const nd::TiXmlNode* const xmlNode);
66  void Save(nd::TiXmlNode* const xmlNode) const;
67 
68  private:
69  ndFloat32 CalculateFactor(const ndSpeedForcePair* const entry) const;
70 
71  ndFloat32 m_gravity;
72  ndFloat32 m_suspensionStiffnessModifier;
73  ndSpeedForcePair m_downForceTable[5];
74  friend class ndMultiBodyVehicle;
75  friend class ndMultiBodyVehicleTireJoint;
76  };
77 
78  D_CLASS_REFLECTION(ndMultiBodyVehicle);
79  D_NEWTON_API ndMultiBodyVehicle(const ndLoadSaveBase::ndLoadDescriptor& desc);
80  D_NEWTON_API ndMultiBodyVehicle(const ndVector& frontDir, const ndVector& upDir);
81  D_NEWTON_API virtual ~ndMultiBodyVehicle ();
82 
83  D_NEWTON_API ndFloat32 GetSpeed() const;
84  D_NEWTON_API ndShapeInstance CreateTireShape(ndFloat32 radius, ndFloat32 width) const;
85 
86  D_NEWTON_API void AddChassis(ndBodyKinematic* const chassis);
87  D_NEWTON_API ndMultiBodyVehicleMotor* AddMotor(ndFloat32 mass, ndFloat32 radius);
88  D_NEWTON_API ndMultiBodyVehicleGearBox* AddGearBox(ndMultiBodyVehicleDifferential* const differential);
89  D_NEWTON_API ndMultiBodyVehicleTireJoint* AddTire(const ndMultiBodyVehicleTireJointInfo& desc, ndBodyKinematic* const tire);
90  D_NEWTON_API ndMultiBodyVehicleTireJoint* AddAxleTire(const ndMultiBodyVehicleTireJointInfo& desc, ndBodyKinematic* const tire, ndBodyKinematic* const axleBody);
91  D_NEWTON_API ndMultiBodyVehicleDifferential* AddDifferential(ndFloat32 mass, ndFloat32 radius, ndMultiBodyVehicleTireJoint* const leftTire, ndMultiBodyVehicleTireJoint* const rightTire, ndFloat32 slipOmegaLock);
92  D_NEWTON_API ndMultiBodyVehicleDifferential* AddDifferential(ndFloat32 mass, ndFloat32 radius, ndMultiBodyVehicleDifferential* const leftDifferential, ndMultiBodyVehicleDifferential* const rightDifferential, ndFloat32 slipOmegaLock);
93  D_NEWTON_API ndMultiBodyVehicleTorsionBar* AddTorsionBar(ndBodyKinematic* const sentinel);
94 
95  D_NEWTON_API void SetVehicleSolverModel(bool hardJoint);
96 
97  D_NEWTON_API ndMultiBodyVehicle* GetAsMultiBodyVehicle();
98 
99  private:
100  void ApplyAerodynamics();
101  void ApplyAligmentAndBalancing();
102  void ApplyTireModel(ndFloat32 timestep);
103  ndBodyKinematic* CreateInternalBodyPart(ndFloat32 mass, ndFloat32 radius) const;
104  void ApplyTireModel(ndFloat32 timestep, ndTireContactPair* const tires, ndInt32 tireCount);
105  void ApplyVehicleDynamicControl(ndFloat32 timestep, ndTireContactPair* const tires, ndInt32 tireCount);
106 
107  void CalculateNormalizedAlgningTorque(ndMultiBodyVehicleTireJoint* const tire, ndFloat32 sideSlipTangent) const;
108  void CoulombTireModel(ndMultiBodyVehicleTireJoint* const tire, ndContactMaterial& contactPoint, ndFloat32 timestep) const;
109  void BrushTireModel(ndMultiBodyVehicleTireJoint* const tire, ndContactMaterial& contactPoint, ndFloat32 timestep) const;
110  void PacejkaTireModel(ndMultiBodyVehicleTireJoint* const tire, ndContactMaterial& contactPoint, ndFloat32 timestep) const;
111  void CoulombFrictionCircleTireModel(ndMultiBodyVehicleTireJoint* const tire, ndContactMaterial& contactPoint, ndFloat32 timestep) const;
112 
113  protected:
114  bool isActive() const;
115  virtual void ApplyInputs(ndWorld* const world, ndFloat32 timestep);
116  D_NEWTON_API virtual void RemoveFromToWorld();
117  D_NEWTON_API virtual void AddToWorld(ndWorld* const world);
118  D_NEWTON_API virtual void Debug(ndConstraintDebugCallback& context) const;
119  D_NEWTON_API virtual void Update(ndWorld* const world, ndFloat32 timestep);
120  D_NEWTON_API virtual void PostUpdate(ndWorld* const world, ndFloat32 timestep);
121  D_NEWTON_API virtual void Save(const ndLoadSaveBase::ndSaveDescriptor& desc) const;
122 
123  ndMatrix m_localFrame;
124  ndBodyKinematic* m_chassis;
125  ndIkSolver m_invDynamicsSolver;
126  ndShapeChamferCylinder* m_tireShape;
127  ndReferencedObjects<ndBody> m_internalBodies;
134  ndDownForce m_downForce;
135 
136  friend class ndMultiBodyVehicleMotor;
137  friend class ndMultiBodyVehicleGearBox;
138  friend class ndMultiBodyVehicleTireJoint;
139  friend class ndMultiBodyVehicleTorsionBar;
140 };
141 
142 inline void ndMultiBodyVehicle::ApplyInputs(ndWorld* const, ndFloat32)
143 {
144 }
145 
146 #endif
nd::TiXmlNode
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:437
ndMultiBodyVehicle
Definition: ndMultiBodyVehicle.h:41
ndMultiBodyVehicleTireJoint
Definition: ndMultiBodyVehicleTireJoint.h:52
ndSharedPtr< ndMultiBodyVehicleMotor >
ndMultiBodyVehicleTorsionBar
Definition: ndMultiBodyVehicleTorsionBar.h:31
ndModel
Definition: ndModel.h:33
ndShapeChamferCylinder
Definition: ndShapeChamferCylinder.h:32
ndMultiBodyVehicleDifferentialAxle
Definition: ndMultiBodyVehicleDifferentialAxle.h:29
ndContact
Definition: ndContact.h:97
ndConstraintDebugCallback
Definition: ndConstraint.h:42
ndBodyKinematic
Definition: ndBodyKinematic.h:40
ndMultiBodyVehicle::ndDownForce::ndSpeedForcePair
Definition: ndMultiBodyVehicle.h:54
ndMatrix
Definition: ndMatrix.h:42
ndContactMaterial
Definition: ndContact.h:54
ndModel::ndReferencedObjects
Definition: ndModel.h:37
ndMultiBodyVehicleMotor
Definition: ndMultiBodyVehicleMotor.h:31
ndMultiBodyVehicle::ndTireContactPair
Definition: ndMultiBodyVehicle.h:44
ndMultiBodyVehicleTireJointInfo
Definition: ndMultiBodyVehicleTireJoint.h:42
ndShapeInstance
Definition: ndShapeInstance.h:62
ndLoadSaveBase::ndLoadDescriptor
Definition: ndSaveLoadSytem.h:59
ndLoadSaveBase::ndSaveDescriptor
Definition: ndSaveLoadSytem.h:93
ndMultiBodyVehicleDifferential
Definition: ndMultiBodyVehicleDifferential.h:31
ndMultiBodyVehicleGearBox
Definition: ndMultiBodyVehicleGearBox.h:31
ndVector
Definition: ndVectorArmNeon.h:41
ndIkSolver
Definition: ndIkSolver.h:32
ndWorld
Definition: ndWorld.h:47
ndMultiBodyVehicle::ndDownForce
Definition: ndMultiBodyVehicle.h:51