Suspending Newton and proper scene setup sequence

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Suspending Newton and proper scene setup sequence

Postby misho » Sat Jun 10, 2017 2:57 am

I'm having problems re-creating an orbital structure, made up of several bodies, when I load them from a file. Here is my set-up:

My bodies are orbiting Earth and moving at great speeds (above 7000 m/s). As the simulation runs, I insert new bodies and I attach (link) them to the existing structure. Then, I save this set up to a text file (the save process is instantaneous). The objects get saved with their positional and attitude data, and their linkage info.

Then, I want to load the file I just saved. The problem is, my game engine has to load the saved visual objects asynchronously (requests them from a server and waits for the reply). While this is happening, I have to suspend calls NewtonUpdate(), otherwise, the few frames the server waits for the requested object to load, the previously loaded objects "run away" ahead and appear at the wrong positions.

As the objects are loaded, I link them, but for that I found (by experimenting) that I need to call NewtonUpdate(0.0) (with zero time step) for the link to function. Once all the objects are loaded, I activate calls to NewtonUpdate() and the sim runs.

Now, I'm sure this is not the proper way of doing it, because I am getting inconsistent results. Sometimes the whole linked structure loads fine, sometimes it has a rotation, and sometimes it goes into a wild spin with all the objects being thrown away form each other, and sometimes the objects are not in the positions they were when I saved them.

My questions are:
  • What's the proper way (sequence of calls) for Newton engine when I need to discard existing objects from a scene and load now objects into the scene? (Keeping in mind asynchronous nature of loader of my visual objects).
  • How do I properly suspend Newton engine while my objects load?
  • Do I need to destroy and re-create the world?
  • Do I need to use NewtonInvalidateCache (m_world); ? (I tried but not much changed, not sure where exactly it goes)

Thanks in advance.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Suspending Newton and proper scene setup sequence

Postby Shaderman » Sat Jun 10, 2017 12:23 pm

Would this probably work?

- Create a Newton world, but don't NewtonUpdate it
- Add all objects to your engine and the Newton world (wait until all objects were received asynchronously)
- Start NewtonUpdate-ing the world
Shaderman
 
Posts: 66
Joined: Tue Mar 08, 2016 2:51 am

Re: Suspending Newton and proper scene setup sequence

Postby misho » Sat Jun 10, 2017 12:53 pm

Shaderman wrote:Would this probably work?

- Create a Newton world, but don't NewtonUpdate it
- Add all objects to your engine and the Newton world (wait until all objects were received asynchronously)
- Start NewtonUpdate-ing the world


Thanks - that's essentially what I'm doing. I'm "freezing" the NewtonUpdate() by passing "0" for the timestep, so that the whole sim suspends (I'm using the same principle for pausing the sim, and it works)

I think my "problem" is similar to what's happening in the demo sandbox - it seems that the newton world is destroyed and re-created every time a new scene is invoked. This is happening in

Code: Select all
void DemoEntityManager::Cleanup ()
   // is we are run asynchronous we need make sure no update in on flight.
   if (m_world) {
      NewtonWaitForUpdateToFinish (m_world);
   }

   // destroy all remaining visual objects
   while (dList<DemoEntity*>::GetFirst()) {
      RemoveEntity (dList<DemoEntity*>::GetFirst());
   }

   m_sky = NULL;

   // destroy the Newton world
   if (m_world) {
      // get serialization call back before destroying the world
      NewtonDestroy (m_world);
      m_world = NULL;
   }
   // check that there are no memory leak on exit
   dAssert (NewtonGetMemoryUsed () == 0);

   m_world = NewtonCreate();

   ...


I'm just wondering if this is the right approach for me, since I regularly add/remove objects in sim real-time (using GUI I put in), so I don't understand why loading and adding objects from a file would be any different...
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Suspending Newton and proper scene setup sequence

Postby Julio Jerez » Tue Jun 13, 2017 10:25 am

can you use serialization to memory, and pass the use callback to save use data, then you can recreate the scene on another newton world, or and getting this wrong?

once you have a memory image of your world you can use to recreate independent of that rest of the other world, you can even recreated on an existing world.

the only requirement is that you do the call from outside a newton update.

The serialization callback have function pointer call back for saving the data the storage, and also for saving it use data that can be associate with bodies an joints.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Suspending Newton and proper scene setup sequence

Postby misho » Tue Jun 13, 2017 12:39 pm

Julio Jerez wrote:the only requirement is that you do the call from outside a newton update.


Thanks Julio...I think that's the key. I basically need to do something similar to what demosSandbox is doing when switching any one of the 40 or so scenes - the world is completely cleaned and destroyed, then re-created. The only difference is, demosSandbox scene is hard-coded, and mine is loaded through a text script.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Suspending Newton and proper scene setup sequence

Postby Julio Jerez » Tue Jun 13, 2017 2:22 pm

