# Dynamic gravity for characters

**URL:** <https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678>\
**Category:** C++\
**Tags:** unreal-engine, Engine-Source-Code, GitHub, Engine-Source-GitHub\
**Created:** [January 26, 2015, 4:45pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678 "2015-01-26T16:45:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xaklse](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xaklse/32/769867_2.png) [@Xaklse](https://forums.unrealengine.com/u/Xaklse)\
**Post date:** [January 26, 2015, 4:45pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/1 "2015-01-26T16:45:43Z")

</div>

> [@](#):
>
> _ **Ninja Character** _ code plugin for Unreal Engine 4.25.x is up **HERE** ]([https://forums.unrealengine.com/unreal-engine/marketplace/1819258](https://forums.unrealengine.com/unreal-engine/marketplace/1819258))!

.

**OLD first post follows:** [HR][/HR]

[QUOTE]  
_ **Ninja Character** _ code plugin _0.9.3_ for _Unreal Engine 4.21.1+_ is up! Go to [UnrealEngine · GitHub](https://github.com//UnrealEngine) and grab the **ZIP file** (not the green button) from one of the mediafire links posted at the top part (_if error 404: [log in to Github, link your UE4 account to Github](http://www.unrealengine.com/en-US/ue4-on-github)\*\*, links are always online_).

is for C+±based game projects.

…com/=cAtlpOk-VlE

.

**VERY OLD first post follows:** [HR][/HR]

_ **NinjaCharacter** _ subclass is up! Go to **\>\>** [UnrealEngine · GitHub](https://github.com//UnrealEngine) **\<\<** and grab the **ZIP file** (not the green button) from the link posted at the top part (_if error 404: log in to Github, link your UE4 account to Github_).  
I see no harm if I share the code I have now, let me know what you think if you use .  
is for vanilla Unreal Engine **4.12.2** and **4.13.1** , you still need Visual Studio to compile your game project; detailed instructions are provided.

.

**VERY VERY OLD first post follows:** [HR][/HR]

=IW9E6XzfDbg

is a showcase of my code adaption of the standard CharacterMovementComponent from Epic Games. Once they’re ready (_ **UPDATE: they’re ready \>** _ ), changes will be shared with everyone through **GitHub** ([https://github.com/](https://github.com/)) (_ **if error 404: log in to Github, link your UE4 account to Github** _), which might be merged into UE4 by Epic (_ **UPDATE: progress aborted** _). Code changes are based on an updated **4.7** version of the engine. [HR][/HR]  
I need help; the last function I modified is _UCharacterMovementComponent::PhysicsRotation()_, where you can read :

```auto

//YAW
if( DesiredRotation.Yaw != CurrentRotation.Yaw )
{
    NewRotation.Yaw = FMath::FixedTurn(CurrentRotation.Yaw, DesiredRotation.Yaw, DeltaRot.Yaw);
}

// PITCH
if( DesiredRotation.Pitch != CurrentRotation.Pitch )
{
    NewRotation.Pitch = FMath::FixedTurn(CurrentRotation.Pitch, DesiredRotation.Pitch, DeltaRot.Pitch);
}

// ROLL
if( DesiredRotation.Roll != CurrentRotation.Roll )
{
    NewRotation.Roll = FMath::FixedTurn(CurrentRotation.Roll, DesiredRotation.Roll, DeltaRot.Roll);
}

```

It calculates the interpolation between the current rotator and the desired rotator by using each component of _RotationRate_ (_DeltaRot = RotationRate \* DeltaTime_). is incompatible with arbitrary capsule orientations, the _DeltaRot.Yaw_ should only affect the local horizontal orientation of the capsule.

How would you fix problem? Any idea? I already had some and attempted to implement them, but they weren’t correct.

---

<div class="post-metadata">

**Author:** ![zeroexception](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zeroexception](https://forums.unrealengine.com/u/zeroexception)\
**Post date:** [January 26, 2015, 6:16pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/2 "2015-01-26T18:16:15Z")

</div>

Wow that is looking great!!!

As for your code question: I guess it depends on the frame of reference for the DesiredRotation and CurrentRotation. If they are transformed to local space, then shouldn’t it work as-is, and then you transform back to world space?

Another approach may be Quaternion Slerp. I might need more context in terms of what space you are working in.

---

<div class="post-metadata">

**Author:** ![Dark-Veil](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dark-Veil](https://forums.unrealengine.com/u/Dark-Veil)\
**Post date:** [January 27, 2015, 4:49pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/3 "2015-01-27T16:49:23Z")

</div>

Very cool project, ! We would love to see reviewed and integrated at some point.

I’m curious how you decided to manage the various gravitational influencers. We had talked about here in the past and thought that having functions like UWorld::GetGravityAt( const FVector& Location ) and a UWorld::GetUpVectorAt( const FVector& Location ) might be a good approach. Those functions would be optimized for a single fixed gravitational influence, but could also be implemented to support all sorts of influencers.

–Mike

---

<div class="post-metadata">

**Author:** ![ULLS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ULLS](https://forums.unrealengine.com/u/ULLS)\
**Post date:** [January 27, 2015, 5:02pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/4 "2015-01-27T17:02:16Z")

</div>

is fantastic!! I need in my life 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9fdc589](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9fdc589](https://forums.unrealengine.com/u/anonymous_user_b9fdc589)\
**Post date:** [January 27, 2015, 10:58pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/5 "2015-01-27T22:58:37Z")

</div>

'Looks really cool ! :o  
I’m afraid I can’t help regarding your coding questions, but I’m looking forward for a testable version.

---

<div class="post-metadata">

**Author:** ![Xaklse](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xaklse/32/769867_2.png) [@Xaklse](https://forums.unrealengine.com/u/Xaklse)\
**Post date:** [January 28, 2015, 11:48am UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/6 "2015-01-28T11:48:34Z")

</div>

> [@Zak Middleton;211489](#):
>
> As for your code question: I guess it depends on the frame of reference for the DesiredRotation and CurrentRotation. If they are transformed to local space, then shouldn’t it work as-is, and then you transform back to world space?
> 
> Another approach may be Quaternion Slerp. I might need more context in terms of what space you are working in.

_DesiredRotation_ and _CurrentRotation_ are determined in world space; I need the FRotator that transforms _CurrentRotation_ into _DesiredRotation_, avoiding “gimbal lock”, then interpolate a fraction, but knowing, for example, that the max allowed “Pitch angle” is determined by the _CurrentRotation_ local space, and that _RotationRate_ is an Euler FRotator. Haaa, it’s complex… do you get it? Or is it more simple than I thought?

I tried to interpolate _current XYZ axes_ with _desired XYZ axes_, but what’s pitch, yaw and roll in case? Did some experiments with azimuth and elevation (FMath::GetAzimuthAndElevation()).

About quaternion slerp, I do use it, but only if all components of _RotationRate_ are equal. See:

```auto

		if (DeltaRot.Roll == DeltaRot.Yaw && DeltaRot.Yaw == DeltaRot.Pitch)
		{
			// Calculate the spherical interpolation between the two rotators.
			const FQuat CurrentQuat(CurrentRotation);
			const FQuat DesiredQuat(DesiredRotation);

			// Get shortest angle between quaternions.
			const float Angle = FMath::Acos(FMath::Abs(CurrentQuat | DesiredQuat)) * 2.0f;

			// Calculate percent of interpolation.
			const float Alpha = FMath::Min(FMath::DegreesToRadians(DeltaRot.Yaw) / Angle, 1.0f);

			NewRotation = (Alpha == 1.0f) ? DesiredRotation : FQuat::Slerp(CurrentQuat, DesiredQuat, Alpha).Rotator();
		}

```

[HR][/HR]

> [@;211992](#):
>
> I’m curious how you decided to manage the various gravitational influencers. We had talked about here in the past and thought that having functions like UWorld::GetGravityAt( const FVector& Location ) and a UWorld::GetUpVectorAt( const FVector& Location ) might be a good approach. Those functions would be optimized for a single fixed gravitational influence, but could also be implemented to support all sorts of influencers.

That sounds great Mike; I don’t really manage gravity by zones yet, I just used last known reverse floor normal for the video when gravity is in DYNAMIC mode.  
I’ll probably leave matter to you, since that change should affect classes that I prefer to not modify without proper experience.

---

<div class="post-metadata">

**Author:** ![WolfSpinach](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@WolfSpinach](https://forums.unrealengine.com/u/WolfSpinach)\
**Post date:** [January 29, 2015, 6:26am UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/7 "2015-01-29T06:26:34Z")

</div>

As a non-coder but lover of Prey and the weird fun that playing with gravity brings, thank you very much!

---

<div class="post-metadata">

**Author:** ![HughieDM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HughieDM](https://forums.unrealengine.com/u/HughieDM)\
**Post date:** [February 6, 2015, 5:02pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/8 "2015-02-06T17:02:25Z")

</div>

Great work! Im looking to mess with gravity my self and you have givin me a great starting poin

Do you achieve the dynamic orientation and dynamic gravity by the normal of the surface or do you have volumes that tell the character how to act when in different location?

---

<div class="post-metadata">

**Author:** ![Xaklse](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xaklse/32/769867_2.png) [@Xaklse](https://forums.unrealengine.com/u/Xaklse)\
**Post date:** [February 12, 2015, 11:22am UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/9 "2015-02-12T11:22:11Z")

</div>

I just used the last known reverse floor normal; Epic has some nice ideas regarding zone-based gravity (check Mike’s comment above).

---

<div class="post-metadata">

**Author:** ![HughieDM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HughieDM](https://forums.unrealengine.com/u/HughieDM)\
**Post date:** [February 18, 2015, 8:46pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/10 "2015-02-18T20:46:31Z")

</div>

So to achieve do you rotate the character and apply the gravity in the negative up vector of the character?

Also do you call when the character is in physics mode or not grounded? or do you Tick the rotation function?

I have been trying to read through the code on how Uworld, the character, and the character component all work. I want to implement the gravity change so that it other physics actors and vehicles. Is there any information directly related to how gravity actually works through all of the classes.

Would love to have a full picture of how gravity affects everything in UE4!

---

<div class="post-metadata">

**Author:** ![do-while-true](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@do-while-true](https://forums.unrealengine.com/u/do-while-true)\
**Post date:** [February 19, 2015, 9:03am UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/11 "2015-02-19T09:03:27Z")

</div>

is exactly what I need to develop a game concept I have! So awesome! However, I don’t want to wait for 4.8. Can I just replace the CharacterMovementComponent files in the engine to get working myself or would I need to do other things?

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [February 19, 2015, 12:22pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/12 "2015-02-19T12:22:42Z")

</div>

Cool! Love the music 😉

---

<div class="post-metadata">

**Author:** ![Xaklse](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xaklse/32/769867_2.png) [@Xaklse](https://forums.unrealengine.com/u/Xaklse)\
**Post date:** [February 23, 2015, 2:00pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/13 "2015-02-23T14:00:16Z")

</div>

> [@HughieDM;224496](#):
>
> 1. So to achieve do you rotate the character and apply the gravity in the negative up vector of the character?
> 2. Also do you call when the character is in physics mode or not grounded? or do you Tick the rotation function?
> 3. I have been trying to read through the code on how Uworld, the character, and the character component all work. I want to implement the gravity change so that it other physics actors and vehicles. Is there any information directly related to how gravity actually works through all of the classes.

1. Capsule is rotated by floor normal; walking character moves along floor while surface is ‘walkable’, you could walk without gravity, I think it’s similar to Unreal’s old spider physics mode.
2. I don’t get the question. Stuff is done per-tick, that’s how Epic programmed character movement.
3. I don’t think there is any documentation, I guess mainly because gravity is always fixed to (0,0,-1). Look for other threads in these forums, you might find something useful for you.

> [@;224835](#):
>
> is exactly what I need to develop a game concept I have! So awesome! However, I don’t want to wait for 4.8. Can I just replace the CharacterMovementComponent files in the engine to get working myself or would I need to do other things?

I didn’t upload the source code yet.

---

<div class="post-metadata">

**Author:** ![do-while-true](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@do-while-true](https://forums.unrealengine.com/u/do-while-true)\
**Post date:** [February 24, 2015, 12:38pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/14 "2015-02-24T12:38:41Z")

</div>

> [@;227390](#):
>
> I didn’t upload the source code yet.

Do you know when you will upload the code? Whatever you do, I think you are doing an amazing job!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8943aceb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8943aceb](https://forums.unrealengine.com/u/anonymous_user_8943aceb)\
**Post date:** [February 27, 2015, 4:17pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/15 "2015-02-27T16:17:21Z")

</div>

Very cool stuff! I’m working on something similar as well currently and I’m looking forward to seeing your solution. I’ve got dynamic gravity working pretty quickly, had no time to figure out the orientation yet.

---

<div class="post-metadata">

**Author:** ![zalo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zalo](https://forums.unrealengine.com/u/zalo)\
**Post date:** [February 27, 2015, 10:49pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/16 "2015-02-27T22:49:20Z")

</div>

I wonder if there’s a smart way to utilize the Distance Field to determine the gravitational gradient based on arbitrary gravitational influencers…

---

<div class="post-metadata">

**Author:** ![Xaklse](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xaklse/32/769867_2.png) [@Xaklse](https://forums.unrealengine.com/u/Xaklse)\
**Post date:** [March 1, 2015, 3:01pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/17 "2015-03-01T15:01:18Z")

</div>

> [@;228153](#):
>
> Do you know when you will upload the code? Whatever you do, I think you are doing an amazing job!

No, I don’t know. Sooner than later, because I think I’m almost done.

---

<div class="post-metadata">

**Author:** ![Moss](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/moss/32/704032_2.png) [@Moss](https://forums.unrealengine.com/u/Moss)\
**Post date:** [March 2, 2015, 1:56pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/18 "2015-03-02T13:56:54Z")

</div>

Would love to see a full PR with , having custom gravity will let us do really crazy stuff!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_48f65eba](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_48f65eba](https://forums.unrealengine.com/u/anonymous_user_48f65eba)\
**Post date:** [March 2, 2015, 7:39pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/19 "2015-03-02T19:39:49Z")

</div>

> [@zalo;231421](#):
>
> I wonder if there’s a smart way to utilize the Distance Field to determine the gravitational gradient based on arbitrary gravitational influencers…

Rather, what you could do is get the hit position of the floor right before the action (jumping, falling, being launched in the air…), then make a plane out of that position and the normal direction of the plane, and calculate the distance from the player to that plane. There you can generate the gravity gradient easily.

If your concern is more about gravity gradient to the OBJECT, you might want to do something more customized, like have an actor posing as the center of mass / gravitational center of your object, then raytrace from your player actor to the gravity point actor and calculate that line’s collision with the object, and the length of that vector would e distance from the floor, in which gravity gradient can be handled that way :V

something more customizable could be a bunch of volumes, or combining volumes with the aforementioned point-line method.

---

<div class="post-metadata">

**Author:** ![Xaklse](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xaklse/32/769867_2.png) [@Xaklse](https://forums.unrealengine.com/u/Xaklse)\
**Post date:** [March 22, 2015, 4:26pm UTC](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678/20 "2015-03-22T16:26:23Z")

</div>

The code is up!  
The base code branch is official ‘promoted’ changelist 2484152 (19/Mar/2015) which is newer than version 4.7.3.

[Next page](https://forums.unrealengine.com/t/dynamic-gravity-for-characters/19678.md?page=2)
