Download

Collision handling with Paper2D sprites

Hi,

I am coding (and learning UE4) a game in C++ using Paper2D and I am having trouble getting collision to work.

In my simple scenario I have a flipbook component (enemy) and I want it to fire my delegate if another sprite component (laser) overlaps it.

Here is a snippet of my code:

Enemy constructor:

[FONT=courier new]Enemy = CreateDefaultSubobject<UPaperFlipbookComponent>(TEXT(“Enemy”));
Sprite = Cast<UPaperFlipbook>(StaticLoadObject(UPaperFlipbook::StaticClass(), NULL, TEXT(“PaperFlipbook’/Game/Flipbooks/Enemy_Flipbook.Enemy_Flipbook’”)));
Enemy->SetFlipbook(Sprite);
Enemy->SetPlayRate(2.0f);
Enemy->SetLooping(true);
Enemy->SetRelativeRotation(FRotator(180.0f, -180.0f, 90.0f));
Enemy->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);

Enemy BeginPlay():

[FONT=courier new]Enemy->SetCollisionProfileName(TEXT(“OverlapAll”));
Enemy->SetGenerateOverlapEvents(true);
Enemy->OnComponentBeginOverlap.AddDynamic(this, &AEnemy::OnBeginOverlap);

Enemy callback:

[FONT=courier new]void AEnemy::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 0.5f, FColor::Red, FString::Printf(TEXT(“Something overlapped with me the enemy!”)));
}

Laser constructor:

[FONT=courier new]Laser = CreateDefaultSubobject<UPaperSpriteComponent>(TEXT(“Laser”));
Sprite = Cast<UPaperSprite>(StaticLoadObject(UPaperSprite::StaticClass(), NULL, TEXT(“PaperSprite’/Game/Sprites/Laser_Sprite.Laser_Sprite’”)));
Laser->SetSprite(Sprite);
Laser->SetRelativeRotation(FRotator(180.0f, -180.0f, 90.0f));
Laser->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);

Laser BeginPlay():

[FONT=courier new]Laser->SetCollisionProfileName(TEXT(“OverlapAll”));
Laser->SetGenerateOverlapEvents(true);

That’s it basically. When I run the game, the laser is on the same Z-position as the enemy and when it moves over the enemy, nothing happens.

I have also tried [FONT=courier new]SetCollisionEnabled() in various ways on the enemy component, but it makes no difference.

Am I missing some setting somewhere? This is my first attempt at collision so there could be something I have overlooked.

The enemy sprites are Shrink Wrapped and the laser sprite is set to Tight Bounding Box and all are set to OverlapAll in the editor.

NOTE: I also have OnComponentHit set up but it does not fire either.

Thanks,
M.

So this looks all correct to me, i cant see anything you missed except you might be missing the actual collision volumes. I believe you need to also load in (for both objects) a uprimitive component collision mesh, like a box or a sphere object and scale to the size you need. When THESE overlap, then you will get overlap hits.

and move the delegate and collision profile name to the umprimitve component instead of the sprite

Thank you so much ViperG! I already had some code to find the primitive component, but it did not work. Now as you say, I have created a sphere component as the root component and it works great!

Can you please let me know how this collision is working in relation to my sprites that have a geometry type of shrink wrapped and tight bounding box?

I have set the sphere as [FONT=courier new]SetSphereRadius(28.0f) which is just a random number for me, but it seems to cover a small area around the middle of my enemy sprite.

So what should this be set to so it honours the size and collision geometry of the two sprites?

you’ll have to play around with it to get what you want. Other thing you can do is add some debug helpers in your game, which would allow you to draw the sphere so you can see the size compared to your sprite. There is a debug draw sphere you can create DrawDebugSphere | Unreal Engine Documentation

It should match the size of your collisions sphere so you can see what it looks like in your game. You can draw the sphere once in being play with 10 - 30 second life time,
or you can draw it every tick and make the life time short like 0.02f.

There’s some youtube videos on how to use the debug helpers if you get stuck. Most of them are blueprint but theres one or two that show it in c++.