x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Collision Detection in C++

Hey everyone, So I am working on a melee attack with a sword, which will eventually be able to do things like, chop off zombie limbs or stop a animation if it hits a dense wall or chop through it if it is thin enough, etc... stuff like that, what I have to do first is detect when an object is hitting it, which I am trying to do right now:

This is the code for picking up the item and adding the collision code and later removing the item and collision code:

 void APlayerCharacter::Use(AUsableItem* curItem, FName Socket){
     curItem->getItemMesh()->SetSimulatePhysics(false);
     curItem->SetActorEnableCollision(true);
     curItem->getItemMesh()->AttachTo(GetMesh(), Socket, EAttachLocation::SnapToTarget, true);
     TScriptDelegate<FWeakObjectPtr> BeginDelegate;
     TScriptDelegate<FWeakObjectPtr> EndDelegate;
     BeginDelegate.BindUFunction(this, FName("OnWeaponOverlapBegin"));
     EndDelegate.BindUFunction(this, FName("OnWeaponOverlapEnd"));
     curItem->OnActorBeginOverlap.Add(BeginDelegate);
     curItem->OnActorEndOverlap.Add(EndDelegate);
 }
 void APlayerCharacter::OnWeaponOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool fromSweep, const FHitResult& result){
     GEngine->AddOnScreenDebugMessage(1, 1, FColor(1, 1, 1, 100), TEXT("HIT"));
 }
 
 void APlayerCharacter::OnWeaponOverlapEnd(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex){
     GEngine->AddOnScreenDebugMessage(1, 1, FColor(1, 1, 1, 100), TEXT("NoHit"));
 
     }
 
 void APlayerCharacter::RemoveItem(AUsableItem* curItem, FName Socket){
     curItem->DetachRootComponentFromParent();
     curItem->getItemMesh()->SetSimulatePhysics(true);
     curItem->SetActorEnableCollision(true);
     //curItem->getItemMesh()->SetCollisionProfileName(TEXT("BlockAll"));
     curItem->ApplyWorldOffset((FirstPersonCamera->GetComponentRotation().Vector() * 200), false);
     curItem->OnActorBeginOverlap.RemoveAll(Cast<UObject>(curItem));
     curItem->OnActorEndOverlap.RemoveAll(Cast<UObject>(curItem));
 
 }


curItem is the melee Weapon, and it should be obvious that I am adding a delegate to when the actor begins/ends overlapping. The Item does work with physics, such as moving boxes around etc... Also I have set up the UFUNCTION() tag in the header with OnWeaponOverlapBegin/End. I cannot seem to figure out what the problem is, but no matter what, I dont see "Hit" being shown on the screen...

Thanks

Product Version: UE 4.9
Tags:
more ▼

asked Oct 21 '15 at 12:09 AM in C++ Programming

avatar image

joelComberiati
112 26 22 28

avatar image joelComberiati Oct 21 '15 at 02:48 AM

By the way, I am using a USkeletalMesh For the weapon, so cant check just a simple collision Mesh

avatar image joelComberiati Oct 22 '15 at 12:23 AM

Still having an issue with this, can anyone help? Seems like this would be a common thing to do in a game

avatar image joelComberiati Oct 24 '15 at 11:14 PM

Actually now the collision works somewhat, it is just not at all accurate, sometimes saying it is hitting when it is not, and not hitting when it is...

avatar image joelComberiati Oct 25 '15 at 12:50 AM

And I think this issue is due to it being touching the character...

(comments are locked)
10|2000 characters needed characters left

3 answers: sort voted first

So I figured out why it was acting so strangely. I used this instead:

 curItem->getItemMesh()->SetCollisionProfileName(FName("OverlapAll"));
     curItem->collMesh->MoveIgnoreActors.Add(this);
     curItem->collMesh->MoveIgnoreActors.Add(curItem);
     curItem->collMesh->OnComponentBeginOverlap.AddDynamic(curItem, &AUsableItem::OnWeaponOverlapBegin);
     curItem->collMesh->OnComponentEndOverlap.AddDynamic(curItem, &AUsableItem::OnWeaponOverlapEnd);


The MoveIgnoreActors allows the weapon ignore the weapon that it is surrounding and the player, that way there is no chance for collision.

