Hi, I would like to set the inertia tensor of physics body manually to do an accurate simulation.
For PhysX, I found that there is no function in components to directly do this, but I can directly interact with PhysX through this API(FPhysicsInterface_PhysX::SetMassSpaceInertiaTensor_AssumesLocked | Unreal Engine Documentation ); and at here ([PHYSICS] Center of mass needs a rework - #9 by BoredEngineer ), the function UMMTBPFunctionLibrary::MMTSetInertiaTensor is a sample to use it.
However, for chaos physics engine, I cannot find an equivalent API. How should I set the inertia tensor manually for chaos?
vr_marco
(vr_marco)
October 11, 2021, 9:13am
2
To explicitly set the inertia tensor for a rigid body, you can call the Physics Interface with FPhysicsInterface::SetMassSpaceInertiaTensor_AssumesLocked(Actor, MassSpaceInertiaTensor).
If you check for example inside the Chaos Vehicles source code, this is what it is used within UpdateMassProperties.
void UChaosVehicleMovementComponent::UpdateMassProperties(FBodyInstance* BodyInstance)
{
if (BodyInstance && FPhysicsInterface::IsValid(BodyInstance->ActorHandle) && FPhysicsInterface::IsRigidBody(BodyInstance->ActorHandle))
{
FPhysicsCommand::ExecuteWrite(BodyInstance->ActorHandle, [&](FPhysicsActorHandle& Actor)
{
const float MassRatio = this->Mass > 0.0f ? this->Mass / BodyInstance->GetBodyMass() : 1.0f;
FVector InertiaTensor = BodyInstance->GetBodyInertiaTensor();
InertiaTensor.X *= this->InertiaTensorScale.X * MassRatio;
InertiaTensor.Y *= this->InertiaTensorScale.Y * MassRatio;
InertiaTensor.Z *= this->InertiaTensorScale.Z * MassRatio;
FPhysicsInterface::SetMassSpaceInertiaTensor_AssumesLocked(Actor, InertiaTensor);
FPhysicsInterface::SetMass_AssumesLocked(Actor, this->Mass);
});
}
}