Creating gravity physics with Newton's Law of Universal Gravitation

Hello!

This is my first dev post.

I would like to create gravity for an object or many objects using Newton’s Law of Universal Gravitation. From what I have seen on other posts it is possible to change gravity through world settings and even through Character Movement within the pawn, but what I’m trying to do is a bit more dynamic. The formula for what would be the gravitational force is:

*F = (GmM)/(r^2)*

F is the resulting gravitational force.
G is the gravitation constant 6.67 x10^-11.
m is the mass of a smaller object.
M is the mass of a larger object.

Full transparency this is for a college project, Physics with Calculus, and I am a CS major. I know the math and have done the calculations so the classwork portion is done. The wall that I have hit is how to properly create gravity in UE4 using the formula.

The goal here would be to create two objects: one with a very large almost planetary mass and another with a much smaller mass similar to the average human. Their masses would get passed to the formula as well as the distance between the two objects. The output would be the resulting gravitational force.

I am also curious if either Blueprints or CPP would be easier to build this in. I do have a bit of experience in CPP and since I’ve already done the physics work in Python it would not be hard to translate to a different language. Ultimately, I am looking to get a vector <a,b,c> output since that would be easiest to implement into later parts of the project.

If anyone could point me in the right direction to get this working, it would be greatly appreciated. Thanks in advance.

1 Like

Creating gravity for an object in Unreal Engine can be done using either Blueprints or C++. It highly depends on how much performance you require. Physics should technically often be done in C++ for that reason. However, since this isn’t for a game and you have little experience with Unreal Engine I highly recommend diving into Blueprints.

I also think there are some existing components you can use in order to simulate gravitation without creating everything from scratch. I suggest looking into what you have available to you before you create a system from scratch.

Here’s an example of how you could implement this in Blueprints:

  1. Create two actors, one for the large object (e.g. “Planet”) and one for the small object (e.g. “Human”).
  2. On the “Human” actor, add a “Sphere Component” to represent the small object’s mass.
  3. On the “Planet” actor, add a “Sphere Component” to represent the large object’s mass.
  4. Create a custom event in the “Human” actor’s Blueprint graph and call it “CalculateGravity”.
  5. In the “CalculateGravity” event, use the “GetActorLocation” node to get the location of the “Human” and “Planet” actors.
  6. Use the “Subtract” node to calculate the distance vector between the two actors.
  7. Use the “Divide” node to calculate the distance between the two actors (length of the distance vector).
  8. Use the “Sphere Component” nodes to get the mass of the “Human” and “Planet” actors.
  9. Use the “Multiply” node to calculate the gravitational force using the formula F = (GmM)/(r^2) where G is the gravitational constant, m is the mass of the “Human” actor, M is the mass of the “Planet” actor and r is the distance between the two actors.
  10. Use the “Normalize” node to get the direction of the gravitational force vector.
  11. Use the “Multiply” node to scale the gravitational force vector by a desired strength.
  12. Use the “Add Force” node to apply the gravitational force to the “Human” actor.

I haven’t tried the code above, but hopefully this will point you in the right direction of what you can use and how it in theory can be completed.

1 Like

Back in physx - meaning build a custom 4.22/27 with updated physx Dlls - the engine does support the model by manipulation of the gravitational constant.

Note that the engine code limits the value of the mass of objects. Also note that physical animation is = to the strength of all the gods from everywhere + Kratos, meaning you can move a planet with character animations :stuck_out_tongue:

See above, but if the point of OP is to

Then I’d just suggest building out your own C++ classes and calling it a day.

How you go about it?

  1. custom class called “CustomActor” that extends the already extended Actor class.
    instantiate custom variables, mark them required and editable in engine.
    Build the class. Create objects from that custom class. Enter appropriate variables. Save em up.

  2. Create a “PhysicsManagement” class, set all the required things you need in it / private / public function(s) returning whatever is needed, as well as either public or private values for what you need - say gravitational force can be changed, that’s got to be set to public and exposed.
    Expose the functions needed to get values.

  3. Go back to the CustomActor class, set a trigger of some sort for when to actually perform gravity checks or not.
    Create the code to move the object according to the values that the manager will return. (easiest way is probably to access the manager from the CustomActor [add a variable of type PhysicsManagement to the actor, expose it, make sure to select it once the customactor is in the level], but you could also go back and implement a blueprint interface to it, so as to instantiate via interface calls).

This is actually very similar to having to create an Ocean Manager for instance - where you want the wave height to be returned according to whatever math function powers the wave for x/y at game time.
So maybe you can draw some inspiration from that by looking over the community ocean project (is that still a thing even?) if it’s still a thing.

I’m assuming we are talking gravity in a vacuum in terms of outputting results. If so, then the directional vector should be calculated based on The heading At speed, rather than 2 locations. It gets a bit more complex.
Particularly because for it to be applied you want to calculate the value of the Next frame, not the current frame.
You need to know what speed/location the object will be at next, so that you can set the location for the frame to come. Or else the simulation would lag behind a frame or worse, be frame rate dependent, causing wildly different results based on the FPS of the project.
When building your own custom system, you may actually want to start thinking a bit on this before you even start.
Your functions for applying force will essentially all need to depend on a specific game time…

Hey! It took me a few days to get back to this, thank you for your response. I will have to take some time over the next few days to try this out. I cannot thank you enough for this step by step breakdown of how to approach this. You are right that I don’t a lot of experience with UE. I’ve messed around with the starting tutorials in the past but haven’t had a lot of time to dedicate to learning. Hopefully after this term I will have more time. I will get back to you on the success and if I have more questions here. The entirety of the project is revolved around the physics and representing that visually. There wont be any really graphically intensive portions to the project.

1 Like

Thank you for the insight here, I will dig more into this when I get the time this week to sit down and start working more heavily on this side of the project.

You mentioned that the engine code limits the mass of objects. Luckily, I think, my primary object for simulating gravity is Oumuamua, an oblong meteor. I don’t believe that the mass of the object would be larger than the engines limits but if thats the case I will probably just have to scale the masses for all objects to properly simulate gravity.

I will definitely take your advice here and look over the community ocean project, if its still a thing.

We are talking about gravity in a vacuum, so the only objects that would be measured and have interactions would be the person actor and the meteor.