How to program Collisions?

Hi, is there any tutorials on how to program collisions? I can do it easily in BP, however i’d like to as much as possible in code, just to hone my skills. If there are no tutorials at the moment, would someone like to give me a quick run through? I have tried to do it before, but ended up with loads of breakpoints which i couldn’t fix.

I want to trigger an event when player collides with collidePlate

I.E

if ((OtherActor != NULL) && (OtherActor != this) && (OtherActor == collidePlate)

Hi Green

Work your way through these tutorials if you haven’t already;

The Engine’s physics simulation should calculate most of the collisions for you, in both blueprints and code (Such as ActorGetDistanceToCollision method). However, a basic approach to collisions can be done with a sphere-to-sphere method.

You will need each of your objects positions and a radius in which to calculate whether a collision happens when the radius of each object intersects.

It would look something like:

FVector objAPos = ;//Get objects location
FVector objBPos = ;//Get objects location

if (FVector::Dist(objAPos, objBPos) < someRadius)
{
    // You have a collision
}

Obviously that is the most naive approach to solving this problem but it gives you a basic idea of collisions.

Thanks for the reply :smiley:

Is it not possible to use Overlap events in code? Because that is the main focus, i just want to know when a collision occurs between two actors

Yes it is. A blueprint callable overlap event would look like;
UFUNCTION(BlueprintCallable, Category = Interaction)
void OnBeginOverlap(AActor* Other, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

In the header file. If you do the intro programming tutorials there’s a collection component as part of the character, with overlap events etc in C++. Its not exactly the same as what you’re asking, but the practice would help you use the docs better…

In the header file of what? Thanks

There’s some information here on overlapping events. You can also call overlap functions such as GetOverlappingActors/Components from any actor class as well.

I’ve done it :slight_smile: Still couldn’t find the answer to my question, though it was very beneficial :slight_smile:

The header file of the class that has the component that the event is being called on. I promise you it’s worth doing that tutorial series…

Well, if your collider plate doesn’t have a collision volume, maybe just call onactorhit, on the mesh? Have you made a class, or a blueprint, for your collision plate? or is this a single instance of a brush or something? If it’s a single instance of an object, select it in the editor, look down the right hand side details panel and click “add level events for…”

That looks promising, i’ll look through it tomorrow :slight_smile:

I could easily do this using blueprints, but i would like to do it in Code, just to learn :stuck_out_tongue:

I think if you’ve got no experience at all in programming, it may be a good idea to have some other assistance too. Maybe a course or a book? I’m also doing it on my own- I did the Stanford uni CS106A and CS106B, online. I get pretty lost as it is, but if I hadn’t done that it would be frying my head completely. Take a look at those (if they are still available) and also books like “Thinking in C++” by Bruce Eckell. I’m not being snooty, I promise. Even just the intro to Thinking in C++ would explain why I was asking if its a single instance or if you’ve made a class…

I’ve been programming on and off for around 2 years, i wouldn’t exactly say i have ‘no experience at all’. It’s just the way Unreal is structured puzzles me at times, where logically code should work but it just doesn’t ;/ And no offense taken, i know you’re just trying to help. I switched the code around, and fixed something. Will show you when i get back

When I say fix something I did it in code. Using delegates and signatures and all this good stuff. But now I’ve got another problem. Code works on a capsule component with a mesh attached in the bp editor but not on a box component with a static mesh attached in code. Will be easier to show you when I return :wink:

EDIT: Upon reviewing the question, i’ve found that the title is misleading, i was asking how to fire events, which i now know how to do.

“Well, if your collider plate doesn’t have a collision volume, maybe just call onactorhit, on the mesh? Have you made a class, or a blueprint, for your collision plate? or is this a single instance of a brush or something? If it’s a single instance of an object, select it in the editor, look down the right hand side details panel and click “add level events for…””

I’m going to try and call OnActorHit on the mesh when i get back in, that was one of my potential fixes but i didn’t get a chance to try it. The collision plate is a component attatched to my CoinBrick class. Which i have created a blueprint from.

call OnActorHit on the mesh.