Can the UE be used efficiently for pure physics simulations?

Hello Community!

currently I’m preparing for a rather complex software project in college related to the the subjects around artificial intelligence.
The final goal is to make a dragon or bird teach itself how to fly.
This will require a genetic/intelligent algorithm which takes the model of a dragon or a bird and adjusts the controls of it’s wings (like its “muscles”) as well as possible to perfect the efficiency at flying with this model.

There is this famous project here which is my inspiration: Flexible Muscle-Based Locomotion for Bipedal Creatures
For simplicity, the model in my case will consist only of several plates (not curved wings like a plane, rather think of a bat) connected to each other with hinges and actors simulating the muscles.

The evolution-based algorithm requires a so called fitness-function. That means I need to test how well the current settings perform (like what’s visible in that video). That’s where the engine like UE matters.

From what I can tell, the engine needs the following requirements:

  • Able to separate rendering and physic-computation (like simulating 1000 steps with different values but only rendering only one of them actually on the screen or work entirely in the background till the best values have been found)
    EITHER:
  • Able to simulate real air-forceback, as in if the wing is pushed downwards, the physic-engine has to compute how much imaginary air has been pushed out of the way and apply a contrary force to this section of the wing. Simple movement-damping does not suffice for that!
    OR:
  • Since I doubt the engine already supports the mentioned functionality, it requires the possibility to insert this myself into the physics code in a performant way. With C++ at best.

By the way, I’ve already realized such a functionality in 2D:


The blue rectangles are the air which has been pushed out of the way.

My question now is, would PhysX together with UE4 be the right environment for my Project?
Does it match the mentioned criteria?

Another engine I’ve looked closer at, ist Unity3D. That one would support the possibility to insert the physics code like that and it has already be done for an interesting boat simulation.
However, I’d prefer UE though since C++ ist usually faster than C# which Unity3D uses.

Huge thanks in advance! :slight_smile:

I would think the precision of the physics simulation would be an issue, since it sacrifices that for speed. I would be very surprised if physics don’t update on a different thread though, and you could definitely insert some changes since there is full C++ access. People have done some pretty complex stuff already by having the source code.

You should ask this question on NVidia CUDA/PhysX (or ATI’s) forums. They know limitations of physics simulation.

Unreal uses simulation for pure visual effects. I doubt you can do your simulation without modifying (or exposing) more parts of unreal physics engine, you need much more from it that unreal can give now. So I am pretty sure that you will be forced to extend or rewrite unreal physics engine.

And if you dig that deep you can extend it to have (or replace with) all current implementations from nvidia (or ati), you can also focus on one hardware vendor (it does not need to be compatible with mobile, consoles html5 etc).

Thank you very much for the responses, guys!

In the meantime I’ve looked more into the documentations of UE as well as of Unity3D. The conclusion is that they both seem to support about everything I need and both use PhysX as the underlying engine for physic simulations, so I don’t expect huge performance differences…
However, that means the question stays which engine to use.

Well, I’m not too sure how deep I need to go. From what I’ve seen, I can use constrained hinges. Basically most settings from this article: Physics Constraint Reference in Unreal Engine | Unreal Engine 5.3 Documentation
Using soft limits this alone should suffice to create the basic, flexible wing itself.
The stiffness and the damping of the hinges are already a few good variables to represent parts of the “chromosomes” for the genetic algorithm.

Angular motors would mimic the muscles which move the wings where they are attached to the body.
I know in reality, muscles work a little different, but that shouldn’t be very significant in this case.

The only problem remaining, is what I had already mentioned: Calculating the “buoyancy” as the force, the wing produces.
This means to compute the “Bernoulli Effect” - at least in a much simplified form.

There’s the question how efficient I can do the following steps:
–Retrieve the movement-vector of each plate-segment of the wing for the current step
–Retrieve the orientation-vector of each plate-segment of the wing (to compare the two)
–Calculate current forces produced by air-friction in this step for each segment
–Apply corresponding forces back against each segment

The key-feature would be to retrieve those vectors in C++ to perform the computations in an efficient way.
Does UE have a significant delay for accessing that data or would it actually be possible to get something like a pointer directly into the memory?
After all, this has to happen about 15-20 times per step and per instance.
I assume to require ~200-300 steps until the algorithm can tell whether the current chromosome (current variable-set) is actually a good one or not.
Additionally there will be many instances running simultaneously to create a new generation of chromosomes before the whole process restarts over and over with a part of the crossed, mutated, best new chromosomes (“natural” selection).

The talk with my profs in charge of the project didn’t really help with the decision as one of them doesn’t know much about the engines and the other actually teaches with Unity3D in a course (which I sadly didn’t visit yet) but that’s purely traditional motivated since he started with that several years ago when UE wasn’t beginner-friendly at all (from what he said).

Hmm… hard to decide… have to start next week and guess I wont have time to look deep enough into both environments to be sure…

[QUOTE=TooManyfps;399287]

There are also other options for pure simulations:https://youtube.com/watch?v=FZOYnfMVO2k[/QUOTE]

