Use 'Forces' / Physics Simulation for Child Movement instead of simple AddActorLocal

Hello everyone,

in my current project the player can design his own war-machine, comparable to “Besiege” but without any box-system, e.g. he can place all objects freely. There are special object-types like Spinners and Tilt’er (don’t now the correct word in english).
Currently those “transforming-objects” move their children only by AddActorLocalRotation / Offset.

I was wondering if their is a way to make this more physically correct. I would desire a system where every Element of the machine has Input- und Output forces every Tick, since objects of the robot should collide and also the player could create movement-conflicts (for example when the player attaches two Spinner’s to a Rod and rotates them in different directions, or when he connects a Rod with A Spinner, and the other end of the Rod with an object, where the Spinner itself is connected.)

Also I would need it for collision, for example when a Spinner rotates a Rod against a object, the spinner should slow down or stop, depending if the force is able to move the object.

Of course, I would be able to implement it myself, but my question is if there is a proper way that I don’t know about on how to solve this. It would be senseless if I would implement a system by myself if there are “professional” physics systems already provided, since I am sure my system wouldn’t be able to compete.

Physics Constraints sound pretty interesting for the Movement of certain objects, but my key problem is the Collision and Force-conflicts stuff… Any ideas / hints on how to get started with it?

Thanks in advance :slight_smile:

Start experimenting, this seams to be changing from version to version. In 4.3 my airplane constructed from a collection of static mesh components couldn’t collide by anything but root. In later version of UE4 components started to work more consistently but compound actors (a set of actors attached together) were only partially working. Later even funnier issues were discovered like for example no collision detection if body is not moving but rotating.
Try with physics constrain components and AddForce and AddForceAtLocation nodes.

I’m not sure what you mean by force and collision conflict actually. The tricky part with constraints and forces is to find a mass scale balance such that you simplify work for constraint solver. Which is tricky as it’s not intuitive. You have to keep everything as close as 1 to 1 mass scale. If I understand correctly you can do this not by changing mass of objects but by changing mass scale either in constraint or in mesh component properties.

Thanks for your answer. With conflits I mean, for example, imagine a object with two sockets. On socket is for connecting it with the vehicle. The other socket spins the object connected with it. Let’s say a Cube. Now the cube is spinning. But if you establish a second connection from the cube to the element where the spinner is connected, the spinner would rotate itself, what is impossible by the laws of physics.

Well, give it a try :smiley: Besiege physics engine is rather wacky, I have impression that they use a a rig of springs for pretty much everything. Which is good for fun stuff, there was an old rally racer build like this.

I will :slight_smile:
Yes, the physics in Besiege are fun but wouldn’t be fun for my project.
If someone else has ideas, I will just sum up what I need:

Applying a force to an object instead of just moving it. Example: A Spring. The Spring pushes an object in X-Direction, so I would like to give the object a Force in X direction that transfers to other objects, the powered object may hit, like a physical simulation. Spring powers object -> object speed increases -> object hits stone -> stone speed increases & object speed decreases. And the question is not on how to implement it myself, but if there is some system that basically already deliveres handling of forces. Then every object would just need a “weight” and “grip” variable and a Force-In and Output

That’s what AddForce, AddForceAtLocation, AddImpulse and AddTorque can do for you. When use wants to attach objects you can spawn constraint and define what kind of limits it should have - fixed or maybe allow it to bend or rotate in some axis.

Wow, thanks for that great answer! Do you mind when I may ask you one or two more questions about it? :slight_smile:

Okay, I will ask some questions…

Wait, no, I will explain my system:

The player can build his own Bot with elements, where some Elements can perform actions, like rotating, tilt, spinning, extend and so on. I would need the system to correctly move all objects that are directly and indirectly connected to this element. That would be rather easy to accomplish by just parenting, but you mentioned yourself why this is insufficient.

Also it should handle forces, like, for example a rail-element that is extended to 100%. Now a force is impacting on the rail. The rail should retract until a maximal point and then give the remaining power to his connection element.
Example 2: A Spring is connected to the robot and gets released against a cube. Depending on the weight of the cube, the robot, and the strength of the spring both objects, cube and robot should react to the force.

Thats the goal. So, what do I have to provide to implement that? I assume every object that can move child objects should have a constraint. Also I assume this constraint is connectable with the child-element the player attaches to it. How do I implement forces? Calculate them manually? If yes, how to I convert them that constraints react to them, eg what is their data-input? And what about indirect forces, eg the element connected to the constraint doesn’t experience force directly but maybe via a child of 4th generation (layer 4 of the connection tree).

Or do I have to give every element, whether movable or static a constraint?

Thanks for your help again :slight_smile:

Thanks for your answer. I think it really helped me. But what I still don’t know is how to manually move the motors. Just by AddActorLocalRotation / Offset? I don’t think so, or am I wrong? I mean, for example the ventilator. What does a spin look like? Is there some kind of rotational force, or is it really just a tick based actor transform? Or with the spring, do I just scale the Spring in one direction and it is automatically a force? I think there has to be some funcitonality to say the constraint “do this” and “do that”.

And well, my robots are vehicle based, with rods and plates, wheels etc, so no humanoid roboters. But yours look good :slight_smile:

Is there a way to do it in C++?

I am spawning all my objects in C++, so I would like to stay in C++ since communication between C++ and Blueprints is really ****** in my opinion.

So, I will try to repeat what I would basically have to do:

0.) Enable physics simulation on all the objects (all?)
1.) When object is spawnend, at first it doesn’t have constraints
2.) When the object is connected with another element, I create a constraint and set the values for them
3.) Static Objects should be locked on all axis and have locked rotation (act like one object)
4.) Objects than can swing / extend etc should modify the constraint values depending on that
5.) I give objects that can execute forces tick-based algorythms that output forces on their constraints
6.) Done?

So, what would be the best way to do that? Create C++ classes for every type of constraint that can occur, or save constraint-informations in a collective struct to initialize basic constraint-components?
Is there a way to position constraints in the mesh-editor, like sockets? Or do I have to do it manually in Code, since all my classes are C++ based? I mean, to get it done in BP’s I would have to convert all my existing classes to BP’s, wouldn’t I?

Okay, I got the constraints “working”. They react as they should to input-forces from the environment, though I still don’t get them to execute forces by themselfs. I have an object constrained to another, and I want it to spin - like you described with your ventilator. So I get the current rotation of the object every frame, add the additional rotation to add this frame and set this as the target orientation and set the angular drive params, but nothing happens. But I have to note that I don’t know what values to use in the params function… Unfortunaley your image doesn’t really show how it’s done… Is that inside a tick function? I just need to rotate the object around its limited axis a bit every frame…

Let me intrude. this constraint you refer to sounds to me like what they call in the music world midi. Or am I out there. Then use freqency like they do in rc. I picturing drones here,lol.

Awesome work on the physics method TooManyFraps and thanks for guiding us through it. I needed guidance to help get a physics-driven truck working for a client. Seems I’ve underestimated the vehicle task he assigned me.