I am no sure how loading form a text file scrip play into thes problem you descrived.
when you say this.
My bodies are orbiting Earth and moving at great speeds (above 7000 m/s). As the simulation runs, I insert new bodies and I attach (link) them to the existing structure. Then, I save this set up to a text file (the save process is instantaneous). The objects get saved with their positional and attitude data, and their linkage info.

Then, I want to load the file I just saved ....


my interpretation is that you have a world, that is already setup, and you want to save it an recovery it some time later.
and what I say is that you do no need to save to a file, you can save to a memory buffer, using the serialization routines.
they way there are in 3.14 will no world because I made then to save to a file, but that was as mistake that I am fixing now.

they will serialize to a use specified function pointer and user data handle.
you can make the handle a vector, and the function simple append the data to the end.

at the end you will get a stream that represent that world, and you can added to another world or make a new world initialized to that data.
I will committee that modification tonight with a demos.

this is no for saving and loading, although it can be use for that, with in a newton version, is for real time saving a loading like you describe.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Suspending Newton and proper scene setup sequence

Postby misho » Tue Jun 13, 2017 4:38 pm

Julio Jerez wrote:my interpretation is that you have a world, that is already setup, and you want to save it an recovery it some time later.
and what I say is that you do no need to save to a file, you can save to a memory buffer, using the serialization routines.


Ah, ok, there is the misunderstanding, I expressed myself incorrectly. What I need to do is being able to save the current file, and load it (OR some other file), later (NOT for the purpose of recovery, but more of a "save game" situation).
The files I save to are already established, specifically formatted "situation" text files used by the visual engine (a flight simulator). Here is what it looks like (2 objects in orbit, attached)

Code: Select all
[SpaceSim.0]
sim=      Orion MPCV Block I
GUID=      {8323009F-238E-4ED4-B54F-7AC10701ED72}
Attachpoint.0=   {AFDAC45D-08DE-470A-BC7A-D9D09B0DB52A}, 0
attitude=   0.283796, 0.305183, 0.729938, -0.541769
position=   0.000000, -2637771.711662, 3228073.850438, 5617910.972863
velocity=   0.000000, 3839.115410, 6250.630290, -1785.578221
omega=      0.000000, -0.000376, 0.000509, -0.000508
params=      15

[SpaceSim.1]
sim=      Node 1 Unity Module
GUID=      {AFDAC45D-08DE-470A-BC7A-D9D09B0DB52A}
Attachpoint.0=   {8323009F-238E-4ED4-B54F-7AC10701ED72}, 0
attitude=   0.541769, 0.729938, -0.305183, 0.283796
position=   0.000000, -2637776.872243, 3228072.296857, 5617915.205971
velocity=   0.000000, 3839.116720, 6250.634329, -1785.575143
omega=      0.000000, -0.000376, 0.000509, -0.000508
params=      0


When I read in this file, I load the "sim" objects (which have their own set of properties, namely, collision primitives), build the newton body out of them, position them through the attitude/position/velocity parameters and link them according to the "Attachpoint" parameters.

The users of the flight simulator are already familiar with the format and syntax of this file. My space simulator is an add-on for this flight simulator, and I insert these entries into it when I save my space-specific situation. So, serialization with "bin" files is an option, but I'd like to expose the details of the saved spaceflight-related objects to the users just like they are exposed for atmospheric flight portion. The open-source nature of this flight simulator is actually what made it popular, and I'd like to keep it that way. There can be many such "situation" files, with different spacecraft linked to other objects either in orbit, or on the launch pad, or any combination thereof. User can at any time load any of these files.

Looking at how the demosSandbox is set up, it is close to what I need - every time a new scene is loaded, the whole world is destroyed and re-set, and the new scene is built. What differs is, the demo scenes are hard coded, and I build my scenes using parameters read through text files.

My question was relating to the proper sequence of Newton calls when destroying one scene and re-loading the next. Up until now, I was only suspending calls to NewtonUpdate(), but from the looks of the demo, I need to destroy and re-create the world.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am

Re: Suspending Newton and proper scene setup sequence

Postby Sweenie » Wed Jun 14, 2017 12:44 am

Did you try NewtonInvalidateCache directly after you loaded the objects into the scene?
That is...
* Destroy objects
* Restore them from file
* NewtonInvalidateCache
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Suspending Newton and proper scene setup sequence

Postby misho » Wed Jun 14, 2017 12:49 am

Sweenie wrote:Did you try NewtonInvalidateCache directly after you loaded the objects into the scene?
That is...
* Destroy objects
* Restore them from file
* NewtonInvalidateCache


Hey, thanks, yes, I'm doing this right now - it's a little more involved because my object loading from file is asynchronous so I have to split the sequence.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 673
Joined: Tue May 04, 2010 10:13 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 16 guests