Newton Plugin for Unity 3D

Share with us how are you using the powerrrr of the force

Moderator: Alain

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Fri Apr 29, 2016 3:19 pm

I do not think it is a problem if the list is too long, people can always select by parts.
plus I assume the widget will show a slider to scroll up an down.

also users can add by selecting bodies fro the graphics scene.

as for duplicates, that's why I mentioned a dictionary not a vector, in general dictionaries containers only allow unique entries. I assume c sharp will have an option for unique entries.

also further down we can get fancy and add filters to the dictionary, where people can filter and group but classes of objects types, and we add some group id to the body.
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Sweenie » Fri Apr 29, 2016 3:40 pm

I forgot to mention that my method can autoassign the world when you add a new Body component.

Code: Select all
        void Reset()
        {
            if (World == null)
            {
                NewtonWorld findWorld = FindObjectOfType<NewtonWorld>();
                if (findWorld != null)
                    World = findWorld;
            }
        }


Haha, well, I think we have plenty of options and Unity is flexible so we can try what works best.
You can also create custom inspectors so if Unity doesn't support Dictionaries by default in the Editor maybe we can add it in ourselves. :)
Sweenie
 
Posts: 481
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Kaos » Fri Apr 29, 2016 4:11 pm

Cons: It would be trickier to expose individual world settings for each world.


Well as we discussed before. I propose to extend the Editor for easy user setup of newtons worlds and settings.
http://docs.unity3d.com/Manual/editor-E ... ndows.html

And i Propose that a newton worldID 0 is always there and all rigidbody created are automatically on that worldID 0, if not specified over script or per hand.
That would be much easer for people who just want to use this without multiple worlds. Which could be the majority.
if i have to pick i would take sweenies solution. And thats because its more the unity workflow of doing things andtherefore will be more accepted.




It actually makes sense. You can't create a NewtonJoint without at child RigidBody.
It's only the parent body that is optional.


It sounds somewhat wrong to me when you speak of parent and child when talking about newton bodies.
I propose to stick with the unity definitions of GO are parents and children and Components are just that. And not that a component is a child of its GO. Because its just clearer to divide and its the way unity treats em.

Example from NewtonHinge.cs

Code: Select all
childBody = GetComponentInParent<NewtonBody>();

That is not a child its a component of the parent GO.
It would be a bit more appropriate to call that just a "NewtonBodyComponent"

BTW: GetComponentInParent<> is a bit strange and misleading named, because it could actually get the newton body from the GO the script is on and all parents above. So its not really defined what GO youre connecting in the end you only know its the first it finds. Thats why i didnt call that a "parentNewtonBodyComponent" above.

I believe that you know all this perfectly well and i dont want to anger you both. And its not that i dont see components as children of some sort too.

But i believe It makes the source and the discussion easier to follow and prevents missunderstandings.

What do you guys think?
Kaos
 
Posts: 38
Joined: Mon Apr 18, 2016 11:24 pm

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Fri Apr 29, 2016 7:47 pm

I agree Sweenei Unity is flexible enough that we can chose one method and then we can improve for there, we are not going to have a perfect plugin the fort time.

For now My gold the weekend is that was can have a least a world and the ability to navigate the edit and add solid with the basics collision primitives.


On the joint.
Kaos wrote:
It actually makes sense. You can't create a NewtonJoint without at child RigidBody.
It's only the parent body that is optional.

It sounds somewhat wrong to me when you speak of parent and child when talking about newton bodies.
I propose to stick with the unity definitions of GO are parents and children and Components are just that. And not that a component is a child of its GO. Because its just clearer to divide and its the way unity treats em.



This is because we nee both system. The Joint should a component that a no game object can inherited only once.

when we assign a Joint component to a RigidNody, The body will find the Parent and connect that other slot and the parent body. The give us automatic Skeletons with articulation that can be mapped o rag dolls.
The we make a GameObject and we call it some think Constraint, this object will have only one Joint Component with and two reference to two bodies. an these joint connect any two bodies the belong to the same world.

These are the weaker joints that can serve as kinematic look, for example a rag done with a tow side weapon, can no be done by the first method but can easy be done by these joints.

another example would be two vehicles an one serve as a trailer.
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun May 01, 2016 11:55 am

Ok I now made some changes.

first I excluded script I added file like: newton_wrap.cs and newton_wrap.cxx and *.dll.
those file are constantly been recreated fo rteh Newton.i script

I renamed c++ classes to use the prefix d, like dNewtonWorld intead of NetwonSDK
I also stat to add the dRigidBody

I added a Demo folder and added a post build script to the visual studio project so that I copy the dll to the ..\NewtonUnityPlugin\Demos\Assets\Plugins

