plugin for unity 3D

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

plugin for unity 3D

Postby blackbird_dream » Thu Jun 02, 2016 11:34 am

Hi all
Can anyone tell me if the plugin for unity is useable right now ?
Thks so much for any reply
User avatar
blackbird_dream
 
Posts: 379
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: plugin for unity 3D

Postby Sweenie » Thu Jun 02, 2016 5:25 pm

Hi.

The plugin isn't ready yet. We are still deciding the best approach for creating the wrapper code. Once we get a solid base the plugin should be trivial to finish. If you're eager to try it out you can download the current work done but it's far from production ready right now and only a few functions has been wrapped to test the plugin.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Fri Jun 03, 2016 2:12 pm

Hey Sweeney, I think I made some good progress last weekend.
I did not committed because I made a local copy.

I did not get as far as I hoped, I has other commitment,
but I believe these weekend I will have a solid and robust foundation to build upon.

here are some points:
-after given another try with swigs, ai believe is it much better that using cppShaps, yet we have ot do the wraping thing to newton core, but afte that we get a lot in return

-the wrappen is actually very clean, I was keeping an of the legacy code, but now I am full convince that this is a better way so I will clean up all the legacy before committing. we can always get the old code from the history.

-I am focus in getting the basics, world, bodies, and collision, I know you want to get joints and vehicle stuff, but we must learn to crawl before running :D

-I believe that after I commit what I have then moving forward will be quite easy just, and cookbook procedure.

-I believe I found that the reason why we have problems, was that we are trying to fight Unity by using their way to create components. That is actually a mistake, we must let unity do what it want to do, and Newton do what it want to do.
for this we should not try to create or destroy any newton object form a Star, or on destroy.

I try that with bodies and shapes, but not with the world, and everything start to magically work. but I still get a memory leaks with the world, the it dawned on me that the world/body should also work the same way as body/collision.

you will see after I check it in.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Sweenie » Fri Jun 03, 2016 4:16 pm

Hmm, sounds interesting. Dont really understand how you make it work without start and destroy. I look forward to see your approach on this.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Tue Jun 07, 2016 10:55 am

I think have the mechanic working, without the dreaded crashes.
I could not do anything last weekend, I am trying to do a little one hour each morning, but
I am having some problems with csharp learning curve, I cann't figure out why
this is an error
Code: Select all
dNewtonCollision BuildCollision(List<NewtonPlugin.NewtonCollider> colliders)
s(45,49,45,82): error CS0246: The type or namespace name 'List<NewtonPlugin.NewtonCollider>' could not be found (are you missing a using directive or an assembly reference?)


but this is not.
Code: Select all
 dNewtonCollision BuildCollision(NewtonCollider[] colliders)


another thing I have problem is how can I pass a csharp class as the use data to a newton objects, I try many different ways, but I keep getting errors.
I read over the web and it seem I nee to cerate a Garbage collector memory object by I can to figure out how.

If I can get pass those to point I think I we can get a clean starting point, but I never though it was going to be so hard.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Sweenie » Tue Jun 07, 2016 11:48 am

The first seems to be you forgot to include the needed assembly for list object.
Try adding
Code: Select all
using System.Collections.Generic
at the top.

The second thing you're looking for is probably a gcHandle object. Google on gchandle for info.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 08, 2016 8:51 am

ok that workled,

now I am stoked on this.

Code: Select all
     public void Init()
        {
            // get the list of all collision components associated with the body
            List<NewtonCollider> colliders = new List<NewtonCollider>();
            TraverseColliders(gameObject, colliders);

            // generate the collision shape
            dNewtonCollision collision = m_world.BuildCollision(colliders);

            // create the body
            Matrix4x4 matrix = Matrix4x4.identity;
            matrix.SetTRS(transform.position, transform.rotation, Vector3.one);

// here I can't get the address of the matrix array of floats
            m_body = new dNewtonDynamicBody(m_world.GetWorld(), collision, &matrix.m00, GCHandle.ToIntPtr(m_pinThisObject));
        }



to initialize a body properly It nee the matrix array of floats, but I can no see to get that no mattes what O do I keep getting these errors,

    >C:\Development\NewtonUnityPluginUsingSwig\NewtonUnityPlugin\NewtonPlugin\NewtonBody.cs(237,76,237,87): error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
    2>C:\Development\NewtonUnityPluginUsingSwig\NewtonUnityPlugin\NewtonPlugin\NewtonBody.cs(237,76,237,87): error CS1503: Argument 3: cannot convert from 'float*' to 'SWIGTYPE_p_float'

I do no see how to apply the cgHandle or the Marshal stuff.

I see you use the keyword ref, but how do I make the prototype to take a ref as argument, that for cs functions,
basically all it need to pass the array of floats that make the matrix.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Sweenie » Wed Jun 08, 2016 9:11 am

To be able to pass the matrix using ref keyword the constructor for dNewtonDynamicBody needs to declare the third parameter as "ref Matrix4x4".

