Timing and FPS question

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Timing and FPS question

Postby misho » Thu Jul 05, 2018 11:22 am

Julio Jerez wrote:Can you set a break point a step through it,
That function should be not more than a dozen line of code.
Anyway I am write a simple frame capture profiler so that we can determine where this inconsistencies com from.
I am almost done with the instrumented part, it will be few day for the client.
I will use Windows form for simplicity, so it will be a windows only tool.
Does your project supports windows?


Yes, it is an add-on for Microsoft FSX, a windows-based simulator.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am

Re: Timing and FPS question

Postby Julio Jerez » Thu Jul 05, 2018 11:28 am

Ok, let us see this logically.
1-how many rigid bodies are in your scene?
2-how many rigid bodies in the scene do that call?
3-are you using a debug build.

If the answer to any of those question is not, the there is not reason why that function call is slow.
The function only set the values on the body mass matrix.
Julio Jerez
Moderator
Moderator
 
Posts: 11039
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Timing and FPS question

Postby misho » Thu Jul 05, 2018 11:33 am

Julio Jerez wrote:Ok, let us see this logically.
1-how many rigid bodies are in your scene?
2-how many rigid bodies in the scene do that?
3-are you using a debug build.

If the answer to any of those question is not, the there is not reason why that function call is slow.
The function only set the values on the body mass matrix.


No problem:

1: 13
2: 3
3: No - it is a release_double build.

I only work with release_double. Size of the newton.lib is 860K - check if that's the size of the release version. I compiled SDK using the solution settings that came with it, I didn't change anything. The build is from early April this year, but I don't think you changed anything in that area...
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am

Re: Timing and FPS question

Postby Julio Jerez » Thu Jul 05, 2018 11:56 am

The number you posted do not explain the slow down even if you where running a debug build.
With those figures a newton update should be less that 0.1 ms even in debug mode.
In fact it can be less if you set thread emulation, there is a fix over head for task switching because the update runs in its own thread blocking the parent.

Even that can be set to run asynchronous and the physics update should be 100% free.

To get asyncronus update, you should make a listener, and move you changes and all related physics manipulations in the pre or post listener callback.

But first we most determine why the slow down happens in the first place.
Julio Jerez
Moderator
Moderator
 
Posts: 11039
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Timing and FPS question

Postby Julio Jerez » Thu Jul 05, 2018 12:01 pm

Is this simulator
free?
Can I download it?
If so can I test your plug in?
Julio Jerez
Moderator
Moderator
 
Posts: 11039
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Timing and FPS question

Postby misho » Thu Jul 05, 2018 12:11 pm

Julio Jerez wrote:Is this simulator
free?
Can I download it?
If so can I test your plug in?


Not free, but cheap: $12 on Steam:

https://store.steampowered.com/app/3141 ... m_Edition/

Yes, you can test my plugin but I don't have the installer yet, and the installation is quite complicated and tricky, a lot of config files to modify.
I do think this is something I'm doing wrong, although I can't see it yet, so let me poke around my code for a bit. I am sure your code is as you say, simple and it should be running without much performance degradation...
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am

Re: Timing and FPS question

Postby JoeJ » Thu Jul 05, 2018 12:55 pm

misho wrote:1: 13
2: 3
3: No - it is a release_double build.


Yeah, that's really nothing. I start to guess you accidently call Newton update 10.000 per frame or something. You should increase a static counter in force torque callback to check out how often it is called.

misho wrote:I don't understand your comment about "fuel to be part of a rigid body" - it is part of a rigid body, but also it is evenly and symmetrically distributed so I don't see a need for inertia matrix recalculation.


