Download

How to add custom n-body gravity field solver to UE4?

Hi,

I’ve put together my own RK45 based n-body gravity field solver in C++ and have a pretty basic visualization setup in Ogre3D to test it out. I’ve been wanting to use that gravity field solver as a starting point for a semi-realistic spaceflight game, my own rigid body dynamics class or some engineering visualization sims. Was excited to see UE4 go “free” and thought this was a good opportunity to get my projects going in a very well supported, mainstream engine.

Just curious if anyone could point me in the right direction on a problem I’ve run into. I think it’s pretty fundamental.

Been looking through tutorials and wiki’s for a month or so now. Basic UE4 blueprint and C++/UE4 C++ is no problems and I think I understand most of the example applications shown.

The problem I have is that I just can’t figure out how to bridge the gap between my generic n-body solver class and the UE4 actor world space. It was easy at a low-level with just basic OpenGL or even Ogre3D. I think the problem I want to solve is applicable to any environment-wide physics effect I would want in UE4 where a certain set of actors to respond to each other in a certain way based on the output from another actor that is monitoring their properties. I think I am missing some fundamental UE4 concepts just beyond the basic examples, or missed something in the basic examples :).

  1. What is the proper way to “register” an actor class with the solver class? I’d imagine i’d just instantiate the solver as another actor to get it into the game world space, but not sure how to go from there.

  2. How do I tick the solver within the UE4 physics frames so that the latest position and velocity solutions, or even look-ahead results are available during all actor rendering tick cycles?

In my generic C++ test setup I just setup a STD vector and push in “gravity field” objects i’ve defined and then iterate the sover from some initial position state. I can add in forces from user controls or predefined arrays easily. For visualization in Ogre3D I have a simple game-loop that iterates as many n-body solver cycles in between each render cycle. Then I just sync the graphics coordinate space to physics for the render cycle to display at the fastest frame-rate possible.

  1. Might be a tie-in to question #2, but is there a good UE4 friendly way to optimize this type of actor to actor interaction? I don’t want my custom physics to be a burden to the rest of the engine.

At a high-level, I would want to be able to add a property to the basic actor class that toggles between the n-body solver and the z-axis normal gravity which UE4 physics uses out of the box. For now, I plan on using the rigid-body physics that comes with the engine, at least until I create a soft-body model for more realistic material deformations and figure out how to get that into UE4 as well. Looking into different real-time CFD methods for calculating aerodynamic forces as well.

I’ll keep looking through the forums and examples to see if I can get a clue. Thanks for anything anyone can show me and I appreciate the time you’ve taken to read this.

Replacing the current physics (PhysX and Box2D) with one of your own sounds fairly ambitious, especially if you want to be able to switch between them all independently. Good luck! I’d probably start by hunting down all the PhysX stuff that’s currently in the codebase - you will probably have to duplicate a similar amount of code, so it will give you some idea of the task ahead.

I’m not sure if this helps you or not, but, you can access the physics substepping Ticks- check this post for more information : https://forums.unrealengine.com/showthread.php?3986-Physics-Sub-Stepping&p=224098&viewfull=1#post224098

Hi AusPaco,

That is definitely helpful :). I never saw that post after searching through here. Thanks for linking me to it. I’ll check it out and the PhysX documentation that they are linking to as well. They brought in the fix your time-step article, so that is good :slight_smile:

The sub-step access is definitely useful as is the general discussion for improving responsiveness and accuracy of the PhysX solver. The PhysX documentation on gravity references a SampleCustomGravity example that can be used to simulate radial gravity fields as well as any other custom radial force (electrostatic force?). So, it sounds like if I am willing to give up on using my own solver, I can use the PhysX one to get what I want. Might go down that path to play a bit, but I’d like to keep control over the solver for the gravity field to allow me to the do long-duration, high-accuracy, n-body solutions for interplanetary travel. Not sure what PhysX uses, or if I can even get an integration accuracy spec out of it. But I wouldn’t mind using it for the ridgid body dynamics.

I’ll keep digging. Thanks for the spring-board.

Depends on how accurate you want to be. Quick, dirty, and inaccurate way to do it would be to simulate physics on all your actors, then have a GravityController actor that has an array of all the physics-enabled actors in the scene. Every frame, it takes an object from the array, adds a force to it and all the actors after it in the array based on their masses and distances, and then goes to the next actor. Make sure you only add the forces between each actor and the ones after it in the array, because the forces between it and the ones earlier in the array have already been applied.