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"

Applying force to dynamically created object

I'm attempting to spawn objects, place some force on them, and then when they fall below x, destroy them.

I've pulled some code from the following tutorial: Components and Collisions to get me started.

 // Spawner
 void AProjectileSpawner::SpawnOneItem()
 {
     UWorld *w = GetWorld();
     APlayerCameraManager *cMgr = GetCameraManager();
 
     UClass *uc = AProjectile::StaticClass();
     FTransform t(cMgr->GetCameraRotation(), cMgr->GetCameraLocation());
     ESpawnActorCollisionHandlingMethod always;
     always = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
 
     AProjectile *p = w->SpawnActorDeferred<AProjectile>(uc,
                                                         t,
                                                         NULL,
                                                         NULL,
                                                         always);
 
     p->ForwardForce = ForwardForce;
     p->UpwardForce = UpwardForce;
     UGameplayStatics::FinishSpawningActor(p, t);
 }
 
 // Spawnee
 AProjectile::AProjectile()
 {
 #define SOBJ_ROOT TEXT("RootComponent")
 #define SOBJ_MESH TEXT("Mesh")
 #define PROFILE_NAME TEXT("PhysicsActor")
 #define SPHR_DIR TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere")
 
     PrimaryActorTick.bCanEverTick = true;
 
     USphereComponent* sCmp = NULL;
     sCmp = CreateDefaultSubobject<USphereComponent>(SOBJ_ROOT);
     RootComponent = sCmp;
     sCmp->InitSphereRadius(1.0f);
     sCmp->SetCollisionProfileName(PROFILE_NAME);
 
     mesh = CreateDefaultSubobject<UStaticMeshComponent>(SOBJ_MESH);
     mesh->SetupAttachment(RootComponent);
 
     static ConstructorHelpers::FObjectFinder<UStaticMesh> asset(SPHR_DIR);
     if (asset.Succeeded())
     {
         mesh->SetStaticMesh(SphereVisualAsset.Object);
         mesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
         mesh->SetWorldScale3D(FVector(0.3f));
         mesh->SetSimulatePhysics(true);
         mesh->WakeRigidBody();
     }
 
 #undef SOBJ_ROOT
 #undef SOBJ_MESH
 #undef PROFILE_NAME
 #undef SPHR_DIR
 }
 
 void AProjectile::BeginPlay()
 {
     Super::BeginPlay();
 
     FRotator cameraRotation = GetCameraRotation();
 
     FVector up, forward, upAndForward;
     up = RotationToDirection(cameraRotation, EAxis::Z) * UpwardForce;
     forward = RotationToDirection(cameraRotation, EAxis::X) * ForwardForce;
     upAndForward = up + forward;
 
     mesh->AddImpulse(upAndForward);
 }
 
 void AProjectile::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 
     FVector l = GetActorLocation();
     if (l.Z < -50000.0f)
         Destroy();
 }
 

(FYI - The overuse of temp variables and the #define stuff is for readability. The engine and API enjoy naming things so long they need punctuation, but this site wraps on 80 char columns.)

Visually, things are working as expected. However, when viewing properties of the items that are spawned, only the innermost "component" has it's Z adjusting. The "Root Component" (Assuming that is the correct term), and the one in between have a static position. Nothing spawned ever gets deleted.

When I add force to the mesh I created, mesh->AddImpulse(upAndForward);, how do I make that force effect everything associated with that mesh?

Thanks in advance for any help!

Product Version: UE 4.14
Tags:
more ▼

asked Jan 17 '17 at 08:11 PM in C++ Programming

avatar image

nathansizemore
92 4 8 13

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Simply had to move the physics items to the Root Component instead of the second component created (the mesh).

Possible Duplicate

more ▼

answered Jan 17 '17 at 09:36 PM

avatar image

nathansizemore
92 4 8 13

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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