Just to be complete, what i mean is, think of a full glass of wine resting in outer space. If we rotate it along it's long axis, the wine would not rotate with the glass, so we could say it is not part of the rigid body we use to approximate the simulation.
But if we rotate it around a perpendicular direction of this axis, the wine must rotate with the glass, so its mass contributes to the inertia of the rigid body model. (It's easier to rotate a empty glass than a full glass in this case.)

Edit: I think to simulate this pretty accurate, you could use rigid body for the fuel and a sliding joint so it can move up and down inside the tank. Additionally you would need some soft joint limits to model fluid distribution and also change it's inertia accordingly. (But nobody would notice the work you spend on this :) )
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Timing and FPS question

Postby Julio Jerez » Thu Jul 05, 2018 1:35 pm

Joe what you say is true, but only partially.
if you are talking about mass distribution the is true, but by definition the moment of inertia of a body is given by
I = sum (mass(I) * X(I) ^ 2)
therefore is the mass change then the total inertia should change.

Michio how are you calculation the inertia? I remember we talk about this in the pass.
are you using a compound are a helper to calculate the inertia distribution?

if this is the case and you compound is made of dense convex shapes them the slow down in on function of some equivalent. NewtonConvexCollisionCalculateInertialMatrix

the reason is the convex for a convex polygonal shape the only way I know to calculable the inertia is by using the divergence and the green theorem. and that iterates over the entire polygonal mesh of the collision shape so it can by quiet expensive for a dense shape.
Is that what you are doing?

tell me how are calculating the inertia after you change the mass?
Julio Jerez
Moderator
Moderator
 
Posts: 11039
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Timing and FPS question

Postby misho » Fri Jul 06, 2018 8:10 am

Julio Jerez wrote:Michio how are you calculation the inertia? I remember we talk about this in the pass.
are you using a compound are a helper to calculate the inertia distribution?

if this is the case and you compound is made of dense convex shapes them the slow down in on function of some equivalent. NewtonConvexCollisionCalculateInertialMatrix

the reason is the convex for a convex polygonal shape the only way I know to calculable the inertia is by using the divergence and the green theorem. and that iterates over the entire polygonal mesh of the collision shape so it can by quiet expensive for a dense shape.
Is that what you are doing?

tell me how are calculating the inertia after you change the mass?


Morning (?) guys!

To calculate inertia, I first load up the compound collisions (cylinders, cones, etc) and create a body using the compound collision and STARTING mass. For the main orange rocket body, for example, I have 9 cylinder compound collision... is that what you consider dense?

Code: Select all
   // create a body with given compound collision and mass
   newEntity->nBody = CreateRigidBody (world, newEntity, objectCollision, newEntity->GetGrossMass()); // <- mass parameter. read this from the object data


When that's done, I get inertia vector using:
Code: Select all
   NewtonBodyGetMass(newEntity->nBody, &dummyMass, &dvInertia.m_x, &dvInertia.m_y, &dvInertia.m_z);

I save dvInertia vector and I NEVER re-calculate it again (because I assume symmetry, what JoeJ mentioned). I re-apply new mass in a visual frame loop, with the NewtonBodySetMassMatrix method, new mass, and this saved inertia... Yes we talked about this before and this is how I understood this needs to be done. Is that correct?

I should mention that while investigating this, I shifted some code around and now I'm getting crashes instead of performance degradation. They are definitely because of NewtonBodySetMassMatrix (when I comment it out, everything works, except of course the mass isn't changing). So:

with NewtonBodySetMassMatrix in visual loop, all is ok
with NewtonBodySetMassMatrix in physics loop, crash
with NewtonBodySetMassMatrix in physics loop commented out, fine.

Crashes are always at the same point of execution, but not in the NewtonBodySetMassMatrix but a few lines before or after... I checked all the NewtonBodySetMassMatrix parameters and everything I pass in is valid...

the only reason I can think of is that, when in physics loop, NewtonBodySetMassMatrix is called earlier than when in visual frame loop. Perhaps some objects and links aren't created yet, and that presents a problem?
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am

Re: Timing and FPS question

Postby Julio Jerez » Fri Jul 06, 2018 9:00 am

I mean by dense a convex hull that is made of lot of polygons.

you say call the function cause a crash, and thta's a sign some else is very wrong, that funstion soudl no cause a crash anywhere.

I loo hwo the inerta of a cylynder is calculated, and it turnput is no using the close form formula, instead is polygonizin the shape and doing the calculation.

the cylder is converted to a mesh of 24 segments, so that
24 * 2 for the side and 24 * 2 for the caps aproximatlly 100 triangles.
and you say the compoun is 9 cylinders
taht mean in total for 600 to 900 trinagles, still nothing that justify that kind of slow down.

can yopu run in debug and set a brak point in function

dgFloat32 dgCollisionCylinder::CalculateMassProperties (const dgMatrix& offset, dgVector& inertia, dgVector& crossInertia, dgVector& centerOfMass) const

to see if it called when you update the inertia?
Julio Jerez
Moderator
Moderator
 
Posts: 11039
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Timing and FPS question

Postby JoeJ » Fri Jul 06, 2018 9:33 am

I save dvInertia vector and I NEVER re-calculate it again (because I assume symmetry, what JoeJ mentioned). I re-apply new mass in a visual frame loop, with the NewtonBodySetMassMatrix method, new mass, and this saved inertia... Yes we talked about this before and this is how I understood this needs to be done. Is that correct?


