Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Vehicle/Wheel.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <Jolt/Physics/Body/Body.h>
8 #include <Jolt/Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
9 #include <Jolt/ObjectStream/SerializableObject.h>
10 #include <Jolt/Core/StreamIn.h>
11 #include <Jolt/Core/StreamOut.h>
12
13 JPH_NAMESPACE_BEGIN
14
15 class VehicleConstraint;
16
17 /// Base class for wheel settings, each VehicleController can implement a derived class of this
18 class JPH_EXPORT WheelSettings : public SerializableObject, public RefTarget<WheelSettings>
19 {
20 JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, WheelSettings)
21
22 public:
23 /// Saves the contents in binary form to inStream.
24 virtual void SaveBinaryState(StreamOut &inStream) const;
25
26 /// Restores the contents in binary form to inStream.
27 virtual void RestoreBinaryState(StreamIn &inStream);
28
29 Vec3 mPosition { 0, 0, 0 }; ///< Attachment point of wheel suspension in local space of the body
30 Vec3 mSuspensionForcePoint { 0, 0, 0 }; ///< Where tire forces (suspension and traction) are applied, in local space of the body. A good default is the center of the wheel in its neutral pose. See mEnableSuspensionForcePoint.
31 Vec3 mSuspensionDirection { 0, -1, 0 }; ///< Direction of the suspension in local space of the body, should point down
32 Vec3 mSteeringAxis { 0, 1, 0 }; ///< Direction of the steering axis in local space of the body, should point up (e.g. for a bike would be -mSuspensionDirection)
33 Vec3 mWheelUp { 0, 1, 0 }; ///< Up direction when the wheel is in the neutral steering position (usually VehicleConstraintSettings::mUp but can be used to give the wheel camber or for a bike would be -mSuspensionDirection)
34 Vec3 mWheelForward { 0, 0, 1 }; ///< Forward direction when the wheel is in the neutral steering position (usually VehicleConstraintSettings::mForward but can be used to give the wheel toe, does not need to be perpendicular to mWheelUp)
35 float mSuspensionMinLength = 0.3f; ///< How long the suspension is in max raised position relative to the attachment point (m)
36 float mSuspensionMaxLength = 0.5f; ///< How long the suspension is in max droop position relative to the attachment point (m)
37 float mSuspensionPreloadLength = 0.0f; ///< The natural length (m) of the suspension spring is defined as mSuspensionMaxLength + mSuspensionPreloadLength. Can be used to preload the suspension as the spring is compressed by mSuspensionPreloadLength when the suspension is in max droop position. Note that this means when the vehicle touches the ground there is a discontinuity so it will also make the vehicle more bouncy as we're updating with discrete time steps.
38 SpringSettings mSuspensionSpring { ESpringMode::FrequencyAndDamping, 1.5f, 0.5f }; ///< Settings for the suspension spring
39 float mRadius = 0.3f; ///< Radius of the wheel (m)
40 float mWidth = 0.1f; ///< Width of the wheel (m)
41 bool mEnableSuspensionForcePoint = false; ///< Enables mSuspensionForcePoint, if disabled, the forces are applied at the collision contact point. This leads to a more accurate simulation when interacting with dynamic objects but makes the vehicle less stable. When setting this to true, all forces will be applied to a fixed point on the vehicle body.
42 };
43
44 /// Base class for runtime data for a wheel, each VehicleController can implement a derived class of this
45 class JPH_EXPORT Wheel : public NonCopyable
46 {
47 public:
48 JPH_OVERRIDE_NEW_DELETE
49
50 /// Constructor / destructor
51 explicit Wheel(const WheelSettings &inSettings);
52 virtual ~Wheel() = default;
53
54 /// Get settings for the wheel
55 const WheelSettings * GetSettings() const { return mSettings; }
56
57 /// Get the angular velocity (rad/s) for this wheel, note that positive means the wheel is rotating such that the car moves forward
58 float GetAngularVelocity() const { return mAngularVelocity; }
59
60 /// Update the angular velocity (rad/s)
61 void SetAngularVelocity(float inVel) { mAngularVelocity = inVel; }
62
63 /// Get the current rotation angle of the wheel in radians [0, 2 pi]
64 float GetRotationAngle() const { return mAngle; }
65
66 /// Set the current rotation angle of the wheel in radians [0, 2 pi]
67 void SetRotationAngle(float inAngle) { mAngle = inAngle; }
68
69 /// Get the current steer angle of the wheel in radians [-pi, pi], positive is to the left
70 float GetSteerAngle() const { return mSteerAngle; }
71
72 /// Set the current steer angle of the wheel in radians [-pi, pi]
73 void SetSteerAngle(float inAngle) { mSteerAngle = inAngle; }
74
75 /// Returns true if the wheel is touching an object
76 inline bool HasContact() const { return !mContactBodyID.IsInvalid(); }
77
78 /// Returns the body ID of the body that this wheel is touching
79 BodyID GetContactBodyID() const { return mContactBodyID; }
80
81 /// Returns the sub shape ID where we're contacting the body
82 SubShapeID GetContactSubShapeID() const { return mContactSubShapeID; }
83
84 /// Returns the current contact position in world space (note by the time you call this the vehicle has moved)
85 RVec3 GetContactPosition() const { JPH_ASSERT(HasContact()); return mContactPosition; }
86
87 /// Velocity of the contact point (m / s, not relative to the wheel but in world space)
88 Vec3 GetContactPointVelocity() const { JPH_ASSERT(HasContact()); return mContactPointVelocity; }
89
90 /// Returns the current contact normal in world space (note by the time you call this the vehicle has moved)
91 Vec3 GetContactNormal() const { JPH_ASSERT(HasContact()); return mContactNormal; }
92
93 /// Returns longitudinal direction (direction along the wheel relative to floor) in world space (note by the time you call this the vehicle has moved)
94 Vec3 GetContactLongitudinal() const { JPH_ASSERT(HasContact()); return mContactLongitudinal; }
95
96 /// Returns lateral direction (sideways direction) in world space (note by the time you call this the vehicle has moved)
97 Vec3 GetContactLateral() const { JPH_ASSERT(HasContact()); return mContactLateral; }
98
99 /// Get the length of the suspension for a wheel (m) relative to the suspension attachment point (hard point)
100 float GetSuspensionLength() const { return mSuspensionLength; }
101
102 /// Check if the suspension hit its upper limit
103 bool HasHitHardPoint() const { return mSuspensionMaxUpPart.IsActive(); }
104
105 /// Get the total impulse (N s) that was applied by the suspension
106 float GetSuspensionLambda() const { return mSuspensionPart.GetTotalLambda() + mSuspensionMaxUpPart.GetTotalLambda(); }
107
108 /// Get total impulse (N s) applied along the forward direction of the wheel
109 float GetLongitudinalLambda() const { return mLongitudinalPart.GetTotalLambda(); }
110
111 /// Get total impulse (N s) applied along the sideways direction of the wheel
112 float GetLateralLambda() const { return mLateralPart.GetTotalLambda(); }
113
114 /// Internal function that should only be called by the controller. Used to apply impulses in the forward direction of the vehicle.
115 bool SolveLongitudinalConstraintPart(const VehicleConstraint &inConstraint, float inMinImpulse, float inMaxImpulse);
116
117 /// Internal function that should only be called by the controller. Used to apply impulses in the sideways direction of the vehicle.
118 bool SolveLateralConstraintPart(const VehicleConstraint &inConstraint, float inMinImpulse, float inMaxImpulse);
119
120 protected:
121 friend class VehicleConstraint;
122
123 RefConst<WheelSettings> mSettings; ///< Configuration settings for this wheel
124 BodyID mContactBodyID; ///< ID of body for ground
125 SubShapeID mContactSubShapeID; ///< Sub shape ID for ground
126 Body * mContactBody = nullptr; ///< Body for ground
127 float mSuspensionLength; ///< Current length of the suspension
128 RVec3 mContactPosition; ///< Position of the contact point between wheel and ground
129 Vec3 mContactPointVelocity; ///< Velocity of the contact point (m / s, not relative to the wheel but in world space)
130 Vec3 mContactNormal; ///< Normal of the contact point between wheel and ground
131 Vec3 mContactLongitudinal; ///< Vector perpendicular to normal in the forward direction
132 Vec3 mContactLateral; ///< Vector perpendicular to normal and longitudinal direction in the right direction
133 Real mAxlePlaneConstant; ///< Constant for the contact plane of the axle, defined as ContactNormal . (WorldSpaceSuspensionPoint + SuspensionLength * WorldSpaceSuspensionDirection)
134 float mAntiRollBarImpulse = 0.0f; ///< Amount of impulse applied to the suspension from the anti-rollbars
135
136 float mSteerAngle = 0.0f; ///< Rotation around the suspension direction, positive is to the left
137 float mAngularVelocity = 0.0f; ///< Rotation speed of wheel, positive when the wheels cause the vehicle to move forwards (rad/s)
138 float mAngle = 0.0f; ///< Current rotation of the wheel (rad, [0, 2 pi])
139
140 AxisConstraintPart mSuspensionPart; ///< Controls movement up/down along the contact normal
141 AxisConstraintPart mSuspensionMaxUpPart; ///< Adds a hard limit when reaching the minimal suspension length
142 AxisConstraintPart mLongitudinalPart; ///< Controls movement forward/backward
143 AxisConstraintPart mLateralPart; ///< Controls movement sideways (slip)
144 };
145
146 using Wheels = Array<Wheel *>;
147
148 JPH_NAMESPACE_END