Download

Collision checks with 2D sprites

It’s been a while since I last checked the latest changes regarding 2D sprites and UE4.

Is it possible to use UE4’s physics engine to perform rectangle checks (like overlapping rects) and 2D ray casting with sprites?

I’m going to expand on this, hoping that someone with some experience will chime in.

I have got my 2D sprite react to overlaps but as far as I can see the GetOverlappingActors method isn’t letting you specify which collision shape to use… and I do have more than one collision shape on my sprite (one for world collision and one as a hitbox for enemy bullets collision).

My current collision check code looks like this:


AActor *AHeroPawn::CollideFirst(float x, float z) {
    AActor *res = NULL;
    FVector originalLocation = GetActorLocation();
    FVector newLocation = GetActorLocation();
    newLocation.X = x;
    newLocation.Z = z;
    SetActorLocation(newLocation);

    TArray<AActor *> actors;
    Sprite->GetOverlappingActors(actors, NULL);
    for (auto CompIt = actors.CreateIterator(); CompIt; ++CompIt) {
        UE_LOG(LogTemp, Warning, TEXT("Overlapping actor."));
        AActor *OverlappingActor = *CompIt;
        if (OverlappingActor != this) {
            UE_LOG(LogTemp, Warning, TEXT("Actor: %s"), *AActor::GetDebugName(OverlappingActor));
            res = OverlappingActor;
        }
    }

    SetActorLocation(originalLocation);
    return res;
}

It would be nice to be able to choose which collision shape to use for collisions at a certain point in time. Any idea if this is achievable?

I solved this by adding two UBoxComponent to the actor instead of using the collision shapes of the sprite.