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 Kaos » Sat Apr 23, 2016 3:29 pm

Yea youre right. The referencing through another layer shouldnt make a diffrence in that.

I made an interestig observation.
I made a stack of boxes and placed 9 under a First as a child by accident instead of making an empty.
That made no diffrence with a physx stack.

But a Stack made with newton Bodys it behaves diffrently. One Cube stayed in air before sinking and then made some crazy moves. So there is a diffrence when a newton body has a newton body as a child. That behaviour is definitve reproducable. The body of which the other where the children behaved diffrently and stayed in air instead of normal falling. And when multiplying this by putting it under another some more levels things explode ;D There are no joints or other things in play so its only the gameobject children connection in place.

That might even explain some of the car issues we got with the floating and getting in air???
Kaos
 
Posts: 38
Joined: Mon Apr 18, 2016 11:24 pm

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sat Apr 23, 2016 4:41 pm

You can't/shouldn't have Game objects with rigid bodies in a parent/child hierarchy. Newton only retrieves the world transformation from each gameobject at scene start. What you are seeing is just a visual error. If gameobject Y is a child of gameobject X, the transform of child Y will be multiplied with transform of gameobject X.
When I enable the debug rendering again you will see what i mean.

I guess physx sets the transform on the parent first and the child next, that way the parent transform wont mess with the child transform. Attaching bodies together can only be done with joints.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Kaos » Sat Apr 23, 2016 5:18 pm

Sweenie wrote:You can't/shouldn't have Game objects with rigid bodies in a parent/child hierarchy. Newton only retrieves the world transformation from each gameobject at scene start. What you are seeing is just a visual error. If gameobject Y is a child of gameobject X, the transform of child Y will be multiplied with transform of gameobject X.
When I enable the debug rendering again you will see what i mean.

I guess physx sets the transform on the parent first and the child next, that way the parent transform wont mess with the child transform. Attaching bodies together can only be done with joints.



Any and all physx rigidbodies are in worldspace handled (transform prostioned and rotated)and never in local. Forces and Torque also. So there no need to handle parent first and then child next it just uses the world transform Position / rotation of the gameobject.transform and nothing else i think.

This is then just something that should be prevented to be possible to setup then?

And Newton uses local space after initial setup? I will wait then to see in debug mode whats going on here and what you meant only visual. :D

One thing that seems to be exactly the same is the joint without connected body behaviour which connects it to world space ;D Thats nice. Havent that fully tested but it looked that way.
Kaos
 
Posts: 38
Joined: Mon Apr 18, 2016 11:24 pm

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sat Apr 23, 2016 5:58 pm

Kaos wrote:I made an interestig observation.
I made a stack of boxes and placed 9 under a First as a child by accident instead of making an empty.
That made no diffrence with a physx stack.

But a Stack made with newton Bodys it behaves diffrently. One Cube stayed in air before sinking and then made some crazy moves. So there is a diffrence when a newton body has a newton body as a child. That behaviour is definitve reproducable. The body of which the other where the children behaved diffrently and stayed in air instead of normal falling. And when multiplying this by putting it under another some more levels things explode

All of this will be take care off. we are on a very preliminary stage.
for now just place there Gameobjects at the root level.
I am learning a little more before start to get dirty on it, But believe it will be seamless integration.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sun Apr 24, 2016 1:48 am

Now you can display the debug render by checking "Debug render" on the NewtonManager.
To see the debug render, click on the "Gizmo"-button on the top right of the render window.

I also realized now what is causing the weird parent child thing.
It's not a transform problem, it's the parent NewtonBody finding the child NewtonBody's colliders and adding them to it's own collider.

Basically, when a NewtonBody is created it uses GetComponentsInChildren to find all colliders.
If more than one collider is found it combines them into a compound collider.

We will need to implement our own "GetComponentsInChildren" function that skips any child objects that contains a NewtonBody. That should fix the problem.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sun Apr 24, 2016 6:50 am

You can't/shouldn't have Game objects with rigid bodies in a parent/child hierarchy


I retract that statement. :oops:

Placing NewtonBodies in hierarchies should now work.

Mind though, they will still act independently, that is, a parent NewtonBody will not affect the child body in any way.

Placing NewtonBodies in hierarchies are useful for grouping them and easy placement in the scene editor though.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sun Apr 24, 2016 9:23 am

