plugin for unity 3D

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 16, 2016 3:54 pm

al right now box shape has full editing capabilities.
it can be resize, rotated, and translate and scaled. plus is also inherit the parent scale. :mrgreen:
now adding one of more shape to try adding multiple shape to same game objects.

as we get familiar it becomes easier and easier to make progress. :mrgreen:
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 17, 2016 4:35 am

This code needs some more work...( inside GetShape() )
Code: Select all
// find the world that own this collision shape
  NewtonBody body = owner.GetComponent<NewtonBody>();
  while (body == null)
  {
    // this is a child body we need to fin the root rigid body owning the shape
    owner = owner.GetComponentInParent<Transform>().gameObject;
    body = owner.GetComponent<NewtonBody>();
  }


If a user adds the collider to a gameobject and a NewtonBody component hasn't been added before, the Unity editor will freeze since we get stuck in an infinite loop. The loop will never find a Body or the World.

I think the above code can be replaced with this as well
Code: Select all
NewtonBody body = owner.GetComponentInParent<NewtonBody>();


This will first try to find the NewtonBody component in the current GameObject and if non is found recursive search upwards in each parent, returning Null if no NewtonBody Component was found.

https://docs.unity3d.com/ScriptReferenc ... arent.html
Description

Returns the component of Type type in the GameObject or any of its parents.

Recurses upwards till it finds a valid component. Returns null if no component found. Only component on active Game Objects are returned.


The question is, how should we treat collider components without a owning body?
Unitys own collider becomes a static collider if no body owns it, should we do the same?
Maybe adding it to a scene collider?
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Fri Jun 17, 2016 11:11 am

ha yes there is a bug there, I was trying to walk the hierarchy until the root by tryin to find gameobjects, but that too many indirections. I change to use Transform instead
This is how it look like. It test and seem to work
Code: Select all
NewtonBody body = null;
            Transform gameTransform = transform;
            while (gameTransform != null)
            {
                // this is a child body we need to fin the root rigid body owning the shape
                if (body == null)
                {
                     body = transform.gameObject.GetComponent<NewtonBody>();
                }
                gameTransform = gameTransform.parent;
            }


I remove the NameSpace, it seem to generate more problems than need it, and it seem that C# in some case simple ignore, or read a file form some stupid archive that I do no know where is store.
I am having a big hard time when the cod entry to read a dependency file in one machine, and when I opine the project on a different machine.
when I add the reference, it al2way get an absolute path, and I do not know why is that.

I committed the fix, please see if it build for you and you do not get compile error because the reference path are messed up.

The question is, how should we treat collider components without a owning body?
Unitys own collider becomes a static collider if no body owns it, should we do the same?
Maybe adding it to a scene collider?

to me, the rule should be, any collision found in and under a rigid body is a collision shape of that body.
This define if a shape is a single of compound.
to decide if a compound shape is a scene collision, the body will have a check box that will indicate what kind shape to make.

There can be dangling collisions, but if a gameobject do not have a Rigid body on it way to the root gameobject, the that shape will never get a dNewtonCollision.

similrally there can be dangling rigid bodies, bodye will onel be activated whe the user manually asigne the Netwonworld they belong to.
this should be self consitenet.

I will now add the basic iteration loop. see hwo that goes
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 » Fri Jun 17, 2016 11:22 am

her is a question. how do we set the time stamp for fix update?
MonoBehaviour.FixedUpdate()
I do not see anyway to do that, it seem to be an arbitrary 0.02 seconds, by how do we change that?
profile.png
profile.png (54 KiB) Viewed 7411 times


also the regular update is no much faster, I would thing this should go a several thousand frame per second, is there some setting that regulate the fps?

they say this is the way to do it,
http://answers.unity3d.com/questions/30 ... ditor.html
but how do I use that awake function?
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 » Fri Jun 17, 2016 11:59 am

even when I set this
Code: Select all
 void Start()
    {
        QualitySettings.vSyncCount = 0;
        InitScene();
    }


the fastest time step I see is 0.0066 which Is 151 fps, this is quite pathetic. Some else must be worng
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 17, 2016 4:22 pm

The FixedUpdate rate is controlled by the project settings for time.