It seems SWIG has interpreted the float* as a class and generated the wrapper class SWIGTYPE_p_float.

It would probably be difficult to make SWIG interpret the "float*" to "ref Matrix4x4" so instead I think you will have to make SWIG convert "float*" to "IntPtr".
Then you use this code to pass the matrix array(or Matrix4x4 struct) as an IntPtr instead.
Code: Select all
// Initialize unmanged memory to hold the struct.
IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(matrix));
// Copy the struct to unmanaged memory.
Marshal.StructureToPtr(matrix, pnt, false);
m_body = new dNewtonDynamicBody(m_world.GetWorld(), collision, pnt, GCHandle.ToIntPtr(m_pinThisObject));
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 08, 2016 10:14 am

When using that code I the get this error
C:\Development\NewtonUnityPluginUsingSwig\NewtonUnityPlugin\NewtonPlugin\NewtonBody.cs(240,76,240,79): error CS1503: Argument 3: cannot convert from 'System.IntPtr' to 'SWIGTYPE_p_float'

I still do no know how to use a SWIGTYPE_p_float

how to I make a cpp class function use a ref modifier, when I read the definition of ref and say pass argument by reference, but I do no get what is mean.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 08, 2016 10:39 am

in c# there is this struct IntPtr, but I guess there should be something like FloatPtr
by that does not work
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 08, 2016 10:58 am

Ok I think I got by changing the prototype like this.
Code: Select all
   //dNewtonDynamicBody(dNewtonWorld* const world, const dNewtonCollision* const collision, const void* const matrix, void* const userData);
   dNewtonDynamicBody(dNewtonWorld* const world, const dNewtonCollision* const collision, const void* const matrix, void* const userData);


Man this dancing that has to be done to pass dat between cpp and c# is really ugly, but I believe now have the very minima set up.

I will test it a lithe and if is fine I will probable commit it tonight.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Sweenie » Wed Jun 08, 2016 12:40 pm

Yep, by changing float* to void* SWIG will make it an IntPtr instead. In Net an IntPtr basically is a void*
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 09, 2016 9:59 am

Ok I check in what I have. Remember it I sonly a prove of concept.
It does the following

-you can create many NewtonWorlds as GameObjects
-You can create NewtonBodies and associate them to any NewtonWorld by assigning a newton world as a reference

on initialization NetwonBodies do nothing.

The Star function of a NewtonWorld scan the scene starting for the root. I call InitRigidBody for each Object that contain a reference to that Newton World.
On Delete the NetwonWorld do the opposite, scane the Scene calling DestroyRigidBody for each GameObject with a reference to that world.

The reference to the dNewtonBody is used as a switch to synchronize everything.
Creation and destruction of collision is done in similar way.

what this does is that the synchronization of Newton, Bodies and collision is no controlled by the garbage collector, instead is synchronized by the function pair Start/OnDestroy of a NewtonWorkd

I run it few times and is seem stable, no a single crash nor memory leaks, all object are created and destroyed in the proper order, even if they are delayed.

I was trying to move to add fumtionality, like make the box a flat long box to add more objects, by for some reason the Unity demo, seem frozen even when I open the demo, it is as if the demo is perpetually running, I can't select the Box for editing.
even I add new boxed, I do no know why that is, the other demo does no show that behavior.

anyway I check it, please see if this make sense to you.

I did clean up the rest of the code, but if ths make sence thet wea can do that and move forward with a fresh start.

what I wan to do nect is, add bod\y properties like Mass, kinematic/dynamic, etc
the have a basic demo of a small Box on top of a flat big box acting as a static ground, by some how I can edit the scene.

anyway, I will try making a new demo for the beginning.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 09, 2016 10:35 am

Oh I figured out why I was not able to select the Box, It was because I was in the GameView mode,
that like seen the world form the Camera.

when I change to ScenView the I can navigate the scene, select and edit gameobjects.
I though that it was a side effect of eth plug in.

Noew I will add some more basic fumtionality,
-a Floor, and a box falling on it.
-then I will add the funtionality that you have that show the debug display.

That may have some problems, because I do no see how to query object that has no being made yet, for example if I wanst to diaply a Box, I cann't call debug display, on a body that dos no have a world.

There most be a way to make Editor Plug in that allow manipulation that kind of stuff, I'd seem youtube videos that do stuff like splines and bollean and they seem to run in editor mode, no by running the game.
Basically we nee a way to display the collision gizmos and some other features.

but anyway I am not there yet, just thinking about.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 09, 2016 1:14 pm

here is something I found weird, some of the script do not have the check box bottom to turn them on and off, for example the CollisionBox. When I compared it to the NewtonBody, I do no see anything special. When read over the forum I found the same question and someone gave these answer.
Your MonoBehaviour won't have a checkbox next to it in the inspector if it doesn't have any of the per-frame methods, like Update, FixedUpdate, OnGUI, etc.

is that correct?
Julio Jerez
Moderator
Moderator
 
Posts: 12426
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 8 guests

cron