Jolt Physics
A multi core friendly Game Physics Engine
|
#include <VehicleConstraint.h>
Public Types | |
using | CombineFunction = function< void(uint inWheelIndex, float &ioLongitudinalFriction, float &ioLateralFriction, const Body &inBody2, const SubShapeID &inSubShapeID2)> |
using | StepCallback = function< void(VehicleConstraint &inVehicle, const PhysicsStepListenerContext &inContext)> |
Callback function to notify of current stage in PhysicsStepListener::OnStep. | |
Public Member Functions | |
VehicleConstraint (Body &inVehicleBody, const VehicleConstraintSettings &inSettings) | |
Constructor / destructor. | |
virtual | ~VehicleConstraint () override |
virtual EConstraintSubType | GetSubType () const override |
Get the type of a constraint. | |
void | SetMaxPitchRollAngle (float inMaxPitchRollAngle) |
Defines the maximum pitch/roll angle (rad), can be used to avoid the car from getting upside down. The vehicle up direction will stay within a cone centered around the up axis with half top angle mMaxPitchRollAngle, set to pi to turn off. | |
void | SetVehicleCollisionTester (const VehicleCollisionTester *inTester) |
Set the interface that tests collision between wheel and ground. | |
void | SetCombineFriction (const CombineFunction &inCombineFriction) |
const CombineFunction & | GetCombineFriction () const |
const StepCallback & | GetPreStepCallback () const |
void | SetPreStepCallback (const StepCallback &inPreStepCallback) |
const StepCallback & | GetPostCollideCallback () const |
void | SetPostCollideCallback (const StepCallback &inPostCollideCallback) |
const StepCallback & | GetPostStepCallback () const |
void | SetPostStepCallback (const StepCallback &inPostStepCallback) |
void | OverrideGravity (Vec3Arg inGravity) |
Override gravity for this vehicle. Note that overriding gravity will set the gravity factor of the vehicle body to 0 and apply gravity in the PhysicsStepListener instead. | |
bool | IsGravityOverridden () const |
Vec3 | GetGravityOverride () const |
void | ResetGravityOverride () |
Note that resetting the gravity override will restore the gravity factor of the vehicle body to 1. | |
Vec3 | GetLocalForward () const |
Get the local space forward vector of the vehicle. | |
Vec3 | GetLocalUp () const |
Get the local space up vector of the vehicle. | |
Vec3 | GetWorldUp () const |
Vector indicating the world space up direction (used to limit vehicle pitch/roll), calculated every frame by inverting gravity. | |
Body * | GetVehicleBody () const |
Access to the vehicle body. | |
const VehicleController * | GetController () const |
Access to the vehicle controller interface (determines acceleration / deceleration) | |
VehicleController * | GetController () |
Access to the vehicle controller interface (determines acceleration / deceleration) | |
const Wheels & | GetWheels () const |
Get the state of the wheels. | |
Wheels & | GetWheels () |
Get the state of a wheels (writable interface, allows you to make changes to the configuration which will take effect the next time step) | |
Wheel * | GetWheel (uint inIdx) |
Get the state of a wheel. | |
const Wheel * | GetWheel (uint inIdx) const |
void | GetWheelLocalBasis (const Wheel *inWheel, Vec3 &outForward, Vec3 &outUp, Vec3 &outRight) const |
Mat44 | GetWheelLocalTransform (uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const |
RMat44 | GetWheelWorldTransform (uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const |
void | SetNumStepsBetweenCollisionTestActive (uint inSteps) |
uint | GetNumStepsBetweenCollisionTestActive () const |
void | SetNumStepsBetweenCollisionTestInactive (uint inSteps) |
uint | GetNumStepsBetweenCollisionTestInactive () const |
virtual bool | IsActive () const override |
virtual void | NotifyShapeChanged (const BodyID &inBodyID, Vec3Arg inDeltaCOM) override |
virtual void | SetupVelocityConstraint (float inDeltaTime) override |
virtual void | ResetWarmStart () override |
virtual void | WarmStartVelocityConstraint (float inWarmStartImpulseRatio) override |
virtual bool | SolveVelocityConstraint (float inDeltaTime) override |
virtual bool | SolvePositionConstraint (float inDeltaTime, float inBaumgarte) override |
virtual void | BuildIslands (uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override |
Link bodies that are connected by this constraint in the island builder. | |
virtual uint | BuildIslandSplits (LargeIslandSplitter &ioSplitter) const override |
Link bodies that are connected by this constraint in the same split. Returns the split index. | |
virtual void | DrawConstraint (DebugRenderer *inRenderer) const override |
virtual void | DrawConstraintLimits (DebugRenderer *inRenderer) const override |
virtual void | SaveState (StateRecorder &inStream) const override |
Saving state for replay. | |
virtual void | RestoreState (StateRecorder &inStream) override |
Restoring state for replay. | |
virtual Ref< ConstraintSettings > | GetConstraintSettings () const override |
Debug function to convert a constraint to its settings, note that this will not save to which bodies the constraint is connected to. | |
Public Member Functions inherited from Constraint | |
JPH_OVERRIDE_NEW_DELETE | Constraint (const ConstraintSettings &inSettings) |
Constructor. | |
virtual | ~Constraint ()=default |
Virtual destructor. | |
virtual EConstraintType | GetType () const |
Get the type of a constraint. | |
uint32 | GetConstraintPriority () const |
void | SetConstraintPriority (uint32 inPriority) |
void | SetNumVelocityStepsOverride (uint inN) |
Used only when the constraint is active. Override for the number of solver velocity iterations to run, 0 means use the default in PhysicsSettings::mNumVelocitySteps. The number of iterations to use is the max of all contacts and constraints in the island. | |
uint | GetNumVelocityStepsOverride () const |
void | SetNumPositionStepsOverride (uint inN) |
Used only when the constraint is active. Override for the number of solver position iterations to run, 0 means use the default in PhysicsSettings::mNumPositionSteps. The number of iterations to use is the max of all contacts and constraints in the island. | |
uint | GetNumPositionStepsOverride () const |
void | SetEnabled (bool inEnabled) |
bool | GetEnabled () const |
Test if a constraint is enabled. | |
uint64 | GetUserData () const |
Access to the user data, can be used for anything by the application. | |
void | SetUserData (uint64 inUserData) |
virtual void | DrawConstraintReferenceFrame (DebugRenderer *inRenderer) const |
float | GetDrawConstraintSize () const |
Size of constraint when drawing it through the debug renderer. | |
void | SetDrawConstraintSize (float inSize) |
Public Member Functions inherited from RefTarget< Constraint > | |
RefTarget ()=default | |
Constructor. | |
RefTarget (const RefTarget &) | |
~RefTarget () | |
assert no one is referencing us | |
void | SetEmbedded () const |
RefTarget & | operator= (const RefTarget &) |
Assignment operator. | |
uint32 | GetRefCount () const |
Get current refcount of this object. | |
void | AddRef () const |
Add or release a reference to this object. | |
void | Release () const |
Public Member Functions inherited from NonCopyable | |
NonCopyable ()=default | |
NonCopyable (const NonCopyable &)=delete | |
void | operator= (const NonCopyable &)=delete |
Public Member Functions inherited from PhysicsStepListener | |
virtual | ~PhysicsStepListener ()=default |
Ensure virtual destructor. | |
Additional Inherited Members | |
Static Public Member Functions inherited from RefTarget< Constraint > | |
static int | sInternalGetRefCountOffset () |
INTERNAL HELPER FUNCTION USED BY SERIALIZATION. | |
Protected Member Functions inherited from Constraint | |
void | ToConstraintSettings (ConstraintSettings &outSettings) const |
Helper function to copy settings back to constraint settings for this base class. | |
Protected Attributes inherited from Constraint | |
float | mDrawConstraintSize |
Size of constraint when drawing it through the debug renderer. | |
Protected Attributes inherited from RefTarget< Constraint > | |
atomic< uint32 > | mRefCount |
Current reference count. | |
Static Protected Attributes inherited from RefTarget< Constraint > | |
static constexpr uint32 | cEmbedded |
A large value that gets added to the refcount to mark the object as embedded. | |
Constraint that simulates a vehicle Note: Don't forget to register the constraint as a StepListener with the PhysicsSystem!
When the vehicle drives over very light objects (rubble) you may see the car body dip down. This is a known issue and is an artifact of the iterative solver that Jolt is using. Basically if a light object is sandwiched between two heavy objects (the static floor and the car body), the light object is not able to transfer enough force from the ground to the car body to keep the car body up. You can see this effect in the HeavyOnLightTest sample, the boxes on the right have a lot of penetration because they're on top of light objects.
There are a couple of ways to improve this:
Note that when driving over rubble, you may see the wheel jump up and down quite quickly because one frame a collision is found and the next frame not. To alleviate this, it may be needed to smooth the motion of the visual mesh for the wheel.
using VehicleConstraint::CombineFunction = function<void(uint inWheelIndex, float &ioLongitudinalFriction, float &ioLateralFriction, const Body &inBody2, const SubShapeID &inSubShapeID2)> |
Callback function to combine the friction of a tire with the friction of the body it is colliding with. On input ioLongitudinalFriction and ioLateralFriction contain the friction of the tire, on output they should contain the combined friction with inBody2.
using VehicleConstraint::StepCallback = function<void(VehicleConstraint &inVehicle, const PhysicsStepListenerContext &inContext)> |
Callback function to notify of current stage in PhysicsStepListener::OnStep.
VehicleConstraint::VehicleConstraint | ( | Body & | inVehicleBody, |
const VehicleConstraintSettings & | inSettings | ||
) |
Constructor / destructor.
|
overridevirtual |
|
overridevirtual |
Link bodies that are connected by this constraint in the island builder.
Implements Constraint.
|
overridevirtual |
Link bodies that are connected by this constraint in the same split. Returns the split index.
Implements Constraint.
|
overridevirtual |
Implements Constraint.
|
overridevirtual |
Reimplemented from Constraint.
|
inline |
|
overridevirtual |
Debug function to convert a constraint to its settings, note that this will not save to which bodies the constraint is connected to.
Implements Constraint.
|
inline |
Access to the vehicle controller interface (determines acceleration / deceleration)
|
inline |
Access to the vehicle controller interface (determines acceleration / deceleration)
|
inline |
|
inline |
Get the local space forward vector of the vehicle.
|
inline |
Get the local space up vector of the vehicle.
|
inline |
|
inline |
|
inline |
Callback function to notify that PhysicsStepListener::OnStep has just completed wheel collision checks. Default is to do nothing. Can be used to allow higher-level code to e.g. detect tire contact or to modify the velocity of the vehicle based on the wheel contacts. You should not change the position of the vehicle in this callback as the wheel collision checks have already been performed.
|
inline |
Callback function to notify that PhysicsStepListener::OnStep has completed for this vehicle. Default is to do nothing. Can be used to allow higher-level code to e.g. control the vehicle in the air. You should not change the position of the vehicle in this callback as the wheel collision checks have already been performed.
|
inline |
Callback function to notify that PhysicsStepListener::OnStep has started for this vehicle. Default is to do nothing. Can be used to allow higher-level code to e.g. control steering. This is the last moment that the position/orientation of the vehicle can be changed. Wheel collision checks have not been performed yet.
|
inlineoverridevirtual |
Get the type of a constraint.
Implements Constraint.
|
inline |
Access to the vehicle body.
void VehicleConstraint::GetWheelLocalBasis | ( | const Wheel * | inWheel, |
Vec3 & | outForward, | ||
Vec3 & | outUp, | ||
Vec3 & | outRight | ||
) | const |
Get the basis vectors for the wheel in local space to the vehicle body (note: basis does not rotate when the wheel rotates around its axis)
inWheel | Wheel to fetch basis for |
outForward | Forward vector for the wheel |
outUp | Up vector for the wheel |
outRight | Right vector for the wheel |
Mat44 VehicleConstraint::GetWheelLocalTransform | ( | uint | inWheelIndex, |
Vec3Arg | inWheelRight, | ||
Vec3Arg | inWheelUp | ||
) | const |
Get the transform of a wheel in local space to the vehicle body, returns a matrix that transforms a cylinder aligned with the Y axis in body space (not COM space)
inWheelIndex | Index of the wheel to fetch |
inWheelRight | Unit vector that indicates right in model space of the wheel (so if you only have 1 wheel model, you probably want to specify the opposite direction for the left and right wheels) |
inWheelUp | Unit vector that indicates up in model space of the wheel |
|
inline |
Get the state of a wheels (writable interface, allows you to make changes to the configuration which will take effect the next time step)
|
inline |
Get the state of the wheels.
RMat44 VehicleConstraint::GetWheelWorldTransform | ( | uint | inWheelIndex, |
Vec3Arg | inWheelRight, | ||
Vec3Arg | inWheelUp | ||
) | const |
Get the transform of a wheel in world space, returns a matrix that transforms a cylinder aligned with the Y axis in world space
inWheelIndex | Index of the wheel to fetch |
inWheelRight | Unit vector that indicates right in model space of the wheel (so if you only have 1 wheel model, you probably want to specify the opposite direction for the left and right wheels) |
inWheelUp | Unit vector that indicates up in model space of the wheel |
|
inline |
Vector indicating the world space up direction (used to limit vehicle pitch/roll), calculated every frame by inverting gravity.
|
inlineoverridevirtual |
Reimplemented from Constraint.
|
inline |
|
inlineoverridevirtual |
Notify the constraint that the shape of a body has changed and that its center of mass has moved by inDeltaCOM. Bodies don't know which constraints are connected to them so the user is responsible for notifying the relevant constraints when a body changes.
inBodyID | ID of the body that has changed |
inDeltaCOM | The delta of the center of mass of the body (shape->GetCenterOfMass() - shape_before_change->GetCenterOfMass()) |
Implements Constraint.
|
inline |
Override gravity for this vehicle. Note that overriding gravity will set the gravity factor of the vehicle body to 0 and apply gravity in the PhysicsStepListener instead.
|
inline |
Note that resetting the gravity override will restore the gravity factor of the vehicle body to 1.
|
overridevirtual |
Notify the system that the configuration of the bodies and/or constraint has changed enough so that the warm start impulses should not be applied the next frame. You can use this function for example when repositioning a ragdoll through Ragdoll::SetPose in such a way that the orientation of the bodies completely changes so that the previous frame impulses are no longer a good approximation of what the impulses will be in the next frame. Calling this function when there are no big changes will result in the constraints being much 'softer' than usual so they are more easily violated (e.g. a long chain of bodies might sag a bit if you call this every frame).
Implements Constraint.
|
overridevirtual |
Restoring state for replay.
Reimplemented from Constraint.
|
overridevirtual |
Saving state for replay.
Reimplemented from Constraint.
|
inline |
Set the function that combines the friction of two bodies and returns it Default method is the geometric mean: sqrt(friction1 * friction2).
|
inline |
Defines the maximum pitch/roll angle (rad), can be used to avoid the car from getting upside down. The vehicle up direction will stay within a cone centered around the up axis with half top angle mMaxPitchRollAngle, set to pi to turn off.
|
inline |
Number of simulation steps between wheel collision tests when the vehicle is active. Default is 1. 0 = never, 1 = every step, 2 = every other step, etc. Note that if a vehicle has multiple wheels and the number of steps > 1, the wheels will be tested in a round robin fashion. If there are multiple vehicles, the tests will be spread out based on the BodyID of the vehicle. If you set this to test less than every step, you may see simulation artifacts. This setting can be used to reduce the cost of simulating vehicles in the distance.
|
inline |
Number of simulation steps between wheel collision tests when the vehicle is inactive. Default is 1. 0 = never, 1 = every step, 2 = every other step, etc. Note that if a vehicle has multiple wheels and the number of steps > 1, the wheels will be tested in a round robin fashion. If there are multiple vehicles, the tests will be spread out based on the BodyID of the vehicle. This number can be lower than the number of steps when the vehicle is active as the only purpose of this test is to allow the vehicle to wake up in response to bodies moving into the wheels but not touching the body of the vehicle.
|
inline |
|
inline |
|
inline |
|
overridevirtual |
Implements Constraint.
|
inline |
Set the interface that tests collision between wheel and ground.
|
overridevirtual |
Implements Constraint.
|
overridevirtual |
Implements Constraint.
|
overridevirtual |
Implements Constraint.