Download

Determining if Velocity is Greater than Gravity

I’ve been writing my own Physics for a platforming game I’m making that I like to call Space Vacation!

I’m writing my own physics so that I can control the direction and strength of gravity, rather than being limited to the Z Vector. Most of this is going well, however I’m running into an issue for my Gravity.

The concept of Gravity (simplified) is that you’re moving at a constant rate of 32ft a second, per second. So 1s = 32, 2s = 64, 3s = 128, etc,etc,etc until you’ve reached the max Air Velocity which limits you to falling at 128ft/s FROM gravity.

HOWEVER, if something (say rocket boots) are thrusting you downward towards Gravity you will surpass 128ft/s as long as that thruster is pushing you that fast.

With that in mind, here is what I’m trying to do; every tick I have my gravity being calculated. I am trying to take the current Velocity FVector, and determine if it is greater than my Gravity FVector. This is where I get stuck, I’m not entirely sure on how to compare my Velocity and see if it is going in the same direction as my gravity enough to STOP adding gravity.


if(Velocity >= Gravity)
return;


Is more or less the idea. This obviously has to take into account direction as well as magnitude/acceleration. Because if you’re going +512 up, and gravity is -512 downwards. You’re obviously greater than gravity, but if gravity is +512, and you’re going -512 (because you’re on the bottom of an asteroid) then Gravity is going to be seen as greater if we don’t factor in directions.

My gravity is setup like so for anyone curious:

Every Tick() we Apply Gravity first by adding Acceleration towards our Gravity Direction, and then we Calculate our Velocity


void UPSA25D_PhysicsComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	if (!Owner)
		return;

	ApplyGravity(); //Apply Gravity first
	CalcVelocity(); //Calculate new Velocity
	//ProcessMove(); //Make the move
	ZeroAcceleration(); //Remove all Acceleration till next Tick()!
}

Gravity is added by adding Acceleration towards Gravity Direction



void UPSA25D_PhysicsComponent::ApplyGravity()
{
	if (!Owner)
		return;

	FVector NewAcceleration = ReturnGravityInfluence(); //Returns Direction + Strength of Gravity

AddAcceleration(NewAcceleration);	
}

then Velocity is Calculated by Adding the last Velocity with all Accelerating forces together and adjusting it based on DeltaTime (Time Passed). This is because Velocity will never slow down unless something makes the object slow, like Friction, Air, or face-planting into the surface. But if you’re in space (implied in the title of Space Vacation) you’ll always continue in that direction.



void UPSA25D_PhysicsComponent::CalcVelocity()
{
	Velocity += (ReturnAcceleration() * FApp::GetDeltaTime());


	if (MovementState == EMovementState::ENUM_Falling)
	{
		//GEngine->AddOnScreenDebugMessage(-1, 0.01f, FColor::Magenta, *FString::FromInt(FVector::DotProduct(Velocity, ReturnGravityInfluence()) ));
	}

	if (MovementState == EMovementState::ENUM_Walking)
	{
		//Velocity -= ReturnCurrDrag();
	}

}

Acceleration is calculated every Tick() (via CalcVelocity()) by taking the Acceleration TArray and adding all forces together.



FVector UPSA25D_PhysicsComponent::ReturnAcceleration()
{
	FVector FinalAcceleration;

	if (Acceleration.Num() < 1)
		return FVector(0, 0, 0);

	for (int32 i = 0; i < Acceleration.Num(); i++)
	{

		FinalAcceleration += Acceleration*;
	}

	return FinalAcceleration;
}


Want to add Acceleration? Just use the AddAcceleration() function that adds a new entry into an Array



void UPSA25D_PhysicsComponent::AddAcceleration(FVector NewAcceleration)
{

	Acceleration.Add(FVector(NewAcceleration));
}

which then at the end of every Tick() gets cleared out so you need to make sure to provide a looping acceleration (like say from a Key Press, or Gravity tick!)



void UPSA25D_PhysicsComponent::ZeroAcceleration()
{	
		Acceleration.Empty();
}


Any ideas or help would greatly be appreciated!