Go to the menu Edit -> Project Settings -> Time
There you find the settings for time step, maximum allowed timestep and timescale.

To disable vsync go to Edit -> Project Settings -> Quality
There you can set vsync options at the lower part of the screen.
I get around 2500 fps when not using vsync.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Sun Jun 19, 2016 11:41 am

Ok Sweenie I think I have the first working version that can make rigid bodies.
It does not do any thing yet, because I have not hooked the force and toque call back yet.

now we nee to add some more functionality to the work, like :
-worker threads
-run asynchronous
-world gravity
-number of iterations
-etc

to the body, we nee to add an a Component base class, the force and torque call back will call on each body and iterate over it.
this way an use can subclass from that abstract component and use to add force, and control the body. Similarly for the contact call back. but this will be listen that the on Begin contact and contact the will call so ta the user can add the functionality.

The collision at the moment doe no add compound, that I will add later after we get something running first.

after we have that, maybe in the next few days, we can move to add joints.

I test it many time and it seem robust.

if you have time please sync and review it, see if anything can be improve or make more like the Unity people are use to. :mrgreen:

Ha one last thing, you where worry about Garbage collision affecting performance because using manage plugging, notice that the runtime does not stress the Garbage collector at all or a least I thing it does not.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Sweenie » Sun Jun 19, 2016 1:51 pm

Will check it out :D
Regarding multithreading and callbacks. Remember that Unity doesn't allow any threads other than the Unity main thread to touch a Game Object.
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Sun Jun 19, 2016 3:02 pm

ok we now have a fully futional scene, a floor, and a box falling on it.

