how to create a collision response

Hello! I’m new to UE. And I would like to know how I can create a collision response in such a way that a non-physical ball bounces off any surface at a certain speed? Is it possible that through Setactorworldoffset()? I found the necessary logic in blueprints, but is it possible to do this in c++?

Yes ofc, try something like this

#include "MyActor.h"

// Sets default values
AMyActor::AMyActor()
{
 	// Set this actor to call Tick() every frame. 
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	FVector direction;
	FHitResult OutHit;
	AddActorWorldOffset(direction * DeltaTime, true, &OutHit);

	FVector result = FMath::GetReflectionVector(direction, OutHit.Normal);

	GEngine->AddOnScreenDebugMessage(-1, 200, FColor::Green, FString::Printf(TEXT("Result reflect vector: %s"), *result.ToString()));
}

Unfortunately, the sphere just stops when it collides…

Okay, I didn’t figure this out without your help. Thanks!

// Sets default values
ABeaverLog::ABeaverLog()
{

    PrimaryActorTick.bCanEverTick = true;

    // boxComp = CreateDefaultSubobject<USphereComponent>("name");
    // SetRootComponent(boxComp);
}

// Called when the game starts or when spawned
void ABeaverLog::BeginPlay()
{
    Super::BeginPlay();
    direction = GetActorLocation();
}

// Called every frame
void ABeaverLog::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    LogMovement(DeltaTime);
}

void ABeaverLog::LogMovement(float DeltaTime)
{

    FHitResult OutHit;
    AddActorWorldOffset(direction * DeltaTime * speed, true, &OutHit);
    GEngine->AddOnScreenDebugMessage(-1, 200, FColor::Green,
                                     FString::Printf(TEXT("Result direct vector: %s"), *direction.ToString()));

    FVector result = FMath::GetReflectionVector(direction, OutHit.Normal);
    direction = result;
    GEngine->AddOnScreenDebugMessage(-1, 200, FColor::Green,
                                     FString::Printf(TEXT("Result reflect vector: %s"), *result.ToString()));
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.