Unity plugin progress?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Unity plugin progress?

Postby Sweenie » Sat Feb 18, 2017 2:29 pm

Ah, sorry. I know what you mean now.

The NewtonMaterialInteraction asset defines how a material pair interacts.

So you create an asset called RubberConcrete
In it you define two materials and the friction constants.

Saw you made NewtonMaterialInteraction reference two NewtonMaterial objects.
Did that work? Can one asset reference another?

And at start you could probably use this function.
AssetDatabase.FindAssets
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Sat Feb 18, 2017 2:42 pm

Sweenie wrote:Saw you made NewtonMaterialInteraction reference two NewtonMaterial objects.
Did that work? Can one asset reference another?

I do not see why it would work, if you think about textures are assets that most be referenced by other assets like shader and materials.

The part I am not sure is if an asset can reference a game object like the world. If that is possible it make easier to write the code, if it no then is harder by not impossible.
There are still many unknown, be we can solve then as the come along.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Sat Feb 18, 2017 2:49 pm

I just checked your materials in Unity and in does indeed seem to work.

Scratch what I said about AssetsDatabase, that seems to be for Editor plugins only.
But there must be some way to find the assets at runtime as well.

I'm not sure but assets seems to be kind of like single global objects.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Sat Feb 18, 2017 2:54 pm

yes assets seem to be static global objects, if I knew that we should probably made the newton world form an asset.


It does not seem like assets can reference component of other gameobjects we are going to have to do it using search the when the work did created. not a big deal.
I do hope that we can make call to Integrations, because that will be the interface back to the scripts.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Sat Feb 18, 2017 3:12 pm

Just tested this and it works...

Code: Select all
 var materials = Resources.FindObjectsOfTypeAll(typeof(NewtonMaterial));
        var matPairs = Resources.FindObjectsOfTypeAll(typeof(NewtonMaterialInteraction));

        Debug.Log("Materials found: " + materials.Length);
        foreach(NewtonMaterial mat in materials)
        {
            Debug.Log(mat.name);
        }

        Debug.Log("MaterialInteractions found: " + matPairs.Length);
        foreach (NewtonMaterialInteraction matPair in matPairs)
        {
            Debug.Log(matPair.name + " K:"+ matPair.m_kineticFriction + " S:"+matPair.m_staticFriction);
        }


[EDIT]
Hmm, doesn't seem they exist until you load them first. Either by referencing from a gameobject or loading them manually with Resources.Load or Resources.LoadAll or inspect them in the Editor.

Seems the following setup works.
Under project assets, create a folder called Resourcers
Under that create another folder called NewtonMaterials
Move the materials and materialinteractions there.
Then before the above script, call this function
Code: Select all
        Resources.LoadAll("NewtonMaterials");


This will load all assets in the folder NewtonMaterials and you can then find them with
Resources.FindObjectsOfTypeAll
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Sat Feb 18, 2017 9:10 pm

ok I followed those steps, but It can't get it to work.
can you take a look to see what is wrong.

the change is in file NewtonWorld.cs line 102.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Sun Feb 19, 2017 4:44 am

Try now.
I changed the function call to Resources.FindObjectsOfTypeAll<T>().
You were calling Object.FindObjectsOfType<T> and that doesn't return assets, only active components.

By the way, i guess Newton needs every material to have some kind of unique id?
Every asset, gameobject and component in unity gets an unique instance id then created.
Maybe that could be useful?
https://docs.unity3d.com/ScriptReferenc ... nceID.html
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Sun Feb 19, 2017 6:33 pm

I now elaborate some more on material setup.
I hooked the three call backs, for now it only answer the On AABB Overlap.
and yes the unique id came up handy.

please when you \sync tell if it makes sense to you.

one think that I do no know to do is how to assign sub shape ID.
for now a collision tree will see the material that is assigned to it. late we will need to figure out how to associate graphics material to triangle physics materials, but for now this is no a priority.

the other thing we need to think about is who do provide notifications to the scripts.
I remember you said that using delegate was a bad idea because was too slow, or because the game object may not be aviable.

no sure which one it was, but I thonk we do nee to use delegates other wise how will it communicate ifrmatiuon to the game actor.

I was thinking that the Material Integration can have a function callback
OnCollision (GameObject1, GameObject2)

no sure if we need to pass the contact array.


later I will make the friction and restitution demo, then we think about notification make we can make a test that lay sounds, of make actors change color or something like that.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 20, 2017 12:20 am

Ok I added the basic friction coefficient scene with few material interactions.
The appear to move slow but that because ether are very bug is size, so is an optical illusion.

Now I will add the notification funtionality.

here there are many possibilities does to Newton flexible interface, so we are goin to have to settle for some special functionality.

my first idea is that Maybe we need to add a notify function to allowed allow for a Material.
Material Interation is cool for accurate physical behavior but for most people they are fine with setting parameters unilaterally but it get tedious to populate a large table.
To alleviate this we burden we can add a function and an option that say allow notification.
and if the flag is set then the callback is made and the script can override the parameters fopr the interaction.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 20, 2017 3:18 am

