Collision Presets and CCD in C++

Is there a way to set collision presets in C++? Also, how to set CCD to true in C++?

1 Like

Somewhere in the actor that owns the mesh you want to change collision response to, change it like so:



Mesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Block);


2 Likes

No, this is not what I askedā€¦ I need to changing collision preset for example from ā€œBlockAllā€ to ā€œOverlapAllā€. Off course I can just change every object responses to overlap, but it is too massive. And what if I will make some custom collision response.
Iā€™ll have to change it in every code of the fact that I need.

The channels live on the UPrimitiveComponent and thereā€™s a simple method call to change all of responses:



if (UPrimitiveComponent* PrimitiveComponent = Actor->FindComponentByClass<UPrimitiveComponent>())
{
   PrimitiveComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
}



Thereā€™s a bunch of other methods to set specific channel responses, get the current response, etc.

2 Likes

This thread is the first result I got in Google, so Iā€™ll link the answer I found in another thread:

How Do You Set Custom Collision Presets from Code?

10 Likes

Terzaloā€™s answer is correct. But I do find it kinda dangerous to hardcode strings in such manner, in case you (or your game designer) changes their name in future. Unreal does pop up a warning when thereā€™s no Collision Profile with the given name, but it is easy to miss (check BodyInstance.cpp for more details).

With that in mind, I wanted to share my solution, in case someone else wants to setup this in a C++ - BP integrated environment:

  1. After creating your custom Collision Profile in Projects settings, create an variable in your header file as such:
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, meta=(AllowPrivateAccess="true"))
	FCollisionProfileName CollisionProfile;
  1. Set the variable in your Blueprintā€™s Class Defaults. The only options available will be the existing Collision Profiles, so there is no way to pick one that does not exist.

  1. Use the SetCollisionProfileName function (called from a UPrimitiveComponent or a child class, such as UBoxComponent or UShpereComponent):
PrimiteComponent->SetCollisionProfileName(CollisionProfile.Name, true);
8 Likes

I did this in C++ and editor to get collision on a skel mesh:

  1. Import skel mesh in editor.
  2. Right click skel mesh, create, physics asset, create and assign.

Now if I drag the skel mesh into world in editor it has collision. However, if in C++ I used construction helper FObjectFinder to search for that skel mesh, and brought it into game dynamically, it had no collision.

The problem was that it had the following property in editor but lost it in c++: in editor drag skel mesh into world, then in details see collision-collisionpresets-physicsactor. When you bring in the skel mesh via c++ it loses the physicsactor property and therefore loses its ability to collide

I did this

FeedCylinder_SkelComponent->SetCollisionProfileName(TEXT("PhysicsActor"));

to set it back. Was confusing though because the skel mesh has the correct setting in the content drawer, only thing my c++ does is find it in the editor and works with it as-is, as it was found, but no, this setting is randomly changed when I work with that skel mesh using c++.