Could I get some help regarding the collision system in code?

Hi,

I am planning to write a guide on this topic on Unrealengine wiki soon as I couldn’t find any good guides, reference or documentation on it. Also, I am newly learning C++ programing on unrealengine so, it will also help me greatly.

Here is what I found when I was going through Unreal engine files.

There are 2 types of of collisions. Actor collisions and component collisions.

Actor collisions are present in Actor.h file. The collision depends on the collision mesh of the actor. It can be imported or generated in engine.

	/** 
	 */
	UFUNCTION(BlueprintCallable, Category="Collision", meta=(UnsafeDuringActorConstruction="true"))
	void GetOverlappingActors(TArray<AActor*>& OverlappingActors, UClass* ClassFilter=NULL) const;

	/** Returns list of components this actor is overlapping. */
	UFUNCTION(BlueprintCallable, Category="Collision", meta=(UnsafeDuringActorConstruction="true"))
	void GetOverlappingComponents(TArray<UPrimitiveComponent*>& OverlappingComponents) const;

	/** Event when this actor bumps into a blocking object, or blocks another actor that bumps into it. */
	UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "Hit"), Category="Collision")
	virtual void ReceiveHit(class UPrimitiveComponent* MyComp, class AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit);

Here is the documentation for it.

Another kind of collision I found in engine was for Primitivecomponent(Actor components). Those are delegates that point to specific function which should be called during collision events.

	virtual bool ComponentOverlapMulti(TArray<struct FOverlapResult>& OutOverlaps, const class UWorld* World, const FVector& Pos, const FRotator& Rot, const struct FComponentQueryParams& Params, const struct FCollisionObjectQueryParams& ObjectQueryParams=FCollisionObjectQueryParams::DefaultObjectQueryParam) const;

	/** Called when a component is touched */
	UPROPERTY(BlueprintAssignable, Category="Collision")
	FComponentHitSignature OnComponentHit;

	/** Called when something overlaps this component */
	UPROPERTY(BlueprintAssignable, Category="Collision")
	FComponentBeginOverlapSignature OnComponentBeginOverlap;

	/** Called when something ends overlapping this component */
	UPROPERTY(BlueprintAssignable, Category="Collision")
	FComponentEndOverlapSignature OnComponentEndOverlap;

These are used to call particular functions in collision events.

If CollisionComp is the name of your actor component then the OnComponentHit function is referenced in this manner.

CollisionComp->OnComponentHit.AddDynamic(this, &AMyProjectProjectile::OnHit);

I have a huge doubt here

If I name the function it calls as OnHit, it works. If I name it in any other name, it doesn’t work.

Example

.h file

	UFUNCTION()
	void OnCHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

.cpp file

AMyProjectProjectile::AMyProjectProjectile(const class FPostConstructInitializeProperties& PCIP) 
	: Super(PCIP)
//...................
//...................
CollisionComp->OnComponentHit.AddDynamic(this, &AMyProjectProjectile::OnCHit);
}


void AMyProjectProjectile::OnCHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	//GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("HIT"));
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		OtherComp->AddImpulseAtLocation(ProjectileMovement->Velocity * 200.0f, Hit.ImpactPoint);
	}
}

This function doesn’t work… But if I replace it with OnHit. Sadly I couldn’t find anywhere in the engine why is it OnHit has to be used rather than any other function.

Thats it for now. Rest everything I am kind of clear.

Hello, I’m so sorry for delay, and I hope this is resolved by now, but I don’t see anything wrong with this. If it’s still not working, what is the base class of AMyProjectProjectile? I don’t think the name should matter, but it does somehow.

–Lina,