Julio has now got swig setup so we will remake the plugin from scratch so the plugin is easier to manage on the c++ side, and get all Newton stuff wrapped. :D
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun Apr 24, 2016 1:01 pm

ok I now committed a project with an swig script that generates the Newton.wrap.cs for Newton and the dMath libraries.
This are the minimum that we nee to start making the plugin.

The project requires that you set environment variable NEWTON_DYNAMICS and point to the root folder of the newton SDK.
In my system this is defined as:
    NEWTON_DYNAMICS=C:\Development\newton-dynamics

you need to build with visual studio 2015 the newton sdk solution:
../newton-dynamics\packages\projects\visualStudio_2015_dll\build.sln

the you can build
../NewtonUnityPlugin\NewtonUnityPlugin/NewtonUnityPlugin.sln

and it should compile with no errors or warning.

I have no tested or try building the CS version yet, but I hope it work.

Now the real work start with is we need to write a c sharp plugin using only s sharp classes and functions using the newton_wrap.cs generated by the NewtonWrapper.vcxproj

sorry Sweeney but this will requires we clean the project and start writing it again using the knowledge you gain by making the prototype.

the learning curve is stiffer for me because I nev did c harp before, but I do no thonk is that different that c++ anyway.

one thing I noticed is that in the c sharp projects are not like a c ++ projects that you can remove file from the solution but leaves them folder, c sharp projects either try to delete the file every where or try to compile then even when they are not added to the explorer, I found that weird.

anyway My recommendation is that we make a backup of all the file you wrote and we start writing the force class with should be like newton.cs and have that interacting with unity doing the basics stuff. This is all I have for now.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun Apr 24, 2016 1:04 pm

oh I forget next step is to clean up what does no belong to the NewtonWrapper
like Vehicle.cpp and Joints.cpp, I leave there for reference, but once we have a running plug in all we need to do is add the header for those file to the newton.i file and added some swig command to deal with c++ / c sharp compatibility issues.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun Apr 24, 2016 3:20 pm

ok now the fun begin, and I am all confused,

I try this class
Code: Select all
    //   internal class NewtonWorld : Singleton<NewtonWorld>
    class NewtonWorld: IDisposable
    {
        protected NewtonWorld()
        {
            //pWorld = NewtonAPI.NewtonCreate();
            pWorld = cppPINVOKE.NewtonCreate();
            Debug.Log("Newton World created(" + pWorld.ToString() + ")");
        }

        public void Dispose()
        {
            cppPINVOKE.NewtonDestroy(pWorld);
            Debug.Log("Newton World destroyed(" + pWorld.ToString() + ")");
            pWorld = IntPtr.Zero;

            Debug.Log("Newton World destroyed(" + pWorld.ToString() + ")");
            pWorld = IntPtr.Zero;
        }

        protected IntPtr pWorld;
    }


and I get this error
C:\Development\NewtonUnityPlugin\NewtonUnityPlugin\NewtonPlugin\NewtonWorld.cs(70,38,70,44): error CS1503: Argument 1: cannot convert from 'System.IntPtr' to 'System.Runtime.InteropServices.HandleRef'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


when a look at the definition of those two functions in the wrapper, they are different.

Code: Select all
 public static extern global::System.IntPtr NewtonCreate();
 public static extern void NewtonDestroy(global::System.Runtime.InteropServices.HandleRef jarg1);



one returns IntPtr, but the other take HandleRef , I have no found a way to conver one to the oether does any one know C# that can tell me what is worng?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Sweenie » Sun Apr 24, 2016 3:41 pm

I'm not sure but I think Swig wants you to create the world like this now...

Code: Select all
SWIGTYPE_p_NewtonWorld world = cpp.NewtonCreate();

My version returned the raw c++ instance pointer which you store in a IntPtr on the C#-side.

Swig seems to instead return the object SWIGTYPE_p_NewtonWorld this handles the IntPtr internally

I'm having problems with compiling the solution though.

In the wrapper, the file Newton.i is included, but I guess the file newton_wrap.cxx should be included.

So I added that but got loads or warnings and errors about macro redefinitions.
Googled and realised stdafx.h should be at the top.

But now I get these errors instead...