this will allow for launching unity form Visual studio, load a test scene and it we can debug the plug in.

I wrote the very first Scene which only load the NetwonWorld into unity.
I creates and emprty GameObject and add a NetwonWorld and a components.
I test it and seem to work without problems.

I was able to start unity run the demo, step in the debugger in both the plugin and the low level NewtonSDK. so far it seems so good.

The Unity project add lot of generated stuff to a folder name library. and also lot of metadata.
I presume that data can be recreated from information in the projectSetting folder.

So guys when you have time see if this works for you as I described.

Now I will add the Collision and minimal wrapper stuff so that will can add a a secnun demo scene that put bodies in the scene.
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sun May 01, 2016 12:13 pm

Awesome, will check it out.

Regarding adding the Demo on GitHub.
That should be fine. Unity actually supports that, but I see you added .meta files to .gitignore.
Unity needs them to know how to rebuild(recompile) the assets.
The temp and library should be ignored, just like you did.

We also need to enable a setting in the Unity Project, "Visible meta files".

It's explained here...
http://docs.unity3d.com/Manual/External ... pport.html
Sweenie
 
Posts: 481
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun May 01, 2016 1:09 pm

Ok I remove the .meta filter and I re added the .meta file again.
there was a trick there because Unity just append the extension .meta to a file name
so the .dll end up been .dll.meta
so I have to explicitly add filter to NetwpnWrapper.dll and NewtonPlugin.dll
I think it should be fine now
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sun May 01, 2016 1:28 pm

We will need to make a decision how we want to handle parameter passing to and from the wrapper.dll

If we take the Body Constructor as example...

Code: Select all
public dNewtonBody(dNewtonWorld world, SWIGTYPE_p_dFloat mass, SWIGTYPE_p_dNewtonCollision collision, SWIGTYPE_p_dFloat matrix, dNewtonBody.dBodyType m_type, dNewtonBody parent)


The first argument[world] is the world, SWIG has successfully wrapped that object and will pass the underlying object swigCPtr(which basically is a void* to the world object)

The second argument[mass], here SWIG doesn't know what a dFloat is and has automatically wrapped dFloat as a class. That won't work. We will need to "teach" SWIG to remap dFloat to a normal float which we pass by value.

The third argument[collision] will work as the first as soon as SWIG has wrapped the dNewtonCollision class.

The fourth argument[matrix], here we need to decide what to do. Newton uses float* to pass matrices but SWIG doesn't handle float* by default. It will try to wrap float* as a class.

I know of three options we have here...

