Physics constrain to Axis in c++

How can I constrain physics to one axis in c++?

#FConstraintInstance

There is a huge set of properties called FConstraintInstance

You can limit or lock xyz movement axis using this!

Here is my helper function to set these properties

#.h

FORCEINLINE void SetLinearLimits(
		FConstraintInstance& Constraint,
		bool bDisableCollision,
		const uint8& XLim, const uint8& YLim, const uint8& ZLim,
		const float& Size,
		bool SoftLimit=true,
		float SoftStiffness=0,
		float SoftDampening=0
	)
	{
		//Collision
		Constraint.bDisableCollision = bDisableCollision;
		
		switch (XLim)
		{
			case 0 : Constraint.LinearXMotion = ELinearConstraintMotion::LCM_Free; break;
			case 1 : Constraint.LinearXMotion = ELinearConstraintMotion::LCM_Limited; break;
			case 2 : Constraint.LinearXMotion = ELinearConstraintMotion::LCM_Locked; break;
		}
		switch (YLim)
		{
			case 0 : Constraint.LinearYMotion = ELinearConstraintMotion::LCM_Free; break;
			case 1 : Constraint.LinearYMotion = ELinearConstraintMotion::LCM_Limited; break;
			case 2 : Constraint.LinearYMotion = ELinearConstraintMotion::LCM_Locked; break;
		}
		switch (ZLim)
		{
			case 0 : Constraint.LinearZMotion = ELinearConstraintMotion::LCM_Free; break;
			case 1 : Constraint.LinearZMotion = ELinearConstraintMotion::LCM_Limited; break;
			case 2 : Constraint.LinearZMotion = ELinearConstraintMotion::LCM_Locked; break;
		}
		//~~~~~~~~~~
		
		Constraint.LinearLimitSize = Size;
		
		if(SoftLimit) Constraint.bLinearLimitSoft = 1;
		else Constraint.bLinearLimitSoft = 0;
		
		Constraint.LinearLimitStiffness 	= SoftStiffness;
		Constraint.LinearLimitDamping 	= SoftDampening;
	}

#.cpp


#Spawning Contraint

//in some actor class
ConstraintComp = NewObject<UPhysicsConstraintComponent>(this);

:slight_smile:

#Helper Function

//helper function from the .h 
SetLinearLimits(ConstraintComp->ConstraintInstance,true,0,2,2,0);
//0,2,2 = object can only move along x axis

#Init Constraint

//~~~ Init Constraint ~~~
ConstraintComp->SetConstrainedComponents(YourStaticMeshActor->StaticMeshComponent, NAME_None, YourStaticMeshActor2->StaticMeshComponent->StaticMeshComponent,NAME_None);

the above constraints two SMAs together

you could also use the UPrimitiveComponent of just about anything.

Note that the first supplied component will be the root, and the 2nd component is the one that is constrained in its movements in relation to the root.

Thanks I will try it out.