Ok I am stocked again, I tried to add a delegate for sending collision callback events.
but the moment I made the function call the code goes into an infinite loop and never came back.

I think this is similar tow what we saw before, that either we can not make call to Unity object from another thread or that or that some how the object is no active, I do no remember.
maybe you can see what I am doing wrong.
Below is the code, if you uncomment the four lines, you will be able to reproduce it.

Code: Select all
void dNewtonWorld::OnContactCollision(const NewtonJoint* contactJoint, dFloat timestep, int threadIndex)
{
   NewtonBody* const body0 = NewtonJointGetBody0(contactJoint);
   NewtonBody* const body1 = NewtonJointGetBody1(contactJoint);
//   dNewtonBody* const dbody0 = (dNewtonBody*)NewtonBodyGetUserData(body0);
//   dNewtonBody* const dbody1 = (dNewtonBody*)NewtonBodyGetUserData(body1);
//   dbody0->m_onCollision(dbody1);
//   dbody1->m_onCollision(dbody0);


with out the ability to call script from the engine it becomes hard, we are going to have to iterate over all contacts after the update, which will mean that some functionality, like contact modifications will not be possible unless we set it a material options with flags and variables.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Mon Feb 20, 2017 4:22 am

I suspect this is the same problem we had with forceandtorque callback.
Basically the problem is that GameObjects(and it's components) can't be accessed from outside the main thread.

Maybe dNewtonWorld::OnContactCollision could collect all collisions in an array and then loop through that array and trigger the gameobject OnCollision events/calls after NewtonUpdate.

I guess that would be the same as iterating through the contacts though and we loose the ability to modify the contacts during the simulation step. :(

Would it be possible to split up NewtonUpdate into two phases?
Like a collision phase and simulation phase? I don't know the internals enough of Newton to know if that's even possible.
So that updating Newton from C# would take two calls.
Something like this...

NewtonUpdateCollision(world, timestep)
//Collect and trigger collision events here
NewtonUpdateSimulation(world, timestep)
//Update transforms here...

Forget the above if it sounds idiotic, just throwing ideas. :wink:

[EDIT]
I think it's hanging for some other reason though.
If I change the declaration of the callback function(and the delegate) to this...
Code: Select all
    void OnCollision(IntPtr otherBody)
    {
        Debug.Log("collision"); // This works
        //Debug.Log("collision " + this.gameObject.name); // This cause Unity to hang
    }


That is, intptr instead of dNewtonBody as parameter.
But as soon as I try to access the gameobject Unity hangs.

So i guess, the user can manipulate the contact as longs as they don't touch the gameobject in any way.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 20, 2017 7:51 am

oh not splitting the newton update is a major problem.
I suspected that was the problem yes.

Ok we can do it when we are iterating over the Game object top get the force an torque callback.
Or maybe we can make another callback after the newton Update to do the same thing.

the only problem I still have is that we need to get a reference to he other colliding body, and the only information we have is a newton the NewtonBody without having to do a search.
I will set it up maybe you can tell me how to do that next.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 20, 2017 8:33 am

do you think that we should implement a new loop to iterate over the contacts after the frame update, or should we re use the rigid bod upstate loop, sine we already have the body and we can save the over head. Either way the notification will be one frame update behind, but we can no control that anyway.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 20, 2017 9:28 am

Ok I made the change and now we get collision notification.
I also made conditional under a variable name m_needsCollisionNotification that the player nee to set explicitly.
Her is where we now nee to make our Newton Script Interface so that the use can add script and get event from OnCollision so that he do no have to derive bodies just for that.

later we probably have to add have to add a couple of functionality, but for now this is enough for version 1.0 of the plugin.

here is where is get tricky, the function loo like this and you can see the loop in NetwonWorld.cs

Code: Select all
    //  I do not know how to get a NewtonBody from a dNetwonBody, ideally a reference can be save with the NewtonBody
    //  public void OnCollision(NewtonBody otherBody)
    public void OnCollision(dNewtonBody otherBody)


the problems is that, NewtonBody are awared of dNewtonBodies, but dNewtonBodies does not.
since dNewtonBodies are c# object, we should be able to store a reference to the c# NewtonBody Owner in it, but I have not idea how.
I will be really disastrous if we have to do a search for that.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 20, 2017 10:05 am

I move the On Collision to our NetwonBodyScript.

This is much more elegant that the method I did for applying force an torque using actions
I will change that to be a method on the NetwonBodyScript.
There can be as many script as the use need it.

The only thing left is to identy the NewtonBody in the other dNewtonBody and we are ready to make a mini game of pick and collect objects.

BTW InitRigidBody not longer uses sceneIndex, we should be able to create and destroy bodies at run time now.
Little by little the plug in is taking shape.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
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 15 guests