Download

Atempting to Replicate Phys_Flying could use some help figuring the Math

Hi, asking for some ideas on how to replicate udk’s flying physics.

So I am thinking I should start by declaring these variables:

vector Acceleration
vector Velocity
float VelocityDecay

then apply this principle on tick

if(Acceleration != vect(0,0,0))
Velocity += Acceleration * Deltatime;
else
Velocity = interpolate(Velocity, vect(0,0,0), Deltatime, VelocityDecay);
addworldlocation(velocity * deltatime);

of course is pseudo code, but it’s how im thinking about tackling it, but other ideas are always welcome.

K got it to work to somewhat of a good feeling, for those looking for something like this, here is what i did:



//My default values set on the construct of the .cpp adjust them to what you want, figured I would give a reference to start from
	TurnRate = 150.f;
	AirSpeed = 10000.f; //Multiplies the Axis input to get us a Movement speed result
	AirFriction = 0.25f;

//Dir is a vector parameter wich the components(x,v,z) are equal to the inputs (forward and strafe)
//Accel is a private vector on the .h file, used to control the acceleration of the pawn
//Air friction is a private float on the .h file that dictates how fast the Acceleration should change
//Rot is a float Parameter that takes the turn input
//This function is called on every tick, because I have several different subticks, I am not pasting the function declaration
{
        //First we find the foward vector by vectorizing the rotation
	FVector LocalX = GetActorRotation().Vector();
        //Find the cross vector on the Z plane (the vector 90 to the right of it)
	FVector LocalY = LocalX.CrossProduct(LocalX, FVector(0, 0, 1));
        //Set the Gravity Vector(not symulated but your own "velocity.z"
	FVector LocalZ(0.f, 0.f, Dir.Z);

         //Get a world vector by multiplying the magnitudes and adding them to get a final vector
	 LocalX *= Dir.X;
	 LocalY *= -Dir.Y;
	FVector LocalMove = LocalX + LocalY + LocalZ;

        //Interp the acceleration elements to the desired values
	Accel.X = FMath::FInterpTo(Accel.X, LocalMove.X, AirFriction * DeltaTime, 2.0f);
	Accel.Y = FMath::FInterpTo(Accel.Y, LocalMove.Y, AirFriction * DeltaTime, 2.0f);
	Accel.Z = FMath::FInterpTo(Accel.Z, LocalMove.Z, AirFriction * DeltaTime, 2.0f);

        //Add the move on the world axis rather than the local
	AddActorWorldOffset(Accel * DeltaTime, true);

	// Calculate change in rotation this frame
	FRotator DeltaRotation(0, 0, 0);
	DeltaRotation.Yaw = Rot * DeltaTime;

	// Rotate Pawn
	AddActorLocalRotation(DeltaRotation);
}


I also welcome people trying to do more in the realm of networking and physics! I haven’t looked in a while, but what’s the purpose of a ‘velocity decay’? What does it do for the simulation?

EDIT: Maybe it’s just dampening?

Its a dampening factor, so if the higher the value of the “Friction” the quicker the actor will stop. This allows for a nice hover behavior where say the pawn is on a hoverboard, instead of quickly stopping, it would carry a little bit of momentum.

Of Course being this code is design to apply the local rotation, if you wanted to apply it to the world you could just skip all the vector math at the start and just use your input values for instance on the Interp Section, say for a floaty stick shooter like asteroids