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:

191849-acceleration.png

(x: Time, y: Acceleration)

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

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??

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.

Next link can be helpful

UMovementComponent

What do you mean by “movement vector” ?

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

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

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