Hello, I hope you all doing well.
Recently I came across an issue where I wanted to make a door system that have constraints on openning and closing. But I wanted to setup it all on begein play ans c++ so I can scale the number of door and theirs behaviour how I want.
But then I realized that when I setup the component in the blueprint and fill up the rotation and translation constrain myself I works perfectly, but when I try to add the contraint component on begin play in c++, the physic object witch is supposed to get the constrains, just fell trought the ground like there is no constraints.
Blueprint Result:
c++ on begin play result:
Here is the part of my code where I try to add the component and populate the data to acheave the same result as in the blueprint.
the part where I Instantiate the component:
void UHCDoorComponent::RegisterPCComp(AHCPassageOpenableActor* _InPassOpActor, FHCDoors& _InCurrDoor)
{
if (IsValid(_InPassOpActor) && _InCurrDoor.DoorName != "")
{
TArray<UPhysicsConstraintComponent*> CurrentPCComp;
_InPassOpActor->GetComponents<UPhysicsConstraintComponent>(CurrentPCComp);
if (CurrentPCComp.IsEmpty())
{
_InCurrDoor.DoorPCSMComponent = Cast<UPhysicsConstraintComponent>( _InPassOpActor->AddComponentByClass(UPhysicsConstraintComponent::StaticClass(),true,FTransform::Identity,false));
_InPassOpActor->AddInstanceComponent(_InCurrDoor.DoorPCSMComponent);
_InCurrDoor.DoorPCSMComponent->AttachToComponent(this,FAttachmentTransformRules::SnapToTargetIncludingScale);
}
else
{
for (UPhysicsConstraintComponent* PCComp : CurrentPCComp)
{
PCComp->UnregisterComponent();
}
RegisterPCComp(_InPassOpActor, _InCurrDoor);
}
}
}
The part where I populate the physics constraint component
//SetupPhysicFordoors
for (FHCDoors& Door : DoorContainer)
{
if (Door.DoorSMComponent->GetStaticMesh()->GetBodySetup()->AggGeom.BoxElems.Num() > 0)
{
if (IsValid(Door.DoorPCSMComponent))
{
//Constraints & Limits
Door.DoorPCSMComponent->ComponentName2.ComponentName = FName(Door.DoorName);
Door.DoorPCSMComponent->SetConstrainedComponents(nullptr,FName("None"),Door.DoorSMComponent,FName("None"));
FConstraintInstance ConstInstance;
ConstInstance.SetLinearXLimit(LCM_Locked,0);
ConstInstance.SetLinearYLimit(LCM_Locked,0);
ConstInstance.SetLinearZLimit(LCM_Locked,0);
ConstInstance.SetAngularSwing1Limit(ACM_Limited,50);
ConstInstance.SetAngularSwing2Limit(ACM_Locked,0);
ConstInstance.SetAngularTwistLimit(ACM_Locked,0);
//AngularRotOffset
ConstInstance.AngularRotationOffset = FRotator(0,50,0);
//SetFinalStruct
ConstInstance.UpdateAngularLimit();
ConstInstance.UpdateLinearLimit();
Door.DoorPCSMComponent->ConstraintInstance = ConstInstance;
//Door.DoorPCSMComponent->UpdateConstraintFrames();
//Door.DoorPCSMComponent->ConstraintInstance.InitConstraint(nullptr,nullptr,1,this);
}
Door.DoorSMComponent->SetLinearDamping(1);
Door.DoorSMComponent->SetAngularDamping(1);
Door.DoorSMComponent->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic,ECR_Ignore);
Door.DoorSMComponent->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldDynamic,ECR_Block);
Door.DoorSMComponent->SetAllMassScale(10);
Door.DoorSMComponent->SetSimulatePhysics(true);
}
}