Is there a way to determine the Engine’s math for applying AddForce? I followed the functions, but hit a dead-end.
From MyDroneMovementComponent.cpp:
PhysicsCubeRef->AddForce(Force);
Leads to PrimitiveComponentPhysics.cpp:
void UPrimitiveComponent::AddForce(FVector Force, FName BoneName, bool bAccelChange)
{
if (FBodyInstance* BI = GetBodyInstance(BoneName))
{
WarnInvalidPhysicsOperations(LOCTEXT("AddForce", "AddForce"), BI, BoneName);
BI->AddForce(Force, true, bAccelChange);
}
}
Which leads to BodyInstance.cpp:
void FBodyInstance::AddForce(const FVector& Force, bool bAllowSubstepping, bool bAccelChange)
{
FPhysicsCommand::ExecuteWrite(ActorHandle, [&](const FPhysicsActorHandle& Actor)
{
if(!IsRigidBodyKinematic_AssumesLocked(Actor))
{
if(FPhysScene* PhysScene = GetPhysicsScene())
{
PhysScene->AddForce_AssumesLocked(this, Force, bAllowSubstepping, bAccelChange);
}
}
});
}
Which leads to PhysScene_Chaos.cpp:
void FPhysScene_Chaos::AddForce_AssumesLocked(FBodyInstance* BodyInstance, const FVector& Force, bool bAllowSubstepping, bool bAccelChange)
{
using namespace Chaos;
FPhysicsActorHandle& Handle = BodyInstance->GetPhysicsActorHandle();
if (FPhysicsInterface::IsValid(Handle))
{
Chaos::FRigidBodyHandle_External& Body_External = Handle->GetGameThreadAPI();
EObjectStateType ObjectState = Body_External.ObjectState();
Body_External.SetObjectState(EObjectStateType::Dynamic);
if (bAccelChange)
{
const Chaos::FReal Mass = Body_External.M();
const Chaos::FVec3 Acceleration = Force * Mass;
Body_External.AddForce(Acceleration);
}
else
{
Body_External.AddForce(Force);
}
}
}
Which leads to SingleParticlePhysicsProxy.h:
void AddForce(const FVec3& InForce, bool bInvalidate = true)
{
Write([&InForce, bInvalidate, this](auto* Particle)
{
if (auto* Rigid = Particle->CastToRigidParticle())
{
if (Rigid->ObjectState() == EObjectStateType::Sleeping || Rigid->ObjectState() == EObjectStateType::Dynamic)
{
if (bInvalidate)
{
SetObjectStateHelper(*GetProxy(), *Rigid, EObjectStateType::Dynamic, true);
}
Rigid->AddForce(InForce, bInvalidate);
}
}
});
}
…At which point I have no idea what Rigid->AddForce(InForce, bInvalidate);
is calling. I’d done a bit programming but was new to C++ for this course, so I’m not sure where the trail ends that has the actual calculations. Do you know?