Black Hole Challenge: C++/Blueprints

Black Hole Challenge es el primer reto propuesto por el curso Unreal Engine 4 Mastery: Create Multiplayer Games with C++ de Tom Looman certificado por Epic Games disponible en la plataforma Udemy.

A lo largo del curso se desarrollarán dos juegos con base en C++ y extendiendo la funcionalidad con Blueprints. Los dos juegos son multijugador y se profundizará también en AI y para reforzar los diferentes temas se proponen distintos retos. En esta ocasión hay que prototipar un agujero negro que atraiga y destruya los objetos físicos que estén en su campo de acción para posteriormente implementarlo en C++.

UPROPERTY(VisibleAnywhere, Category = "Components")
UStaticMeshComponent* MeshComp;

UPROPERTY(VisibleAnywhere, Category = "Components")
USphereComponent* SphereGravityComp;
UPROPERTY(VisibleAnywhere, Category = "Components")
USphereComponent* SphereDestroyComp;

void checkNearbyActors();

// Marked with ufunction to bind to overlap event
UFUNCTION()
void OverlapInnerSphere(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
ABlackHoleBase::ABlackHoleBase()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  PrimaryActorTick.bCanEverTick = true;

  MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
  MeshComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
  RootComponent = MeshComp;

  SphereGravityComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereGravComp"));
  SphereGravityComp->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
  SphereGravityComp->SetupAttachment(MeshComp);

  SphereDestroyComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereDesComp"));
  SphereDestroyComp->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
  SphereDestroyComp->SetupAttachment(MeshComp);

  // Bind to Event
  SphereDestroyComp->OnComponentBeginOverlap.AddDynamic(this, &ABlackHoleBase::OverlapInnerSphere);

}

void ABlackHoleBase::checkNearbyActors()
{
  TArray<UPrimitiveComponent*> OverlapingActorsGravity;
  SphereGravityComp->GetOverlappingComponents(OverlapingActorsGravity);

  for (int32 i = 0; i < OverlapingActorsGravity.Num(); i++)
  {
    UPrimitiveComponent* primitive = OverlapingActorsGravity[i];
    if (primitive && primitive->IsSimulatingPhysics())
    {
      const float SphereRadius = SphereGravityComp->GetScaledSphereRadius();
      const float ForceStrength = -2000; // Negative value to make it pull towards the origin instead of pushing away
      primitive->AddRadialForce(GetActorLocation(), SphereRadius, ForceStrength, ERadialImpulseFalloff::RIF_Constant, true);

    }
  }
}

void ABlackHoleBase::OverlapInnerSphere(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
  int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
  if (OtherActor)
  {
    OtherActor->Destroy();
  }
}

A continuación os dejaré los enlaces al código, demo y al video en youtube para que podáis ver más. De momento me está gustando el curso pero al final compartiré una valoración general por si a alguien le pueda interesar que sepa más o menos que esperar del mismo.

Repositorio: Black Hole Challenge: C++/Blueprints [project]

Demo: Black Hole Challenge: C++/Blueprints [demo]

Curso: Unreal Engine 4 Mastery: Create Multiplayer Games with C++