That’s quite interesting! Not sure I should ago from a game engine though. It would just give a lot more flexibility for graphics.
Since I’ll be presenting this project on the so called “Media night” of my college, I might try to create a neat visualization if I manage to get a ridged model of a dragon (and simply attach it to the plate-segment-structure below).

P.s. sorry for writing so much… Thought someone might be interested and I’m currently about to write a brief summary of my concept for the profs anyways.

Last time I tried to make physically controlled walker robot in blender 3d and Unity3d (springs for muscles, pieces held together with constraints instead of being parented), its constraints couldn’t hold it together and it fell apart. It was worse in blender 3d, than it was in unity, though.

I think that the problem was caused by spring constraints. Having too many spring constraints attached to object would most likely require more precision than your run of the mill ragdoll. You can probably achieve higher stability if you won’t use springs and won’t try to model muscle forces.

Since Unreal Engine also uses PhysX, you might run into similar problem here. You’ll need to research physx for the purpose of increasing joint solver precision. Physics engine probably runs at fixed framerate, but I won’t be able to tell where exactly that happens.

In your scenario it could make sense to use gaming engine for visualization only, and create separate app for simulation. Transfer initial state into application, run application to simulate, then request required state from time to time to visualize it. Communication can be done via any kind of Interprocess Communication tool.

You might also want to google about “brevecreatures”:

That’s one of the reason I’m not going “full” and try to simulate something like a bipedal being. That requires a lot more accuray than what I plan.
This issue you mention sounds a bit odd. Why would a physic model “fall apart”?
However, didn’t plan use loose springs but only fixed hinges with the possibility to move.
The “stretchyness” of real joints (of a bat for example) is not really significant for the result.

Are you sure about that?
From what I know, engines (graphical as well as physical ones) tend to work time-independent, using delta-values to compensate different step-lengths. So theoretical that should allow the program to utilize nearly 100% of the CPU power if the graphics don’t limit.
Indeed those are details I should better ask in physx forums.

That’s not a bad idea but think that wont work timewise… and my project is about the AI, not the visualisation.

Yep, I know this one already and was my second inspiration :slight_smile: Cool works for that age!

It would fall apart when physical constraints can’t handle forces that are applied to the system. Meaning they either can’t completely cancel forces that violate constraints or do not have proper correction mechanism, so object keeps moving despite the constraint. F = ma, so if improper F is not correctly cancelled/transferred, you still get “a” applied to the object.

Physical constraints are enforced numerically, basically joint has equation that either cancels force outright or transfer it to linked object. So, when force is too big (object is to heavy) or too unpredictable, you’ll get trouble. Correction mechanism in combination with springs can cause object to oscillate, because force of the spring is based on distance, while distance is based on velocity/acceleration, which are based on force, which will be generated by the spring.

You could check those articles for details: http://chrishecker.com/Rigid_body_dynamics

Blender version of robot collapsed (despite having “fixed” constraints on every joint), because it was too heavy and because blender game engine is buggy.

Physx version of robot in unity fared better, because some of the joints couldn’t handle the pressure, apparently.

However, it was a fairly quick one evening test, and the robot was two-legged walker roughly 3 to 5 meters tall with real-world masses and density of iron. Obviously that was too much for the physics engine.

It is not about stretchyness, it was about having force applied to specific point of the model, like muscle. IIRC, breve creatures, by the way, pretty much search for the most efficient combination of frequencies and phase shifts for sinusoidal generators that control joint rotation (you pretty much have rotation=sin(t*x) controller on legs there). That wasn’t what I wanted to test.

I don’t know how UE uses Physx engine, but using fixed timestep is highly recommended practice for stability purposes. With variable timestep “feel” of the game may change depending on hardware, and some objects (like cloth) may explode.That’s also one of the possible reasons of having 30 fps lock in some games.

I’m absolutely certain that unity physics engine runs at fixed timestep (50 fps by default) and uses tricks to smoothly integrate with variable timestep logic. When unity engine performs variable step, at the beginning of the step, it runs enough fixed timesteps so the physics will “catch up” with the world.RigidBody collision detection in unity is performed during fixed timestep, by the way.

Also see: Fix Your Timestep! | Gaffer On Games

Only if physical system is multithreaded.

I think you’ll spend more time trying to get physics work in the engine, but the decision is yours. It shouldn’t take too much time to create simple standalone program that runs physx simulation and takes initialziation data from TCP/IP socket.

I’ve made a simple airplane simulation as a collection of airfoils. Each wing, tail and all control surfaces where done as an individual surface, so they can be broken attached or detached and flight behavior will change immediately. All code was done using blueprints and worked just fine. Necessary forces where calculated in blueprints and PhysX is used only for integration of forces and collision detection.
In your case I’m not sure if you need collision detection, so what is left are joints and lift/drag calculation. Assuming that you want to use empirical formulas and not full fledged fluid simulation. In this case the choice of physics engine would be more about choosing a “better” integrator. Which physics engine is better in this sense is hard to tell. Just a glance on an issue with integrators: http://lolengine.net/blog/2011/12/14/understanding-motion-in-games
From the selection of major engines on market you have: PhysX, Havok, Bullet, Newton and ODE. Another option is to write your own integrator, I’ve heard that RK4 is used a lot in racing simulators for example.