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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How to Move the object "Natural" by C++?

I want to move some object to move "Natural".

The Acceleration graph of the "Natural Move" is like this:

alt text

(x: Time, y: Acceleration)

Than, the graph of velocity and displacement would be following graph:

alt text

I implemented this movement using SetActorLocaion() function like this:

 // Move the object by y-axis to _MoveWidth
 void ASomeObject::MoveSomeObject(float _MoveWidth, float _MoveTime)
 {
     // Ignore if the object is already moving
     if (bIsMoving)
         return;
     ElapsedTime = 0.0f;
     MoveWidth = _MoveWidth;
     MoveTime = _MoveTime;
     // The displacement of the object can be expressed:
     // (MoveWidth)/(MoveTime)^3 * (3*(MoveTime) - 2*(ElapsedTime)) * (ElapsedTime)^2
     MagicNumber = MoveWidth / (MoveTime * MoveTime * MoveTime);
     // Flag for checking the object is moving
     bIsMoving = true;
     // Store the current location and target location
     DefaultLocation = this->GetActorLocation();
     TargetLocation = DefaultLocation + FVector(0, MoveWidth, 0);
     PrevX = DefaultLocation.X;
     PrevY = DefaultLocation.Y;
     PrevZ = DefaultLocation.Z;
 }
 
 void ASomeObject::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
     if (bIsMoving)
     {
         ElapsedTime += DeltaTime;
         FVector PrevLocation = this->GetActorLocation();
         const FVector NewLocation = FVector(PrevX, PrevY + MagicNumber * (3 * MoveTime - 2 * ElapsedTime) * ElapsedTime * ElapsedTime, PrevZ);
         SetActorLocation(NewLocation, false, nullptr, ETeleportType::TeleportPhysics);
         // If the velocity becomes negative
         if ((NewLocation.Y - PrevLocation.Y) / DeltaTime <= 0)
         {
             bIsMoving = false;
             // Revise the final location to Target Location
             SetActorLocation(TargetLocation, false, nullptr, ETeleportType::TeleportPhysics);
             ElapsedTime = 0.0f;
         }
     }
 }


This code works pretty well, but It's just a trickery. The object doesn't have velocity but it just teleported by computed location.

So I want to make the object using movement vector. How to implement it??

Product Version: UE 4.16
Tags:
acceleration.png (7.2 kB)
캡처.png (17.3 kB)
more ▼

asked Jul 10 '17 at 01:55 PM in C++ Programming

avatar image

Rexion_ize
17 2 6 10

avatar image 42EspoiR Jul 10 '17 at 02:40 PM

What do you mean by "movement vector" ?

You can simplify your code using Lerp Vector with a custom ue4 curve that give the alpha.

avatar image Rexion_ize Jul 11 '17 at 12:49 AM

Oh, sorry for my poor English. I meant that I want to give a real velocity to the object, by using like MovementComponent::AddInputVector(). Also, using Lerp sounds good for me, so I'll try it. Thank you.

avatar image 42EspoiR Jul 11 '17 at 09:21 AM

Why not add a movement component in your Actor header ? It should work in cpp

in .h

 UProjectileMovementComponent* MoveComp;

in cpp,

 AYourActor::AYourActor(const class FObjectInitializer& PCIP)
 {
     ProjectileMovement = PCIP.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileMovement0"));
 }

see this post

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

2 answers: sort voted first

Next link can be helpful

UMovementComponent

more ▼

answered Jul 11 '17 at 10:34 AM

avatar image

SvarogZ
49 10 12 21

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

Take a look at the floating pawn movement component. Use SafeMoveUpdatedComponent to move your component through the world, this handels hitting other level geometry for you. At the end of your update call UpdateComponentVelocity(). This ensures that your actor has the correct velocity if you need it afterwards

more ▼

answered Jul 11 '17 at 10:46 AM

avatar image

MatzeOGH
576 21 10 32

(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