What i mean has nothing to do with symmetry of shapes. I mean fuel and ship can been seen as two individual bodies, and depending on how you rotate the ship, the fuels inertia contributes more or less to the turning resistance of the ship containing the fuel.

For a good enough approximization you could precalculate inertia once with the fuel, and once without it. Then when adjusting the mass at runtime, you could lerp the inertia values accordingly.
User avatar
JoeJ
 
Posts: 1147
Joined: Tue Dec 21, 2010 6:18 pm

Re: Timing and FPS question

Postby Julio Jerez » Fri Jul 06, 2018 11:22 am

JoeJ wrote:For a good enough approximization you could precalculate inertia once with the fuel, and once without it. Then when adjusting the mass at runtime, you could lerp the inertia values accordingly.

That is a good optimization idea, however he claim that just setting the mass and the inertia is what causes the slowdown and now even a crash.

Sence he would has to call the function to override the inertia matrix, it would not make any difference.

Michio you say the function crashes, when call from a newton update, this is telling me that you are probably creation and destroying collision shapes, to recreate the compound you are use to calculate the inertia.

Is that what you are doing?

Basically what you should do is quite simple.
Make a compound,
Keep it with the rocket data structure
In the compound, the shape the represent the ful tank
You just use the scale functionality to set the new volume of the fuel tank.

The analogy is that the feel tank is in fact blotted, or maybe a solid fuell so the liquid does not move.
Julio Jerez
Moderator
Moderator
 
Posts: 11039
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Timing and FPS question

Postby misho » Sat Jul 07, 2018 10:08 am

Julio Jerez wrote:Michio you say the function crashes, when call from a newton update, this is telling me that you are probably creation and destroying collision shapes, to recreate the compound you are use to calculate the inertia.

Is that what you are doing?

Basically what you should do is quite simple.
Make a compound,
Keep it with the rocket data structure
In the compound, the shape the represent the ful tank
You just use the scale functionality to set the new volume of the fuel tank.

The analogy is that the feel tank is in fact blotted, or maybe a solid fuell so the liquid does not move.


Hi Julio,

No - I am not creating and destroying collision shapes. I do exactly what you listed, except I don't have a separate tank, I treat the whole compound as a "tank".

So: I create the body using the compound collision just once, when the body is created (I have a rocket definition text file from which I load). I load all collision shapes into the compound collision, create body and assign mass to it, and then obtain inertia vector and save that as well. I obtain Inertia vector using NewtonBodyGetMass(), just after I create body. The only use for the inertia vector is so that I can pass it to NewtonBodySetMassMatrix call, and since in my case it does not change (I assume the whole compound to be a fuel tank), I was questioning the need for passing inertia vector (which is always constant) in the NewtonBodySetMassMatrix method, and wondering if there is something simpler, just to set mass on the WHOLE body - that's all.

So, again, I don't do ANYTHING to the inertia vector - it stays the same. I needed it only to pass something to the NewtonBodySetMassMatrix call. The only variable parameter I pass to the NewtonBodySetMassMatrix call is the new (decreased) mass.

I posted the code of what I was doing a few posts ago - let me know if you want to see it again.

I do like your idea of scaling the compound shape of the fuel tank, but I am not there yet. I want to work out the case where the whole rocket body is treated as a fuel tank, and there is no fuel sloshing or weight shifting
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am

Re: Timing and FPS question

Postby zak » Sat Jul 07, 2018 12:00 pm

It seems like a mistake in your code in dealing with multithreading in Newton callbacks.
To verify try compiling Newton with DG_USE_THREAD_EMULATION defined.
zak
 
Posts: 58
Joined: Mon Dec 06, 2004 9:30 am

Re: Timing and FPS question

Postby Julio Jerez » Sat Jul 07, 2018 12:14 pm

you mean this
Code: Select all
void   TBEntity::AdjustEntityMass(void)
{
   // set new entity mass:
   // mass depeletion due to propellant expenditure
   double fCurrentMass = GetPropellantMass() + GetEmptyMass();

   // set the body mass matrix
   if (fCurrentMass)
   {
      dFloat massScale = fCurrentMass / GetGrossMass();
      inertia.Scale(massScale);
      NewtonBodySetMassMatrix(nBody, fCurrentMass, inertia.m_x, inertia.m_y, inertia.m_z);
   }
}


is that is what you are doing, ther mous be something else wrong.

is you make a debug build and step over the function, you will see that from begin to end is no more than a few dozen lines of code, including every thing, certainly less than 100.
Julio Jerez
Moderator
Moderator
 
Posts: 11039
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 8 guests