Code: Select all
1>------ Build started: Project: NewtonWrapper, Configuration: Release x64 ------
1>  newton_wrap.cxx
1>newton_wrap.cxx(1504): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>  C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\string.h(119): note: see declaration of 'strcpy'
1>newton_wrap.cxx(2234): error C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>  C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\string.h(346): note: see declaration of 'strncpy'
1>newton_wrap.cxx(4036): error C4996: 'NewtonWorldSetCollisionConstructorDestuctorCallback': was declared deprecated
1>  C:\newton\coreLibrary_300\source\newton\Newton.h(519): note: see declaration of 'NewtonWorldSetCollisionConstructorDestuctorCallback'
1>newton_wrap.cxx(4880): error C4996: 'NewtonConvexHullGetVetexData': was declared deprecated
1>  C:\newton\coreLibrary_300\source\newton\Newton.h(628): note: see declaration of 'NewtonConvexHullGetVetexData'
1>newton_wrap.cxx(5572): error C4996: 'NewtonUserMeshCollisionContinueOveralapTest': was declared deprecated


EDIT...
Forgot to download latest Newton...
Trying again...

Ok, is this correct.
Compile once, swig generates files.
Then modify solution, remove Newton.i and add Newton_wrap.cxx

Modify Newton_Wrap.cxx by adding

#include stdafx.h at top
#pragma warning(disable: 4996)

Now it compiled without error.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun Apr 24, 2016 4:46 pm

Sweenie wrote:I
Code: Select all
SWIGTYPE_p_NewtonWorld world = cpp.NewtonCreate();

My version returned the raw c++ instance pointer which you store in a IntPtr on the C#-side.
Swig seems to instead return the object SWIGTYPE_p_NewtonWorld this handles the IntPtr internally

Of course, it is compiling now, awesome!!
How did you figure that out, I could not make head or tail of the file. This show my inexperience in c#. Any way is work now and that the importan part.

Now with need to decide what kind of c# object wil will use, I was readind that IDisposal, which is the closest I can see to a C++ class with a destructor.

I do no think a singleton is a good idea, because many people do in fact use mutiplete newton worlds, a singleton will no allowed for that in a simple way. Beside using a singleton is right away a
sign of a bad design

I was reading the IDisposal thing, but is still confusing to me all example I'r seen use extra variable to prevent calling the deposal method twice.
Any way I nee to learn more before moving on,
But a least we have a good start now.


Then modify solution, remove Newton.i and add Newton_wrap.cxx
Modify Newton_Wrap.cxx by adding
Now it compiled without error.


you do no need to remove the newton.i file if you come to accept it will build the script each time.
The are ways to include file tendencies to the rule, so that the Newton.I does not recreates the wrapper each time the build is compiled. I knew how to does in order VS but 2013 and up are a lithe different now. Later I see if I can get an example form my Script project which is VS 2010

Late I see if I can add dependency to the cosum rule, so that is act nicelly
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun Apr 24, 2016 5:12 pm

Ok, it seems everone have different options of the IDispose stuff. then I found that there is also Finalized which seems to be even worse, basically a problem in search of a solution.

As it turns out c sharp seems to have destructors just like C++, I re factored the class sample like this.
Code: Select all
    class NewtonWorld
    {
        protected NewtonWorld()
        {
            pWorld = cpp.NewtonCreate();
            Debug.Log("Newton World created(" + pWorld.ToString() + ")");
        }

        ~NewtonWorld()
        {
            cpp.NewtonDestroy(pWorld);
            Debug.Log("Newton World destroyed(" + pWorld.ToString() + ")");
        }

        protected SWIGTYPE_p_NewtonWorld pWorld;
    }

why is not that a valid implementation?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Plugin for Unity 3D

Postby Kaos » Sun Apr 24, 2016 7:28 pm

edit: well it compiles fine so is this not working in unity then?
Kaos
 
Posts: 38
Joined: Mon Apr 18, 2016 11:24 pm

Re: Newton Plugin for Unity 3D

Postby Julio Jerez » Sun Apr 24, 2016 7:42 pm

Kaos wrote:Well you call pWorld.ToString() after it is destroyed. Is That legal?


good point, but that was not was I was referring to, I was taking to the issue that garbage collision in c sharp is out of order. I do no thong that a problems at all.
however aparently in unit object creation is also out of order, and that could be a problem.

take for example that you create a newton word, and you add a body.
the you quit unity and unity kill the word then try to kill the body, that will cause a problem in newton because newton will destroy already.

I do not think that this will be a problem either, because I envision the world having a collection of Proxy rigid bodies, not direct newton Bodies, the proxy will have a newtonBody object,
the allow for the work to have objects that may of may no have Newton bodies.

but we are going to far ahead now.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
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 10 guests

cron