Adding the same force to all connected bodies

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Adding the same force to all connected bodies

Postby Schmackbolzen » Wed Sep 27, 2017 1:25 pm

Hi!
Is there an easier way to add the same force to all bodies which are connected via joints than to call the corresponding function for each body? It would be nice if there were a function in Newton for this which also might be faster than all those calls.
Schmackbolzen
 
Posts: 26
Joined: Mon Feb 16, 2015 5:37 pm

Re: Adding the same force to all connected bodies

Postby Julio Jerez » Wed Sep 27, 2017 2:24 pm

I presume you are making a model, a set of rigid bodies connected by joints like an airplane or a vehicle.

There is such function as engine functionality, but is quite easy to make that feature on the application side, by using the existing functionality.
here is one way

-make a Listener
there are few demos how to add a listener in the demos sandbox
../dynamics\applications\demosSandbox\sdkDemos\DemoListenerBase.cpp and .h

-in that listener class you can add a vector to hold the rigid bodies that form you model and maybe an array to hold off the joint that form your model.

the Newton listener have two callbacks
-PreListener called after force and force call back is call for all bodies
-PostListerner called after simulation happens but before the transformation are set.

-in the pres listener callback you can iterate over your array of rigid bodies
and using:
NewtonBodyAddForce (const NewtonBody* const body, const dFloat* const force);
NewtonBodyAddTorque (const NewtonBody* const body, const dFloat* const torque);

you add to the environment forces like gravity and drag that were applied in the basics force and torque callback.

almost every single feature in newton since 3.xx uses the functionality for modeling high lever functionality. but for some reason that I do no really underhand people have a hard time underrating how to use listener.
My guess is because the concept of a model since to be to abstract and is unique to newton, and people are no familiar with.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Adding the same force to all connected bodies

Postby Schmackbolzen » Fri Sep 29, 2017 12:13 pm

Thanks! I did not know about this feature, because there is no documentation. I changed my code like you suggested and it works.

I noticed two things:
  • If you don't set all callbacks for the listener, newton crashes
  • For e.g. the NewtonBodyAddForce function there is a comment which says
    This function is only effective when called from *NewtonApplyForceAndTorque callback*
    so one would never think that it would work in the listener callback.

Also it would be nice if there were functions which take an array of bodies and one for forces so you just have one call instead of a whole bunch. This should be much faster, especially when you use the dll, like I do.

I also have a different question which is more engine specific:
If I want to control a body, is there any negative side effect to add the values to the velocity instead to the forces? Since I know the mass of the body I can rewrite f = m *a to a = f/m and just add the acceleration to the velocity. I have tested it with NewtonBodyGetVelocity and NewtonBodySetVelocity (since there is no NewtonBodyAddVelocity) and it works. The idea is to reduce the number of calculations, since I can save the acceleration and it doesn't get recalculated that often.
Schmackbolzen
 
Posts: 26
Joined: Mon Feb 16, 2015 5:37 pm

Re: Adding the same force to all connected bodies

Postby Julio Jerez » Sat Sep 30, 2017 10:31 am

Schmackbolzen wrote:If you don't set all callbacks for the listener, newton crashes

oh yes you are right, I fixed now.
Please sync again

Schmackbolzen wrote:This function is only effective when called from *NewtonApplyForceAndTorque callback*

that comment should say This function is only effective when called from 'Newton Update"
the reason is that what is says is that this is a legacy from newton 1.xx
but now force can be added from any call back

Schmackbolzen wrote:Also it would be nice if there were functions which take an array of bodies and one for forces so you just have one call instead of a whole bunch. This should be much faster, especially when you use the dll,

Maybe, but that defeat the purpose of the listeners which is far more general solution thanm adding specialized function to add a constant force, for example.

Schmackbolzen wrote:If I want to control a body, is there any negative side effect to add the values to the velocity instead to the forces? Since I know the mass of the body I can rewrite f = m *a to a = f/m and just add the acceleration to the velocity. I have tested it with NewtonBodyGetVelocity and NewtonBodySetVelocity (since there is no NewtonBodyAddVelocity) and it works. The idea is to reduce the number of calculations, since I can save the acceleration and it doesn't get recalculated that often.

