Custom Gravity Direction

Hey guys!
I’m in a deep need to be able to switch gravity’s direction for a specific actor (not just z axis), but it seems this is very hard to do.

I know that this is supposedly going to be implemented in the future into UE4, but since my game is designed around this idea I can’t really wait.
People have worked on it every once in a while, (Any way to modify gravity direction for actor? - Community & Industry Discussion - Epic Developer Community Forums)
but it seems that it doesn’t work for 4.11

Are there any tips to where to start with this? I know it must be done in c++, but I’m not sure where to go from there.

Creating custom gravity is a little bit tricky, but I would recommend creating your own Actor Component and attaching it to your actor, and let the component handle all the gravity. You would have to use the ComponentTick() function to constantly get its owner, and move it in the direction of gravity using DeltaTime (seconds since last frame). You’ll need to trace for collision before each move also, to prevent going through objects or getting stuck in them.

But that sounds immensely sketchy, cause that would not only override complex collisions but also character movement component stuff… also, isn’t gravity acceleration?

Basically, I think I’ll need to do stuff with movement component.

Take this for what it’s worth. I’m very much new to Unreal and I’m still learning how to drill down to certain components in Unreal. I’m also making a few assumptions about how gravity is calculated and applied by the engine. The assumptions I am making is that gravity is an acceleration bounded by a very small terminal velocity (noted by falling inside the engine appears to hit a max velocity very quick). Gravity is an added component to any supplied forces/impulses/accelerations that are provided through the game code and just gets thrown in to the mix to resolve a single vector and rotator to apply to the game object every tick.

I’m not sure how Unreal does gravity, but from a conceptual point of view you could just apply a vector acceleration to your actor by adding a positive gravity vector to negate the current gravity component and a vector in the direction of the new gravity. Take the resulting vector and use the AddForce() function. Depending on where you will be standing you would have to detect the surface you are transitioning to, get a normal to that surface then multiply that normal by the new negative gravity.

Do some Ray Casting or another method to determine where you will leave the normal gravity plane.
Get the scene object you will be walking on and figure out the normal to the surface. There is a GetSafeNormal() function but that might be a normal to the world location vector and not the normal to the plane you would be walking on. One strategy might be to detect where your foot bones contact a new surface and get that normal, this would have the added benefit of it naturally changing the gravity over different geometry ie. a curve vs a 90deg change.

This is pseudo code:

FVector NewGravNormal = GetObjectToWalkON->FindNormalForTheNewWalkingPlane;

float GravMagnitude = 9.8; // or get it from your game mode so if you change the basic engine supplied gravity it will be taken into account
FVector GravNegate = FVector(0,0,GravMagnitude); //where z is the magnitude of the gravity you have set in the game
FVector NewGrav = (GravMagnitue * NewGravNormal) + GravNegate;
GetActor -> Get The UCharacterMovementComponent and AddForce(NewGrav);

Without playing with it I can’t tell you this would work but mathematically it should.

Wish I had more experiance so I could just model it for you and tell you it would work but that is still a month or two away. :frowning:

EDIT: Forgot to add that this would have to be done for the actor under the new gravity every tick so it would belong in the TickComponent for what ever class you put this in. I would just create a Gravity class and attach it to the actor as the person above stated. I don’t think you would need to do anything with a DeltaTime since you would be inserting the new composite vector into a UCharacterMovmentComponent class method and that class would automatically handle any deltas that are related to resolving forces. I could be wrong though.

Kitatus has this kitatus.co.uk might help some.

I’ve not experimented with it yet… working on other things.

That link is dead but here is his Unreal Forum Page for that project with a current DL link https://forums.unrealengine.com/showthread.php?85022-Blueprint-C-Advanced-Templates-Tools-amp-Plugins

It MAY be sketchy, I haven’t dove into the Unreal Physics stuff really much. I could be wrong, but from what I understand what gives things gravity is very hard-coded into the engine at deep layers, and require you to modify all of the Unreal Code to change the direction, which is why they haven’t changed it yet themselves. The best way around it is to turn off Phsyics Simulation and basically start from scratch, and using Components is the easiest way to do that by allowing you to write all your gravity code into one a single component, and apply it to any actor easily.

But I can’t say I’m an expert on the subject to know if this is the best method or not.

Here’s FreeTimeCoder’s explanation on his point/planetary gravity. The same concepts/pitfalls might apply to what you want.

Sadly addforce doesn’t quite work for characters since add movement kinda overrides things (so if addforce is to the left then I can just walk right and override the artificial gravity). So I’d have to do loads of fancy cheese on the characterMovement to change how addforce works, I guess?

Yea I’ve downloaded that and I’ve tried to implement it into my project… only problem is that he created a custom pawn class, meaning he never used character class. My player is a character class, which means I’d have to do some super fancy cheese to get everything working

Sounds pretty useful, and it certainly shows me even more possible problems that I never even thought of! Thanks for the intel! :slight_smile:

In case you haven’t already seen it, be sure to check this one out as well. It was submitted to Epic through a pull request a while back, not sure what the status of that is, but it has all the code you would need to modify the default character:

Hope it helps!

How do you access this code, and how should I use this? Replace the character class? Make a new class? Any quick tips?

Thanks!

I’ve grabbed the files, but now I’m not sure what to do next… how do I recompile engine code?