SOLVED: Forces on a body enigma

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

SOLVED: Forces on a body enigma

Postby misho » Mon Mar 26, 2018 8:21 pm

I'm wrestling here with something I can't explain. I've been working on my spaceflight simulator for a while now... I have it all up and running. Up until a few days ago I was using the "close enough" parameters that would give me a reasonable thrust/weight ratios. But now, I started plugging in "real-life" numbers from Wiki NASA page (rocket thrust vs rocket mass).

My set up is, I have a simple rocket, on a real-life sized earth globe. My gravity vector is always pointing towards the center of the globe, no matter where on the globe I am. I have a rocket, standing upright, so its thrust vector is coaxial with the gravity vector.

In its purest form, my code is:

Code: Select all
dVector gravityForce(...);
double gravmag = vLength(gravityForce);
dVector thrustForce(...);
double thrustMag = vLength(thrustForce);

// debug printout
double angle = 0.0;
angle = GetVectorAngle(gravityForce, thrustForce); // calculates angle between 2 vectors
if (thrustmag > 0.0)
fprintf("Gravity: %f -------Angle: %f---------- Thrust %f\n", gravmag, rad2deg(angle), thrustmag);

dVector totalForce = gravityForce + thrustForce;
NewtonBodyAddForce(body, &totalForce[0]);


When I run my simulation, the printout of the debug code is:

Code: Select all
Gravity: 823750.200000 -------Angle: 179.998895---------- Thrust 7440000.000000


The result is, the rocket is stubbornly still. As you can see, thrust is much larger than gravity, and the forces are in opposite direction, so the rocket should take off vertically!

There are NO other NewtonBodyAddForce or NewtonBodySetForce calls in my sim. The rocket is perfectly still, pointing upwards on the surface of the sphere.

Now, for the sanity check, I scale up the thrust 10x in the second last line of code:

Code: Select all
dVector totalForce = gravityForce + thrustForce.Scale(10.0)


and sure enough - the rocket takes off like a bullet! What is happening here? Why isn't the larger thrust force overcoming the gravity in the first (non-scaled) case? Is it the inertia of the heavy body? Even with a large mass, the force would eventually push it... This is driving me crazy - what am I not seeing???
Last edited by misho on Mon May 07, 2018 6:28 pm, edited 1 time in total.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby Julio Jerez » Mon Mar 26, 2018 9:13 pm

it seem to me this is not right.
dVector totalForce = gravityForce + thrustForce;

I suppose a trust force is in kg * m / s ^2
but a gravity is in m / s ^2
you need to multiply he grtavity by the mass if the body UI am guessing you are using

f = -G * m0 * m1 / r ^2
if m0 is the earth mass, the

f = -(G * m0 / r^2) * m1 = -K * m1 / r ^2

k = G * m0
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Forces on a body enigma

Postby misho » Mon Mar 26, 2018 10:33 pm

Julio Jerez wrote:it seem to me this is not right.
dVector totalForce = gravityForce + thrustForce;


Doing that already - left it out for brevity:

Code: Select all
double toOrigin[3] = { matrix.m_posit.m_x, matrix.m_posit.m_y, matrix.m_posit.m_z };
   normalize(toOrigin); // this gives a vector pointing to origin, of unit length.

   dFloat dGravity = -9.80655;
   dVector gravityForce(mass * dGravity * toOrigin[0], mass * dGravity * toOrigin[1], mass * dGravity * toOrigin[2], 1.0f);


So yes, they are both in Newtons. I checked my math up and down... it comes down to just that call, NewtonBodyAddForce(), and the value I pass is valid. The body is not moving. I scale up the value - the body starts to move. As a reference, the values I am using are true rocket thrust values:

Rocket engine: 1860000 N, and there are 4 engines, for a total of 7440000 N
Rocket mass: 84000kg, which would be 84000x9.80655 = 823750.2 N. Those are the values you can see in the debug printout. There is almost 10x as much thrust force to lift the body and it is not moving. Then, when I scale it up by another 10x (which makes it 100x force), it moves.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby misho » Tue Mar 27, 2018 2:26 pm

Any other thoughts on this?

I have "eyeballed" the point where the rocket body starts to "levitate", so to say, or, where gravity equals to the thrust, and derived a scale factor for the thrust. For the time being, I inserted this "fudge" scale factor with which the rocket behaves as expected:

dVector totalForce = gravityForce + thrustForce.Scale(9.03186);

I should note that I am on a year-old build (April 2017). I'm going to upgrade to the latest Newton version, but first, tonight or tomorrow I will create a similar test case in the tutorial sandbox and see what kind of behavior I'm getting there.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby Julio Jerez » Tue Mar 27, 2018 3:37 pm

well the function that collect the force and torque onthr body is this

file: ../newton-dynamics\sdk\dgPhysics\dgBroadPhase.cpp
../newton-dynamics\sdk\dgPhysics\dgDynamicBody.cpp

you can se a breakpoint on like 181 and see if the variables
m_externalForce += m_impulseForce;
m_externalTorque += m_impulseTorque;

have the value they are support to have.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Forces on a body enigma

Postby misho » Thu Mar 29, 2018 5:27 pm

Thanks! Still plugging away at this. I've set up a sandbox tutorial scenario similar to mine, and everything works the way it should (which is great!)... so obviously, I have a problem somewhere in my code.

Investigating further, I was wondering if the following members of DemoEntity class are used for visuals, or are they used in the physics engine? (i.e. do I need them? since I'm using different visual setup):

Code: Select all
   mutable dMatrix m_matrix;   // interpolated matrix
   dVector m_curPosition;      // position one physics simulation step in the future
   dVector m_nextPosition;      // position at the current physics simulation step
   dQuaternion m_curRotation;     // rotation one physics simulation step in the future 
   dQuaternion m_nextRotation;   // rotation at the current physics simulation step


I have a similar structure of my objects and I was wondering if these were necessary...
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby Julio Jerez » Thu Mar 29, 2018 6:15 pm

no you do no need those thing in your engine, they are just for the demo, and for people to get an idea how to go about using the engine.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Forces on a body enigma

Postby misho » Sun Apr 01, 2018 2:47 pm

Just to re-cap, let me state the problem I have once again: I am seeing odd behaviour on bodies with forces applied to them, namely, a thrust force greater than the weight of the body does not move it. I have to scale the force up to about 9x to observe the movement. My scene is far, far away form the coordinate origin (6370000 meters away).

Ok - so I have since made a demoSandbox analogue of my case, and the sandbox demo behaves properly. However, one big difference is, the demo is located at the "center of the universe" (origin of the coordinate system), and my case is at around 6370 km away from it (surface of the Earth, so Earth radius). Can that have anything to do with any odd behaviour I'm seeing?

Next thing I did was, I made a sandbox demo scene which was 6370 km away, and I observed "choppy" updates (test object would move in steps) but I subscribe that to low precision - I have to re-compile the libraries with double precision, which is a challenge in itself.

Finally, I made a test setup in my own environment - I've set up my objects at the Earth centre. It is kind of difficult to observe the behaviour because the visual system is not letting me go below ground, but I can observe the numerical readouts of the Newton bodies. And, they appear to be correct!

So, this makes me conclude that, the physics engine breaks down at large distances from the center of the coordinate system. Does that make sense?

I have to note that, in my project, I am running with double precision - I managed to get a double precision build of a few core libraries, only the ones I am using.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby Julio Jerez » Mon Apr 02, 2018 11:50 am

It should not be that difficult.
There nothing in the engine that requires single presicion arithmetic, in fact the engine is one that double prediction make far more accurate an stable.

You said you code a repro in the sandbox, if you send it to me I can check it out a fix it so that this problem is solved once for all. It seems we are going in circles seem the same problem over and over in different way.

Can you send me the repro in the sandbox?
or another solution is you send me a running executable that links to the newton all.
The problem you are describing are trivial to fix
On less I am midsing a big point that is in fact a bug in the engine that I am not awered.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Forces on a body enigma

Postby misho » Mon Apr 02, 2018 12:20 pm

Thanks Julio!

Just a heads up, I am working on a year-old build, and was just about to upgrade to the newest. I can still do that if you prefer. But to confirm, yes, I have determined that placing objects very far causes physics to break down. I have a very simple case of a static collision object (Launch Pad) and a normal, dynamic collision object (rocket).

When they are placed at the centre of the coordinate system (Earth's centre), everything is fine, I apply thrust force on the rocket and it slowly raises it.

Then, I change one single variable: an offset for the whole scene, in y (up) direction. This value offsets the object's positions 6370 km in the up-direction, basically places it on the North Pole. The thrust on the rocket is not moving it, and after a short while the rocket starts to tumble away for no apparent reason.

I am using _NEWTON_USE_DOUBLE on all of the libraries and the demo sandbox.

Yes, I can definitely send you the repro - I'm attaching the running executables for both cases. I can also send you a very simple code for the scene setup, and a small code snippet for PhysicsApplyGravityForce() callback that enables spacebar-activated thrust force.

(I'm uploading attachment using this forum's options, but it doesn't show me anywhere that it attached anything... let me know if you didn't get the attachment...)
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby misho » Mon Apr 02, 2018 12:34 pm

Also, here is the scene setup code: switch between two values of vertOffset to move scene from near to far...

Code: Select all
void HeavyStacking(DemoEntityManager* const scene)
{
   // set DEMO_GRAVITY constant to turn on off gravity, currently set to #define DEMO_GRAVITY  -9.80665f

   scene->CreateSkyBox();

   NewtonWorld* const world = scene->GetNewton();
   dMatrix offset(dGetIdentityMatrix());
   //dFloat vertOffset = 6378135.0f;
   dFloat vertOffset = 0.0f;
   dVector damp(0.0f);

   // Make Launch platform as a solid static box...
   NewtonCollision* const LP0 = NewtonCreateBox(world, 20.0f, 10.0f, 20.0f, 0, &offset[0][0]);
   NewtonCollision* const LaunchPlatformShape = NewtonCreateSceneCollision(world, 0);
   NewtonSceneCollisionBeginAddRemove(LaunchPlatformShape);
   NewtonSceneCollisionAddSubCollision(LaunchPlatformShape, LP0);
   NewtonSceneCollisionEndAddRemove(LaunchPlatformShape);
   dMatrix matrixLP(0.0f, 0.0f, 0.0f, dVector(0.0f, vertOffset - 5.0f, 0.0f));
   dFloat LPMass = 979452.0f;  // irrelevant, this is a static object, here for conformity
   DemoMesh* const geometryLP = new DemoMesh("Launch Platform", LaunchPlatformShape, "metal_30.tga", "metal_30.tga", "metal_30.tga");
   NewtonBody* const LPBody = CreateSimpleSolid(scene, geometryLP, LPMass, matrixLP, LaunchPlatformShape, 0);
   //NewtonBodySetMassProperties(coreStageBody, coreStageMass, CoreStageShape);
   NewtonBodySetLinearDamping(LPBody, 0.0f);
   NewtonBodySetAngularDamping(LPBody, &damp[0]);
   geometryLP->Release();
   NewtonDestroyCollision(LaunchPlatformShape);
   NewtonDestroyCollision(LP0);

   // Make Core Stage
   NewtonCollision* const CS0 = NewtonCreateCylinder(world, 4.25f, 4.25f, 59.8f, 1, &offset[0][0]);
   NewtonCollision* const CoreStageShape = NewtonCreateCompoundCollision(world, 0);
   NewtonCompoundCollisionBeginAddRemove(CoreStageShape);
   NewtonCompoundCollisionAddSubCollision(CoreStageShape, CS0);
   NewtonCompoundCollisionEndAddRemove(CoreStageShape);
   dMatrix matrixCS(0.0f, 0.0f, 1.5708f, dVector(0.0f, vertOffset + 30.1f, 0.0f));
   dFloat coreStageMass = 979452.0f;
   DemoMesh* const geometryCoreStage = new DemoMesh("CoreStage", CoreStageShape, "metal_30.tga", "metal_30.tga", "metal_30.tga");
   NewtonBody* const coreStageBody = CreateSimpleSolid(scene, geometryCoreStage, coreStageMass, matrixCS, CoreStageShape, 0);
   //NewtonBodySetMassProperties(coreStageBody, coreStageMass, CoreStageShape);
   NewtonBodySetLinearDamping(coreStageBody, 0.0f);
   NewtonBodySetAngularDamping(coreStageBody, &damp[0]);
   geometryCoreStage->Release();
   NewtonDestroyCollision(CoreStageShape);
   NewtonDestroyCollision(CS0);

   // place camera into position and rotate towards objects
   dMatrix camMatrix(dGetIdentityMatrix());
   dQuaternion dqRotY(dVector(0.0f, 1.0f, 0.0f), 90.0f * 3.141592f / 180.0f);
   dQuaternion rot(camMatrix);
   dVector origin(0.0, vertOffset + 5.0f, 120.0f, 0.0f);
   scene->SetCameraMatrix(rot*dqRotY, origin);
}



Also - here is the code to enable spacebar-activated thrust force vector:

Code: Select all
// add force and torque to rigid body
void  PhysicsApplyGravityForce (const NewtonBody* body, dFloat timestep, int threadIndex)
{
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   dFloat mass;

   // Misho Added
   NewtonWorld* const world = NewtonBodyGetWorld(body);
   DemoEntityManager* const scene = (DemoEntityManager*)NewtonWorldGetUserData(world);
   NewtonDemos* const mainWindow = scene->GetRootWindow();

   bool bThrust = false;

   if (mainWindow->GetKeyState(' ')) {
      bThrust = true;
   }

   NewtonBodyGetMass (body, &mass, &Ixx, &Iyy, &Izz);

   // mass in "HeavyStacking" is set to 979452 kg
   dVector force (dVector (0.0f, 1.0f, 0.0f).Scale (mass * DEMO_GRAVITY)); // Force calculated here is 9.6 million N

   // Misho added
   dVector thrust(dVector(0.0f, 1.0f, 0.0f).Scale(0.0));
   if(bThrust)
      thrust = dVector(0.0f, 1.0f, 0.0f).Scale(10000000.0); // ten million newtons, so the bodies will "rise" slowly...
   else
      thrust = dVector(0.0f, 1.0f, 0.0f).Scale(0.0); // the thrust cuts out

   dVector resultant = thrust + force;
   NewtonBodySetForce (body, &resultant.m_x);
}


Like I noted, you can compile 2 versions of this scene, one with

dFloat vertOffset = 6378135.0f;

and the other with

dFloat vertOffset = 0.0f;

To see the difference between near and far behavior. Thanks again for looking into this!
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby Julio Jerez » Mon Apr 02, 2018 1:22 pm

misho wrote:Thanks Julio!
Just a heads up, I am working on a year-old build, and was just about to upgrade to the newest. I can still do that if you prefer.

well that problem #1, you should be a least ion the last stable version, the sandbox does even use the say gui library anymore, you nee to update to the lastest that is in GitHub first.


[quote="misho"]
But to confirm, yes, I have determined that placing objects very far causes physics to break down. I have a very simple case of a static collision object (Launch Pad) and a normal, dynamic collision object (rocket).

When they are placed at the centre of the coordinate system (Earth's centre), everything is fine, I apply thrust force on the rocket and it slowly raises it.

Then, I change one single variable: an offset for the whole scene, in y (up) direction. This value offsets the object's positions 6370 km in the up-direction, basically places it on the North Pole./quote]

that is can not be happening with double precision, the engine does not any kind of quantization, broad phase does check using dgFloat32 which in double precision resolver to double.
a double has a 17 decimal places dynamic range so in your case if the unit is 1 meter, and let us say need to be resolved is say 0.001 of a millimeter, that 6 digits that nee to be subtracted form 17, so we get 10^11 meter, which is 100 million kilometer.

you should be able to result anything and 0.001 of a millimeter resolution anythnong with a radius for the earth to the sun.
My guess is that you are build in single precision and you think is double.
the fact that your values are printed like this dFloat vertOffset = 6378135.0f;
is telling me that you are running a single precision build.

but anyway please sync to latest and resulve the compiler error. then let us see what is wrong.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Forces on a body enigma

Postby misho » Mon Apr 02, 2018 1:46 pm

OK Will do! thank you!
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby misho » Mon Apr 02, 2018 8:53 pm

Ok, I have the latest Newton version, downloaded, installed, demoSandbox compile without issues.

However - when I run demoSandbox, all of the demos are frozen. The statistics box is active and shows stats, but all of the objects are frozen in the air, and I can't interact with them - any click on them is not registered, however clicking on anywhere other than an object does move the camera.

Is there a key press that "starts" the demo?
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Forces on a body enigma

Postby Julio Jerez » Mon Apr 02, 2018 8:57 pm

I will check tonight, maybe there is a work in progress. But that should be easy to get going.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 13 guests