you are looking that the body in insulation the equation f = m * a you are right on m and a, but not in assuming f I knows, in the equation f mean the summation of all forces. you probably know the force your are adding by you do no know of the reaction to collision or joints which is what he engine try to calculate and add to the know forces.
I know the equation since so simple, and it make one something why that simple expression Is such a big deal, and the reason is that the expression is no a formula is an equivalence that relate the geometry of a body m * a to the energy part of the environment sum of f. once get that
you understand why that simple expression is such a powerful tool.
I would no worry about performance, that is such a simple thing that does make any difference.

what are you try to do, is you tell me I can give you some pointer how to control you object with forces in a precise manner. Modeling with Math and physics laws is an area on physic simulation that is covered very little but all physics engine, and is somethin I like the user of Newton start to use more, the engine has all those fumtionality build but they go unnoticed.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Adding the same force to all connected bodies

Postby Schmackbolzen » Sun Oct 01, 2017 5:31 am

Julio Jerez wrote:Maybe, but that defeat the purpose of the listeners which is far more general solution thanm adding specialized function to add a constant force, for example.


You misunderstood, I meant for e.g. NewtonBodyAddForce, which I call in the listener, that you have a version which takes a list rather then having a loop with over hundred of calls (if you have a lot of bodies).

Julio Jerez wrote:you are looking that the body in insulation the equation f = m * a you are right on m and a, but not in assuming f I knows, in the equation f mean the summation of all forces. you probably know the force your are adding by you do no know of the reaction to collision or joints which is what he engine try to calculate and add to the know forces.
I know the equation since so simple, and it make one something why that simple expression Is such a big deal, and the reason is that the expression is no a formula is an equivalence that relate the geometry of a body m * a to the energy part of the environment sum of f. once get that
you understand why that simple expression is such a powerful tool.
I would no worry about performance, that is such a simple thing that does make any difference.

what are you try to do, is you tell me I can give you some pointer how to control you object with forces in a precise manner. Modeling with Math and physics laws is an area on physic simulation that is covered very little but all physics engine, and is somethin I like the user of Newton start to use more, the engine has all those fumtionality build but they go unnoticed.


We are trying to build a 3D spacesim game (just a hobby) where you control your ship in third person. All the thruster maximum forces etc. are converted to acceleration and saved using the complete mass of the ship so that we have the accelerations in the end which only need to be scaled in intensity per thruster (depending on its power) and added up for the complete acceleration (there is no rotation, we assume the forces never cause one). If the ship loses a part the values are recalculated since the mass has changed. For rotation we assume different thrusters which use the inertia values per axis calculated for each body which we get from newton via NewtonBodyGetMass and summed up for all of the bodies so that we get the complete inertia and in the end three acceleration values which just need to be scaled depending on thruster power. As you can see the flight model is rather simplified, but we save a lot of calculations, which probably will play a role if there are a lot of ships (every ship will use this model, that means other players and AI controlled ones).

The ship has a lot of parts which are connected via joints. For the fixed joints we construct compound collisions so only moving joints are used.

If you have a better way to control the ship it would be nice to know :)
Schmackbolzen
 
Posts: 26
Joined: Mon Feb 16, 2015 5:37 pm

Re: Adding the same force to all connected bodies

Postby Julio Jerez » Mon Oct 02, 2017 11:28 am

when you say the ship has lot of parts, are you saying that is like a space station when the move part are more of less of equal mass. or is this is like a car or airplane where there are moving part by the mass o the moving part is insignificant compared to the main body mass.
ex:
say you have a car is customary to neglect that tires because they has very small mass and the degree of freedom along the at free to move does no affect the vehicle dynamics.

but if you have a trailer, the trailer mass if comparable to the vehicle mass, and affect the motion of the vehicle

I mention this because in modeling the the equations of motion of a vehicle, usually you know what the vehicle want to do, and when formulation the equation some assumption can be made.