Thanks to matthew_clark and Timoses I used both of your answers to find the solution to this problem

more ▼

answered Oct 26 '15 at 09:46 PM

avatar image

joelComberiati
112 26 22 28

(comments are locked)
10|2000 characters needed characters left

Hello joelComberiati,

It seems like the issue here is that your collision is set to Block All, but you're attempting to base your collisions off the overlap events. You'll need either set your weapon to be OverlapAll (Or some form of that) or change your collision events to OnActorReceiveHit or something of the sort. You also may wish to set up a Box Collision (Or any type of collision volume, not sure on your weapon's shape), attach it to the weapon, and run the overlap/hit events based off that.

more ▼

answered Oct 22 '15 at 08:27 PM

avatar image

Matthew J ♦♦ STAFF
31.8k 526 92 417

avatar image joelComberiati Oct 23 '15 at 12:36 AM

Thank you!, So I changed collision profile with: curItem->getItemMesh()->SetCollisionProfileName(FName("OverlapAll")); but it still does not work, I am wondering if it is because there is only a physics asset attached and not a collision mesh, is there a way to use the collision volume that is in a physics asset?

avatar image mrooney Oct 29 '15 at 02:45 PM

If you have a blueprint for your weapon it's a lot easier not to set the collision profile stuff up in C++. It's way easier to just set it up in the gui for the mesh.

Here's a good video to watch https://wiki.unrealengine.com/Videos/Player?series=PLZlv_N0_O1ga0IoRrpI4xkX4qmCrhGu56&video=C_JCtGNW5D8

Set up your component in the editor and just override the response functions in code.

avatar image joelComberiati Oct 31 '15 at 02:09 AM

Ah, you see I am a programmer, and from what I understand, blueprints do a a lot, but c++ will always be able to more because it allows you more freedom. Anyways, I switch the Profile between block all and overlap all when i pick up/drop the weapon. I have decided to use blueprints sort of like objects that I can drop onto the game.

(comments are locked)
10|2000 characters needed characters left

Did you set a CollisionObjectType?

This is a good read if you're unsure if you've understood collision correctly: https://www.unrealengine.com/blog/collision-filtering

more ▼

answered Oct 23 '15 at 03:08 AM

avatar image

Timoses
86 13 13 21

avatar image joelComberiati Oct 23 '15 at 03:54 AM

Thank you, this has actually been very helpful in confirming what I was unsure of. But I have no set the Collision Object Type (by setting the Collision Profile) But I do not know if they physics asset mesh is being used as a collision mesh, and I am not sure how to do that. The reason why I want the physics asset mesh, is because for each object i set whether I can use a single/multi convex hull, or possibly a box or sphere, because each object may be drastically different shape.

avatar image Timoses Oct 23 '15 at 02:49 PM

I don't really know what you're aiming for. A good approach might be to drastically simplify your code, remove possible sources of error and start with the easiest case to see if that works. Then find your way towards your desired procedure step-wise to see where there's a problem.

avatar image joelComberiati Oct 24 '15 at 12:47 AM

That is what I have been doing for the past few weeks... Basically I want my USkeletalMesh to use the attached physics Asset Collision Volume to detect Collision. This is as far as I have gotten, yet it does not work. It seems right, but I have not used Unreal a whole ton, so I am not sure how this works.

avatar image Jerry.Richards Oct 27 '15 at 03:31 PM

Posted 3 days ago, huh? And working on it for the "past few weeks" mirrors exactly what I've been facing too. I'm using a skeletal mesh with the physics asset as well, and I now have the physics assets colliding, albeit I disabled animation for the time being, and switched to using a BOX collider. This link will show the use of OnActorHit.AddDynamic(this, &AMyPawn::OnHit) that does work for collisions with everything :)

https://forums.unrealengine.com/showthread.php?87962-How-to-get-onhit-events-to-fire-with-collisions&p=408205#post408205

Oddly enough though my bullet isn't colliding using the same technique.

avatar image joelComberiati Oct 29 '15 at 02:40 PM

Well, I have actually been working on this long before I posted it here. I only ask questions after I have spent time working on it for a while. No need to bother others for information that I wont spend the time to find myself. But thanks for the input.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question