Hello and thanks in advance for looking.
I have an actor that currently moves between point A & B repeatedly just fine. But I’d like to implement an ease in/out formula so that the object slows before reaching it’s destination, then speeds up slowly again as it moves back toward the other point.
I’ve messed with this a bit and tried finding some generic C++ formulas but I can’t seem to get anything to work right. Any help would be appreciated.
Current working code:
void AMainPlatform::UpdatePlatformBob(float DeltaTime)
{
if (!PlatformBobs) { return; }
FVector Location = GetActorLocation();
float PathLength = (EndLocation - StartLocation).Size();
float PathTravelled = (Location - StartLocation).Size();
if (PathTravelled >= PathLength)
{
FVector SwapTemp = StartLocation;
StartLocation = EndLocation;
EndLocation = SwapTemp;
}
FVector Direction = (EndLocation - StartLocation).GetSafeNormal();
Location += Speed * DeltaTime * Direction;
SetActorLocation(Location);
}
A bit of clarification here. I have found VInterpTo, etc. But those accelerate fast then slow down. This causes a very jerky movement of my platforms. I’d like to to accelerate slowly, get to max speed half way through the path, then start to decelerate again. Then rinse and repeat, just going the opposite direction.
I use FMath::InterpEaseInOut()
http://api.unrealengine.com/INT/API/…Out/index.html
FMath also has EaseIn() and EaseOut() and Sin and Exponential versions.
http://api.unrealengine.com/INT/API/…ath/index.html
2 Likes
I searched all through that header file and did not see those… I feel like an idiot for missing them. Thanks for pointing it out. I don’t just want to use the functions though. I want to pick them apart so I can learn what is actually going on in the background. I’m glad something is built for me already, but I don’t feel good about not knowing how it does what it does. Thanks again!
I have no idea if this will work but
/* you need the following member variables in your class
private:
float SlowRadius; // The max distance from an end point before slowing down
float GoalThreshhold; // How close to and end point to get before switching (maybe necessary?)
// Must be between 0 and 1. Make it small like 0.00001f
float MaxSpeed; // The maximum speed the platform can move
float Speed; // The speed at which the platform currently moves
---------------------------------------------------------------- */
void AMainPlatform::UpdatePlatformBob(float DeltaTime)
{
if (!PlatformBobs) { return; }
FVector Location = GetActorLocation();
float PathLength = (EndLocation - StartLocation).Size();
float PathTravelled = (Location - StartLocation).Size();
FVector NearestLocation = PathTravelled > (PathLength/2.0f) ? EndLocation : StartLocation;
float RadiiFromNearest = (NearestLocation - Location).Size() / SlowRadius;
// Assuming FMath::Sin() uses degrees, i don't remember; use 1.57f for radians
Speed = RadiiFromNearest > 1.0f ? MaxSpeed : MaxSpeed * FMath::Sin(90.0f*RadiiFromNearest);
if (RadiiFromNearest <= GoalThreshhold)
{
FVector SwapTemp = StartLocation;
StartLocation = EndLocation;
EndLocation = SwapTemp;
}
FVector Direction = (EndLocation - StartLocation).GetSafeNormal();
Location += Speed * DeltaTime * Direction;
SetActorLocation(Location);
}
Thanks for that. I’ll give that a shot and see what happens. For the time being I’ve implemented timelines and it works nicely. Just seems unnecessary though.
I tested my algorithm and found that it must check which location is nearest when it swaps:
if(RadiiFromNearest <= GoalThreshhold && NearestLocation == EndLocation)
{ // swap start & end locations }
otherwise it gets stuck in a fit of switching the start & end locations frame-by-frame and locks the platform once it first arrives at the initial EndLocation.
[spoiler]i created my test case in GameMaker Studio 2, not Unreal, because i am a scrub and it was easier :^)))))))))[/spoiler]