again say you have a car, since car move mostly in straight line, when calculation the motion in body center, rotation along longitudinal; motion can be neglected, but lateral rotation can not.
if one the other hand then vehicle is a boat or a hover craft the both longitunal and lateral rotation has to be considered.


if you vehicle fall on the category of a large mass representing the vehicle and some small mass attachment use for trusted and other thing that this is a special case and I can help you with and
"On board computer" that let you control you vehicle with forces.

finally again, I understand that controlling a vehicle by manipulation the acceleration or velocity directly is the thing most people do when try to model a complex vehicle.
by believe me the worse thing you can do.

basically the problem is that most people know how to add all the force action of a body, and the integrate the body. so it seem that since the expression f = m * a is so simple why bother with forces when the acceleration is what count, why no manipulate acceleration?
furthermore you also seems that connected bodies with different masses move with the same acceleration but the forces action on them are different, more reason to just manipulate the velocity and acceleration.

Bui this is the wrong thong to do for many reasons. I mention few.
- your vehicle will always look wrong, this is because when you see the acceleration directly, the vehicle will brake the conservation of momentum and people are very sensitive to that, the can tell you is wrong even if they do not know why.
-it will brake the equation of motion, acceleration is the dependent variable and forces at are the independent variable, so change velocity only work for free body, the movement is come in contact with anything you get weird behavior like random explosions.
-it becomes a nightmare to control and tune,

the way to go about is by solving the inverse problem,
give the vehicle, and the central force and torque that you want acting on it,
calculate the values of each of the individual forces (thruster) so that the vehicle achieve that goal.

here the problem is different, because in calculation the individual forces, you know the capability of that thruster in insulation,

example say you have an air plane, that has several lift on the wing, rotors, multiple engine and so on.
if you want to know how to manipulate the controller so that the air play can take off of a runway of some length,
in the direct method when to se the velocity directly you may be able to tune it after hundred of trial and errors. and good luck the setting of the thruster are rotors.

with eth inverse method, the problem reduce to this.
-given the run way length calculate the velocity that the vehicle need a the end
-get the average acceleration,
-with the acceleration calculate the angle of attack so that the lift math gravity
-calculate the horizontal force the the thruster need to produce to get that acceleration and the end of the run way.
-this give the central force, that need to be applied to the vehicle.

you feed that to the computer and the computer produces a list of the force on each thruster, the orientation of each rotor, the steering values and so on.

is tater you want a to take off for a different run way, the calculation is the same, so the process
adjust, it will also show that for example in short run way the airplane take off at a large inclinations, but in long run way if can take off almost horizontally. It will also tell of it can do it or not. And that is what make people like a project.

please tell me what kind of vehicle are you modeling. Newton has all the tools to achieve that but is has gone unnoticed.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Adding the same force to all connected bodies

Postby JoeJ » Mon Oct 02, 2017 2:38 pm

I assume your biggest concern here is about performance.
I think an interface to add the same force to a cluster of bodies makes no sense because it's totally unrealistic and would only intend people to do things probably wrong.

But having many multibody spaceships is a interesting problem.
I would handle this with a LOD system: Collapse distant ships to a single compound body. As you get closer and few / single ship(s) comes to focus, switch to them to multibody representation. The switch should go unnoticed if you do it correctly.
For very distant ships, you could even treat them as particles (but still with orientation and inertia) and integrate them on your own without newton (Why using a physics engine for floating bodies in space when integrating as all there is to do? There is no reason to perform collision detection, joints etc.)

That's some extra work but you unlock so much performance that you can do proper and detailed control of near ships. (space ships usually don't have that much moving parts that matter - the 'kind' of vehicle really is important to know...) If you argue that observer position should not affect simulation, what's a real reason why not? Even if a server handles physics for N players, N+neighbourhood is still less than all ships.

The only problem i see is an abrupt change of (moment of) inertia. 3 Value representation used in physics engines means your body always 'feels' like mass distribution in form of an ellipsoid. It can not represent any arbitary shape. Eventually some lying and interpolation might be necessary to hide a hard transition from multibody to compound (i doupt the problem is noticable, but choosing a best fitting coordinate frame for the compound at least may need extra attention.)
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Adding the same force to all connected bodies

