A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// SPDX-FileCopyrightText: Copyright (c) 2008-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
#include "vehicle/PxVehicleParams.h"
#include "vehicle/physxConstraints/PxVehiclePhysXConstraintFunctions.h"
#include "vehicle/physxConstraints/PxVehiclePhysXConstraintHelpers.h"
#include "vehicle/rigidBody/PxVehicleRigidBodyStates.h"
#include "vehicle/suspension/PxVehicleSuspensionParams.h"
#include "vehicle/suspension/PxVehicleSuspensionStates.h"
#include "vehicle/tire/PxVehicleTireStates.h"
#include "vehicle/physxConstraints/PxVehiclePhysXConstraintStates.h"
#include "vehicle/physxConstraints/PxVehiclePhysXConstraintParams.h"
namespace physx
{
PX_FORCE_INLINE PxVec3 computeAngular
(const PxVehicleSuspensionParams& suspParams,
const PxVehicleSuspensionComplianceState& suspComplianceState, const PxVehicleRigidBodyState& rigidBodyState,
const PxVec3& direction)
{
const PxVec3 cmOffset = rigidBodyState.pose.rotate(suspParams.suspensionAttachment.transform(suspComplianceState.tireForceAppPoint));
const PxVec3 angular = cmOffset.cross(direction);
// note: not normalized on purpose. The angular component should hold the raw cross product as that
// is needed for the type of constraint we want to set up (see vehicleConstraintSolverPrep).
return angular;
}
PX_FORCE_INLINE PxVec3 computeTireAngular
(const PxVehicleSuspensionParams& suspParams,
const PxVehicleSuspensionComplianceState& suspComplianceState, const PxVehicleTireDirectionState& trDirState, const PxVehicleRigidBodyState& rigidBodyState,
const PxVehicleTireDirectionModes::Enum direction)
{
return computeAngular(suspParams, suspComplianceState, rigidBodyState, trDirState.directions[direction]);
}
PX_FORCE_INLINE PxVec3 computeSuspAngular
(const PxVehicleSuspensionParams& suspParams,
const PxVehicleSuspensionComplianceState& suspComplianceState, const PxVehicleRigidBodyState& rigidBodyState,
const PxVec3& direction)
{
return computeAngular(suspParams, suspComplianceState, rigidBodyState, direction);
}
void PxVehiclePhysXConstraintStatesUpdate
(const PxVehicleSuspensionParams& suspParams,
const PxVehiclePhysXSuspensionLimitConstraintParams& suspensionLimitParams,
const PxVehicleSuspensionState& suspState, const PxVehicleSuspensionComplianceState& suspComplianceState,
const PxVec3& groundPlaneNormal,
const PxReal tireStickyDampingLong, const PxReal tireStickyDampingLat,
const PxVehicleTireDirectionState& trDirState, const PxVehicleTireStickyState& trStickyState,
const PxVehicleRigidBodyState& rigidBodyState,
PxVehiclePhysXConstraintState& cnstrtState)
{
cnstrtState.setToDefault();
//Sticky tire longitudinal
{
const bool isActive = trStickyState.activeStatus[PxVehicleTireDirectionModes::eLONGITUDINAL];
cnstrtState.tireActiveStatus[PxVehicleTireDirectionModes::eLONGITUDINAL] = isActive;
if (isActive)
{
cnstrtState.tireLinears[PxVehicleTireDirectionModes::eLONGITUDINAL] = trDirState.directions[PxVehicleTireDirectionModes::eLONGITUDINAL];
cnstrtState.tireAngulars[PxVehicleTireDirectionModes::eLONGITUDINAL] = computeTireAngular(suspParams, suspComplianceState, trDirState, rigidBodyState, PxVehicleTireDirectionModes::eLONGITUDINAL);
cnstrtState.tireDamping[PxVehicleTireDirectionModes::eLONGITUDINAL] = tireStickyDampingLong;
}
}
//Sticky tire lateral
{
const bool isActive = trStickyState.activeStatus[PxVehicleTireDirectionModes::eLATERAL];
cnstrtState.tireActiveStatus[PxVehicleTireDirectionModes::eLATERAL] = isActive;
if (isActive)
{
cnstrtState.tireLinears[PxVehicleTireDirectionModes::eLATERAL] = trDirState.directions[PxVehicleTireDirectionModes::eLATERAL];
cnstrtState.tireAngulars[PxVehicleTireDirectionModes::eLATERAL] = computeTireAngular(suspParams, suspComplianceState, trDirState, rigidBodyState, PxVehicleTireDirectionModes::eLATERAL);
cnstrtState.tireDamping[PxVehicleTireDirectionModes::eLATERAL] = tireStickyDampingLat;
}
}
//Suspension limit
{
if (suspState.separation >= 0.0f || PxVehiclePhysXSuspensionLimitConstraintParams::eNONE == suspensionLimitParams.directionForSuspensionLimitConstraint)
{
cnstrtState.suspActiveStatus = false;
}
else
{
// To maintain the wheel on the ground plane, the suspension is required to compress beyond the suspension compression limit
// or expand beyond maximum droop (for example, top of wheel being in collision with something).
// We manage the compression up to the limit with a suspension force.
// Everything beyond the limits is managed with an impulse applied to the rigid body via a constraint.
// The constraint attempts to resolve the geometric error declared in the separation state.
// We have two choices:
// 1) apply the impulse along the suspension dir (more like the effect of a bump stop spring)
// 2) apply the impulse along the ground normal (more like the effect of a real tire's contact wtih the ground).
if (PxVehiclePhysXSuspensionLimitConstraintParams::eROAD_GEOMETRY_NORMAL == suspensionLimitParams.directionForSuspensionLimitConstraint)
{
cnstrtState.suspActiveStatus = true;
cnstrtState.suspGeometricError = suspState.separation;
cnstrtState.suspLinear = groundPlaneNormal;
cnstrtState.suspAngular = computeSuspAngular(suspParams, suspComplianceState, rigidBodyState, groundPlaneNormal);
cnstrtState.restitution = suspensionLimitParams.restitution;
}
else
{
const PxVec3 suspDirWorldFrame = rigidBodyState.pose.rotate(suspParams.suspensionTravelDir);
const PxF32 projection = groundPlaneNormal.dot(suspDirWorldFrame);
if (projection != 0.0f)
{
cnstrtState.suspActiveStatus = true;
cnstrtState.suspGeometricError = suspState.separation;
const PxVec3 suspLinear = suspDirWorldFrame * PxSign(projection);
cnstrtState.suspLinear = suspLinear;
cnstrtState.suspAngular = computeSuspAngular(suspParams, suspComplianceState, rigidBodyState, suspLinear);
cnstrtState.restitution = suspensionLimitParams.restitution;
}
else
{
cnstrtState.suspActiveStatus = false;
}
}
}
}
}
} //namespace physx