1. "Teach" SWIG to remap float* (or any ptr) to IntPtr(C# void*).
Then Marshall the values to and from IntPtr in C#

2. Let float* still be float* and use Unsafe Mode in c# to work directly with pointers.(This is the fastest method)

3. Remap to a structure known by C#, in this case Unitys Matrix4x4 would work since it is a 16 floats structure. Then pass it as ref Matrix4x4 matrix.
This works like nr 1, but C# will take care of the Marshalling since he knows the size of the structure.

The fifth argument[m_type], this is just an int, no problems here.

The sixth argument[parent], same as argument 1 & 3, no problems.

So, to sum it up... We need Swig to generate one of these C# functions.

Marshalling using IntPtr
Code: Select all
public dNewtonBody(dNewtonWorld world, float mass, dNewtonCollision collision, IntPtr matrix, dNewtonBody.dBodyType m_type, dNewtonBody parent)


Standard pointers(requires compiling plugin in Unsafe mode)
Code: Select all
public dNewtonBody(dNewtonWorld world, float mass, dNewtonCollision collision, float* matrix, dNewtonBody.dBodyType m_type, dNewtonBody parent)


Marshalling using known structure
Code: Select all
public dNewtonBody(dNewtonWorld world, float mass, dNewtonCollision collision, ref Matrix4x4 matrix, dNewtonBody.dBodyType m_type, dNewtonBody parent)
Sweenie
 
Posts: 481
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun May 01, 2016 1:55 pm

I want to be strict C scharp on the c chaps side, not unsafe business of any kind.

I know, It looks as is we are talking pass is other now, but
I am try to get to the vary basic framework that allow us to add a body with a box.

I added now the very basics objects. a base class for making collisions and I subclassed a Box.

now I believe we have what we need to make a NetwonBody.cs

Bu I am confused, because a collision shape be part of a body, not a component, I try to see hwo you set it up by I can figure it out.

I see the body having a filed of mass for example but no for a collision shape.
Maybe you can tell me how to go form here.

once we can make Boxes the rest is just build on top, mode shapes from a drop down list box and so on. But now I am stocked.

please sync so that you can see what I did.
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun May 01, 2016 2:17 pm

If you sync now you will see that the simple test demo now have a box, and the Box has a filed that connect links that box to the world.

The word will create the body form the start function and it is form there that pass all the initialization parameters.

The next step is adding anther field to eth body that allow for assign the collision shape, and all other properties .

but first I want to see if I can figure out how to add a container to the world so that the both Body and world a re strongly connected as C shape expect the to be.

I think once we have those two steps, we are ready to move faster.
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sun May 01, 2016 2:41 pm

Bu I am confused, because a collision shape be part of a body, not a component, I try to see hwo you set it up by I can figure it out.

I see the body having a filed of mass for example but no for a collision shape.
Maybe you can tell me how to go form here.


Well, I tried to recreate how Unity handles collision shapes.

First you have the Game Object with the Rigid Body Component (NewtonBody).
When the NewtonBody is created it will look for NewtonCollision components, first in it's own Game Object and also in it's children Game Objects.

I use the Unity function FindComponentsInChildren<NewtonCollision>()
This will return an array with all NewtonCollision components it can find.
It will also search in it's own Game Object so that you can add colliders to the same Game Object the Rigid Body is in.

Each collider use the local Transform of the GameObject it's a component of as the OffsetMatrix.
That way I can use the Editor Transform Gizmo to move and rotate my colliders offsetmatrix.

However, if the Collider is part of the same GameObject as the RigidBody you can't transform it with
the gizmo, the offsetMatrix will always be Identity. Unity solved this by also adding a Offset Vector field. But you can't rotate it then, only translate.

Try the standard Unity Colliders, you will see what I mean.

Now , after I call FindComponentsInChildren<NewtonCollision>() i will get an array of all colliders found in the tree.

If the result is 0 (no colliders found), I create a NullCollider.
If the result is 1 , I create a single collider(based on the collider type)
If the result is more than 1, I create a compound collider.
Sweenie
 
Posts: 481
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun May 01, 2016 2:45 pm

I added the collide field to the NewtonBody.cs, but that is no what we want what we want is
a drop down list to chose the collider from. them when the collider is selected, that collider open the option for that, but I do now who to do that.

another way is to make the collision a composed that can only be assigned to NewtonBodies
for these we nee a way to tell unity that a body must take one and only one collision component but that component can be of any of the subclasses of that component excluding the base class.

I think this is done by suing these
[AddComponentMenu("Newton Physics/Rigid Body")]

but I do not know how yet. But I believe this is a simple and more elegant way.
Basically we make the collider a component that can only be applied once to a NewtonBody.
what do you think?
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun May 01, 2016 2:58 pm

actually I see that you already have they was said last.
I am adding the Box to nee of we can create one complete body. 8)
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sun May 01, 2016 3:15 pm

actually I see that you already have they was said last.


Yep, I worked hard on that solution and is very proud of it. :D

In the current code, when Start() in NewtonWorld is run, the world is created.
But it can happen that Start() in NewtonBody is running first, then dNewtonWorld m_world will be null.

But we could add a check in CreateBody(float mass)...
Like this...

Code: Select all
public dNewtonBody CreateBody(float mass)
{
  if(m_world == null)
    m_world = new dNewtonWorld();

  return m_world.CreateBody(mass);

}


also we would have to change Start() as well
Code: Select all
void Start()
{
  if(m_world == null)
    m_world = new dNewtonWorld();
}


This way, if NewtonWorld is starting first, the world will be created in NewtonWorld.Start()
If a NewtonBody is starting first, the world will be created in NewtonWorld.CreateBody().

[EDIT]
Maybe that could work for Body <-> Joint as well?

We have NewtonWorld , NewtonBody and NewtonJoint.

* NewtonJoint is starting first, calls the NewtonBody.
* NewtonBody hasn't started yet, so calls the NewtonWorld to create a body.
* NewtonWorld hasn't started yet so creates a World.

It would be like a chain reaction, the order of creation wouldn't matter.
Sweenie
 
Posts: 481
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun May 01, 2016 4:19 pm

ye I guess we will have to do the same check even for collision. this way no mattr what order the world will be always first regardless what gameObject was called first.
Lets us cross our fingers and ther si not multithread creation.

ideally we should not have to use by start do no pass parameters

anyway I was try to do the same for adding a colliding box, but I am getting all confused working in two areas.
I will try to focus on the CPP side to get a small sub set of function that we can call. otherwise is too much trial and error.
Julio Jerez
Moderator
Moderator
 
Posts: 10916
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

PreviousNext

Return to User Gallery

Who is online

Users browsing this forum: No registered users and 2 guests