Postby Julio Jerez » Mon Oct 02, 2017 3:37 pm

JoeJ wrote:But having many multibody spaceships is a interesting problem.
I would handle this with a LOD system: Collapse distant ships to a single compound body. As you get closer and few / single ship(s) comes to focus, switch to them to multibody representation. The switch should go unnoticed if you do it correctly.


Oh Joe that's was what I was trying to say before with my poor English.
basically most people do not know how to make a mathematical model to simulate some part for a problem.
I ask him to tell me of his model was a large mass with some body parts, or if it was like a space station with many body parts. because this determine the approximation that can be made.

I want to write a tutorial for how to do this with some example because it has gone for too long.

I was thinking of a airplane, because is a case when the body is no subject to collision forces, but space sim is even better.
for example, a space sim can be simulated entirely with compound collision. and eh case that worry you that is the body part move the COG change and that can case hitches, that where math comes in.
say the space ship is a compound collision made of a series of Collison shapes.
and some of these shapes have relative movements
the second law say
F = m * a;

we now see the flaw there, that equation does not account for the mass changing, for thet we go to the more basics from, for equal to the change of momentum

F = d(m * v) / dt

if in the space ship the mas is changing, now we get

F = d (m) / dt + m * dv / dt

m * a = f - d(m) / dt

now the equation is the next force actin on the center of mass plus the rat of change of the mass with time.

remember when I said if we can a vehicle where the moving part as small compere to the mass of the vehicle the d(m) / dt can be neglected

so a model for a small space ship can be represented by F = m * a

a mode for a space station dm / dt is significant and we nee another equation.

for that we can write the expression for the center f mass and write
dg = m1 + m2 +,,, / n

we can write the mumerical equation fro the kinematic motion of each body part because each body part is a rigid body.
CG* m1 = m1 * function (x)
where function of x is some rotations extruded by the joint relation (0)

you can now apply the rule of change and get the rate of change of m1 as a function o fit position and angular velocity

dm / dt = (dm / d0) * (d0 / dt)

but dO / dt is the angular velocity W of that body part,
the rate of change of (dm / d0) may be a constant or it can even be a magic value, K

now we can plug that ito the equation and we get

m * a = F - K1 * W1 - k2 W2 + ....

the terms K1 * w1 are correction factor that are zero when body part is no moving and compensate for the center of pass when there are moving

I know It sound exoteric, but this is how more or less physics and mathematical write model to simulate some objects.

so yes you can model large objects like a air craft carrier, a space station or a 747 made of connected parts that that have motions like vibration, of flex under there weight, stuff like that.
and it call plug into a general physics engine.

it is hard to start with a body with large moving part, is better to start with a small vehicle and build for there.
here is the cool part, by doing this you can make the model are realistic as you can or as arcade as you can base of what you neglect of not.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Adding the same force to all connected bodies

Postby Schmackbolzen » Mon Oct 02, 2017 6:24 pm

I've made a short video of our current prototype: Link. We currently use the assets from Freelancer, as our goal is a similar game. The flight model is yet to be determined, but probably will be similar to the one from Freelancer, which means more arcade like. I am currently adding the velocities as I described to every body of the ship, except for the rotation part, where I just set the omega value for now (not finished yet).

@Joej: I don't think LOD will work, as we either have a lot of players in one place or just one player surrounded with NPC ships. There just won't be too many ships which are far away and we intend to replace the ones which can not be seen with virtual ones so that there won't need to be any physics to be calculated. Since we will use lockstep for network sync the server will have a lot of calculations, so we need to figure out a way to reduce them.

@Julio: I didn't have yet the time to read your explanations, but thanks in advance for taking the time explaining it so elaborate. I really appreciate that and will try to understand it (the maths shouldn't be a problem).
Schmackbolzen
 
Posts: 26
Joined: Mon Feb 16, 2015 5:37 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 16 guests