There is one problem that I have no figure out, and is the racialization.
Code: Select all
serialization work on object that are no controlle but a editor script.
public class NewtonWorldEditor: Editor
{
    public override void OnInspectorGUI()
...

this is rather important because each time I open a scene it set the values to zero or to the default.
I spent lots of time yesterday trying to figure that out, but the moment I added serialization to the I had hard crashes in a mono developer functions.
I read many doc about serialization, but I could not figure it out what I was doing wrong.

on this.
Sweenie wrote:Regarding multithreading and callbacks. Remember that Unity doesn't allow any threads other than the Unity main thread to touch a Game Object.

is that true, why would the be?, the object are just pieces of memory, that sound to be like a huge inexcusable limitation.

My guess is that these is one of these myth that take a life of its own. Like people opinion of Physics engines.
right now Newton run on it own thread, it is no interfacing with gameObjects jet by I am abut o add the first Force and Torque event handler,
if I does not works, that will be bad but not a show stopper, all we nee to do is to have a double buffer on each body.
The body place the data, in one buffer for the Update function, and the Newton update collect that inputs. But like I said I do no believe that this is necessary.

anyway we now have our first working demo.
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 » Sun Jun 19, 2016 3:38 pm

just added a sphere.
Once the plugging is in an advance stage, this is what I have been looking for a long time.
and editor that allowed authoring of new features in an easy way without having to come up with a new c++ demo. just a few click and badabin badabun, you can test whatever you want. :mrgreen:
this is what I will use this for the smart rag doll.

after we are far alone, one feature I will add to the editor DLL is the serialize to the Newton asccii format the dScene library and also binary.
with that I not long have to make more c++ demos, and simple add a load and execute. that wa all the unity demos will be available in the SDK, also the oeth way around we will be able to import all he demos for the NewtonSDK. I love that posibility :mrgreen: .
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 » Sun Jun 19, 2016 4:52 pm

capsules and cylinders in, :shock:
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 » Mon Jun 20, 2016 9:53 am

Code: Select all
Sweenie wrote:
Remember that Unity doesn't allow any threads other than the Unity main thread to touch a Game Object.


sadly you are right, I added the first call back and when call form the newton thread, it crach afte a few seconds.
Platform assembly: C:\Unity\Editor\Data\Mono\lib\mono\2.0\Boo.Lang.Parser.dll (this message is harmless)
Platform assembly: C:\Unity\Editor\Data\Mono\lib\mono\2.0\UnityScript.Lang.dll (this message is harmless)
Registered platform support modules in: 0.0445539s.
Native extension for WindowsStandalone target not found
Load scene 'Temp/__Backupscenes/0.backup' time: 0.369476 ms
The thread 0x5504 has exited with code 0 (0x0).
The thread 0x44e8 has exited with code 0 (0x0).
Exception thrown at 0x000000001F3708E8 in Unity.exe: 0xC0000005: Access violation writing location 0xFFFFFFFFFFFFFFD8.


This is really bad because is almost remove the possibility of running asynchronous. or at least prevent the a large benefit.
also is force us to have loops, that has to be execute outside the newton update colleting temp data.

here is a question. Does Unity support multithreading at all?
say I have 1000 game object and I want to iterate over all of them from a component call a delegate function.
can that be done form some multiple thread that are unity threads?
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 » Mon Jun 20, 2016 11:54 am

I am now calling the delegate for the update method of the NewtonWorld,

Code: Select all
void dNewtonDynamicBody::ApplyExternalForces(dFloat timestep)
{
   dFloat mass;
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;

   NewtonBodyGetMass(m_body, &mass, &Ixx, &Iyy, &Izz);
   const dNewtonWorld* const world = (dNewtonWorld*)NewtonWorldGetUserData(NewtonBodyGetWorld(m_body));

   m_externalForce = world->GetGravity().Scale(mass);
   m_externalTorque = dVector(0.0f);

// this is a unity thrad, yet calling this delegate causes unity to crash.
//   m_forceCallback(timestep);
}


but that still causes the same crash. this is called from a Unity thread. I appear that calling a delegate that belong to a different gameObject is what really cases the crash,
I have not is why would that be.

It appear that unity is too simplistic, is no designed for asycroinosu events, you can only call what is active other wise is will crash.
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 » Mon Jun 20, 2016 12:30 pm

search on the web I found this
http://answers.unity3d.com/questions/20 ... crash.html

The said:
Ensure that your interop does not Callback into unity (if its asyncronous or fed from external thread independent inputs)


It appears that by design Unity is severally crippled for external developers.
this are the same Shenanigan special pleading that companies like Nvidia, Microsoft, AMD, when the claim to expose an API for free to the people.

They farce you use us ether command buffer nonsence, while they can access to internal functionality that the free PI use don't. That why I would not touch CUDA or AMP.

I look lie the only way to communal is by having a command Queue to send events from UnityPluggin, and another Command Queue to read event from Unity.
This is what I call some body run out of ideas in the Unity dev team.
I could not think of any method worse than that.
So far not too impressed with what I have seen.

they also said this
Make the callbacks call into a static function on something extending from System.Object and write the same kind of logic as above to request the information on classes extending UnityEngine.Object

so sure if this means that is possible can for example to querying an object to see if is active by deriving from System.Object and check out is ok to call that objects. have not investigated yet, do you know what that is?
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 » Mon Jun 20, 2016 1:46 pm

well I did this, I pass the callback to the newton update,
the newton update call the call back and iterate the scene scanning for gameobjects that have a dNewton assigned. the for each NewonCompenent found, call this on that compoenet

Code: Select all
    public void OnApplyForceAndTorque(float timestep)
    {
        if (m_body != null)
        {
            Vector3 xxxx = new Vector3(0, 0, 0);
            IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(xxxx));
            Marshal.StructureToPtr(xxxx, pnt, false);
            m_body.AddForceAndTorque(pnt, pnt);
            Marshal.FreeHGlobal(pnt);
        }
    }


at the moment I am applying the force, late that function will iterate over any NewtonForcesAction
and will add forces,
these NewtonForcesAction are the comppned that the use will use to control bodies, for example say we what a force field and or a se control,
the force is a component that apply that force, the use control is anther component that can apply force using a script and so on.

I sound elaborate, but as it seem the we could iterate the scene for NewtonWorld.Start before we call Newton Update, however is we set Newton to run asynchronous then the force accumulation can run into race condition form frame to frame.

like this, the NewtonUpate has a Check point that with until the update is completed, the after the update is completed the accumulation can be write again.

I am now iterating over the entire scene, this could be wasteful if there are lot of particles, and other stuff, but late will can cache the NewtonBodies in a vector and just iterate over that vector.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
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 1 guest