I want to move some object to move “Natural”.
The Acceleration graph of the “Natural Move” is like this:
(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??