Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Wheel.h
Go to the documentation of this file.
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
10#include <Jolt/Core/StreamIn.h>
11#include <Jolt/Core/StreamOut.h>
12
14
16
18class WheelSettings : public SerializableObject, public RefTarget<WheelSettings>, public NonCopyable
19{
20public:
22
23
24 virtual void SaveBinaryState(StreamOut &inStream) const;
25
27 virtual void RestoreBinaryState(StreamIn &inStream);
28
29 Vec3 mPosition { 0, 0, 0 };
31 Vec3 mSteeringAxis { 0, 1, 0 };
32 Vec3 mWheelUp { 0, 1, 0 };
33 Vec3 mWheelForward { 0, 0, 1 };
34 float mSuspensionMinLength = 0.3f;
35 float mSuspensionMaxLength = 0.5f;
37 float mSuspensionFrequency = 1.5f;
38 float mSuspensionDamping = 0.5f;
39 float mRadius = 0.3f;
40 float mWidth = 0.1f;
41};
42
44class Wheel
45{
46public:
48
50 explicit Wheel(const WheelSettings &inSettings);
51 virtual ~Wheel() = default;
52
54 const WheelSettings * GetSettings() const { return mSettings; }
55
57 float GetAngularVelocity() const { return mAngularVelocity; }
58
60 void SetAngularVelocity(float inVel) { mAngularVelocity = inVel; }
61
63 float GetRotationAngle() const { return mAngle; }
64
66 void SetRotationAngle(float inAngle) { mAngle = inAngle; }
67
69 float GetSteerAngle() const { return mSteerAngle; }
70
72 void SetSteerAngle(float inAngle) { mSteerAngle = inAngle; }
73
75 inline bool HasContact() const { return !mContactBodyID.IsInvalid(); }
76
79
82
85
88
91
94
97
99 float GetSuspensionLength() const { return mSuspensionLength; }
100
103
106
109
111 float GetLateralLambda() const { return mLateralPart.GetTotalLambda(); }
112
114 bool SolveLongitudinalConstraintPart(const VehicleConstraint &inConstraint, float inMinImpulse, float inMaxImpulse);
115
117 bool SolveLateralConstraintPart(const VehicleConstraint &inConstraint, float inMinImpulse, float inMaxImpulse);
118
119protected:
120 friend class VehicleConstraint;
121
125 Body * mContactBody = nullptr;
133 float mAntiRollBarImpulse = 0.0f;
134
135 float mSteerAngle = 0.0f;
136 float mAngularVelocity = 0.0f;
137 float mAngle = 0.0f;
138
143};
144
146
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
float Real
Definition: Real.h:27
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(class_name)
Definition: SerializableObject.h:100
Array< Wheel * > Wheels
Definition: Wheel.h:145
Definition: AxisConstraintPart.h:42
float GetTotalLambda() const
Return lagrange multiplier.
Definition: AxisConstraintPart.h:378
bool IsActive() const
Check if constraint is active.
Definition: AxisConstraintPart.h:196
Definition: Body.h:33
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
bool IsInvalid() const
Check if the ID is valid.
Definition: BodyID.h:64
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Definition: Reference.h:154
Definition: Reference.h:35
Definition: SerializableObject.h:147
Simple binary input stream.
Definition: StreamIn.h:11
Simple binary output stream.
Definition: StreamOut.h:11
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition: SubShapeID.h:23
Definition: Vec3.h:16
Definition: VehicleConstraint.h:67
Base class for runtime data for a wheel, each VehicleController can implement a derived class of this...
Definition: Wheel.h:45
RefConst< WheelSettings > mSettings
Configuration settings for this wheel.
Definition: Wheel.h:122
float GetRotationAngle() const
Get the current rotation angle of the wheel in radians [0, 2 pi].
Definition: Wheel.h:63
bool SolveLateralConstraintPart(const VehicleConstraint &inConstraint, float inMinImpulse, float inMaxImpulse)
Internal function that should only be called by the controller. Used to apply impulses in the sideway...
Definition: Wheel.cpp:83
float GetSteerAngle() const
Get the current steer angle of the wheel in radians [-pi, pi], positive is to the left.
Definition: Wheel.h:69
Vec3 mContactNormal
Normal of the contact point between wheel and ground.
Definition: Wheel.h:129
float mAntiRollBarImpulse
Amount of impulse applied to the suspension from the anti-rollbars.
Definition: Wheel.h:133
Vec3 GetContactLateral() const
Returns lateral direction (sideways direction) in world space (note by the time you call this the veh...
Definition: Wheel.h:96
float mAngularVelocity
Rotation speed of wheel, positive when the wheels cause the vehicle to move forwards (rad/s)
Definition: Wheel.h:136
void SetAngularVelocity(float inVel)
Update the angular velocity (rad/s)
Definition: Wheel.h:60
float GetLateralLambda() const
Get total impulse (N s) applied along the sideways direction of the wheel.
Definition: Wheel.h:111
AxisConstraintPart mLateralPart
Controls movement sideways (slip)
Definition: Wheel.h:142
Vec3 mContactLongitudinal
Vector perpendicular to normal in the forward direction.
Definition: Wheel.h:130
float mSuspensionLength
Current length of the suspension.
Definition: Wheel.h:126
SubShapeID GetContactSubShapeID() const
Returns the sub shape ID where we're contacting the body.
Definition: Wheel.h:81
Vec3 GetContactLongitudinal() const
Returns longitudinal direction (direction along the wheel relative to floor) in world space (note by ...
Definition: Wheel.h:93
BodyID mContactBodyID
ID of body for ground.
Definition: Wheel.h:123
AxisConstraintPart mSuspensionMaxUpPart
Adds a hard limit when reaching the minimal suspension length.
Definition: Wheel.h:140
bool HasHitHardPoint() const
Check if the suspension hit its upper limit.
Definition: Wheel.h:102
Vec3 mContactPointVelocity
Velocity of the contact point (m / s, not relative to the wheel but in world space)
Definition: Wheel.h:128
void SetSteerAngle(float inAngle)
Set the current steer angle of the wheel in radians [-pi, pi].
Definition: Wheel.h:72
Vec3 GetContactPointVelocity() const
Velocity of the contact point (m / s, not relative to the wheel but in world space)
Definition: Wheel.h:87
AxisConstraintPart mSuspensionPart
Controls movement up/down along the contact normal.
Definition: Wheel.h:139
virtual ~Wheel()=default
Real mAxlePlaneConstant
Constant for the contact plane of the axle, defined as ContactNormal . (WorldSpaceSuspensionPoint + S...
Definition: Wheel.h:132
void SetRotationAngle(float inAngle)
Set the current rotation angle of the wheel in radians [0, 2 pi].
Definition: Wheel.h:66
AxisConstraintPart mLongitudinalPart
Controls movement forward/backward.
Definition: Wheel.h:141
Vec3 GetContactNormal() const
Returns the current contact mormal in world space (note by the time you call this the vehicle has mov...
Definition: Wheel.h:90
const WheelSettings * GetSettings() const
Get settings for the wheel.
Definition: Wheel.h:54
RVec3 mContactPosition
Position of the contact point between wheel and ground.
Definition: Wheel.h:127
bool SolveLongitudinalConstraintPart(const VehicleConstraint &inConstraint, float inMinImpulse, float inMaxImpulse)
Internal function that should only be called by the controller. Used to apply impulses in the forward...
Definition: Wheel.cpp:78
float GetLongitudinalLambda() const
Get total impulse (N s) applied along the forward direction of the wheel.
Definition: Wheel.h:108
Body * mContactBody
Body for ground.
Definition: Wheel.h:125
BodyID GetContactBodyID() const
Returns the body ID of the body that this wheel is touching.
Definition: Wheel.h:78
float GetSuspensionLength() const
Get the length of the suspension for a wheel (m) relative to the suspension attachment point (hard po...
Definition: Wheel.h:99
float mAngle
Current rotation of the wheel (rad, [0, 2 pi])
Definition: Wheel.h:137
float GetAngularVelocity() const
Get the angular velocity (rad/s) for this wheel, note that positive means the wheel is rotating such ...
Definition: Wheel.h:57
bool HasContact() const
Returns true if the wheel is touching an object.
Definition: Wheel.h:75
Vec3 mContactLateral
Vector perpendicular to normal and longitudinal direction in the right direction.
Definition: Wheel.h:131
float mSteerAngle
Rotation around the suspension direction, positive is to the left.
Definition: Wheel.h:135
float GetSuspensionLambda() const
Get the total impulse (N s) that was applied by the suspension.
Definition: Wheel.h:105
SubShapeID mContactSubShapeID
Sub shape ID for ground.
Definition: Wheel.h:124
RVec3 GetContactPosition() const
Returns the current contact position in world space (note by the time you call this the vehicle has m...
Definition: Wheel.h:84
Base class for wheel settings, each VehicleController can implement a derived class of this.
Definition: Wheel.h:19
float mSuspensionPreloadLength
The natural length (m) of the suspension spring is defined as mSuspensionMaxLength + mSuspensionPrelo...
Definition: Wheel.h:36
Vec3 mSuspensionDirection
Direction of the suspension in local space of the body, should point down.
Definition: Wheel.h:30
float mSuspensionMaxLength
How long the suspension is in max droop position relative to the attachment point (m)
Definition: Wheel.h:35
float mWidth
Width of the wheel (m)
Definition: Wheel.h:40
Vec3 mPosition
Attachment point of wheel suspension in local space of the body.
Definition: Wheel.h:29
virtual void SaveBinaryState(StreamOut &inStream) const
Saves the contents in binary form to inStream.
Definition: Wheel.cpp:29
float mSuspensionDamping
Damping factor of the suspension spring (0 = no damping, 1 = critical damping)
Definition: Wheel.h:38
virtual void RestoreBinaryState(StreamIn &inStream)
Restores the contents in binary form to inStream.
Definition: Wheel.cpp:45
Vec3 mWheelUp
Up direction when the wheel is in the neutral steering position (usually VehicleConstraintSettings::m...
Definition: Wheel.h:32
Vec3 mSteeringAxis
Direction of the steering axis in local space of the body, should point up (e.g. for a bike would be ...
Definition: Wheel.h:31
float mSuspensionFrequency
Natural frequency of the suspension spring (Hz)
Definition: Wheel.h:37
Vec3 mWheelForward
Forward direction when the wheel is in the neutral steering position (usually VehicleConstraintSettin...
Definition: Wheel.h:33
float mRadius
Radius of the wheel (m)
Definition: Wheel.h:39
float mSuspensionMinLength
How long the suspension is in max raised position relative to the attachment point (m)
Definition: Wheel.h:34