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 » Mon Jun 20, 2016 2:59 pm

ok now I added an example of a force actions

Code: Select all
abstract public class NewtonBodyForceAction : MonoBehaviour
{
    abstract public void ApplyForceAction(NewtonBody body, float timestep);
}

[AddComponentMenu("Newton Physics/Force Actions/force field")]
public class NewtonBodyForceField :  NewtonBodyForceAction
{
    public override void ApplyForceAction(NewtonBody body, float timestep)
    {
        body.m_forceAcc += m_forceValue;
    }

    public Vector3 m_forceValue;
}


at this stage we can now make our first function demo, maybe the proverbial a rolling sphere controlled by the use input. :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 » Mon Jun 20, 2016 4:05 pm

now I also added the ability to run the physics update synchronous or asynchronous.
the default is asynchronous and it seems to work fine.

If this works, that mean that as along as is played in a multicores system with at lest one spared core, and the frame take less that a Unity update the physics is actually free. all cost is on Unity :mrgreen: :mrgreen:
if the physic update is expensive the cost is Unit Plus the extra time to complete the frame since the both pay concurrently.
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 22, 2016 3:02 pm

I now added all the basic primitives, and I am doing the compound and scene collisions.
after we get that we can add a basics collision notification method and the we can stat adding joints.

These are the problem I have so far that I have no being able to get any information from docs, of form google.

1- serialization seem to he arbitrary is some components. all the componedt get serialize fines expect the NewtonWorld. Some people say that adding the [Serilizatble] attribute fixed and some people suggest different thing, I try them all but no matter what, the world always start wit default settings.
No sure how to fix that.

2- this in minor, but I notice that is a collision shape has a custom editor, the active check box is removed. this is annoying of the wire frame is always visible and you are doing some else. I can add a noteh check Box but it would be nice to follow the Unity feel and look.

2- this is important, there do not seem to be a Callback for when a gameobject is move form the hierarchy, is there is one I do not know what it is. This creates a problem if you have a child gameObject with a collision shape but without body.
If later you move the child gameobject outside the Hierarchy, then the collision be dangling.
I suppose I can a full hierarchical check each time the OnDrawGizmosSelected is called and make sure the hierarchy is valid. But it would be nice to have an call back like OnHieraachy ....

anyway afterr those lose end are done, I believe we have a start.

BTW the engine can run asynchronous or synchronous and also multithreaded and there could be multiple worlds.
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 22, 2016 4:01 pm

Is it only components with custom editor that has problems with serialization?

Regarding hierarchy changing callbacks, the closest thing I know of is OnTransformParentChanged and OnTransformChildrenChanged
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Wed Jun 22, 2016 4:15 pm

That's the thing, so far the only compound that has serialization problems is
public class NewtonWorld : MonoBehaviour

and I compared to the other, and I do no see what is special about one the other are no doing.

Imagine you set the FPS to be 60, and yo run. then you quit,
if you open the scene again the fps is back to 120.
So are all other member variables on that component.

but is more than that, if I create a new variable and I give a default value, so go that value is remember for ever, even if I changed.
for example variable m_numberOfThreads is set to 0
but each time I run the demo is set to 2, even of I delete it. I have never seen that kind of behavior.
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 23, 2016 10:41 am

now I have the compound collision going, using the same idea you first started.
I am now running a few test, and I test the profiler.
I notice something very weird in unity. It seem as if periodically Unity freezes for a few millisecond for no reason at all. at first I thought I was the newton plug in, by after have the engine practically with zero runtime allocation, I notice that is no newton it is the Camera.Render.

to see this you need to set Deep Render mode so that is shows the script functions.
is that a know issue? an if so will this cost go up as the scene become more complex?
look on the image below, notice how at some frames the Camera take 2 to 2.5 ms.
profile.png
profile.png (388.68 KiB) Viewed 8061 times
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: plugin for unity 3D

Postby Sweenie » Thu Jun 23, 2016 11:38 am

Hmm, I do get some spikes on camera.render but not 2.5 ms, more like 0.5 in extreme cases.
There is some GC allocation going on in Camera.Render, 1.2 Kb and when I run the demo scene I get allocations as well but about 150 bytes.

I haven't upgraded to 5.3.5 yet, will do that and see if I get the same values.

EDIT:
Ok, now I upgraded to 5.3.5f1 and now it's even better.
No spikes in Camera.Render and no GC Allocations in Camera.Render either.
The only thing that allocates some GC memory is something inside NewtonWorld.Update(ForceAndTorque callback)
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Thu Jun 23, 2016 1:28 pm

Oh you got 5.3.5 running? awesome.

on the 150 bytes allocation in function NewtonWorld.Update(ForceAndTorque callback)

as far as I can tell this is the creation of the callback itself.
It seems C# makes delegate method call to unmanaged functions is by making a runtion C# objects, and coping the piece of code that is the glue that the call back can call.

It seems that this ForceAndTorque callback is the same as: new ForceAndTorque callback
maybe we can cache the call back into a local private variable.

I had a similar problem with the calling GetComponets<type> that creates a run time object. I was reading some of eth C# optimization tricks, and I have a hard time determine whet
new Type crate a what the they call a Boxed Object, (for what I can see a Boxed object is an object that is crated on the stack, and then is converted to a Garbage collected objects.
for example

Vector3 = new Vector (1, 2, 3)
is on the Stack, but if another object takes a reference that is become a Memory objects and that triggers the garbage collector.
This sound to me really bad, because in the end C# order to get high performance C# code the programmer has to be aware of more internals of the memory system that you do in C++.
anyway I tried to caching the Delegate into a variable like this.
Code: Select all
private OnWorldUpdateCallback m_onWorldCallcak = new OnWorldUpdateCallback(OnWorldUpdate);

but I get this error.
2>------ Build started: Project: NewtonPlugin, Configuration: Debug Any CPU ------
2>C:\Users\jjerez\Downloads\NewtonUnityPlugin\NewtonPlugin\NewtonWorld.cs(124,80,124,93): error CS0236: A field initializer cannot reference the non-static field, method, or property 'NewtonWorld.OnWorldUpdate(float)'
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This Is why I suspect that Delegate are in fact memory allocated runtime C# objects.
I have no idea if we will be about to avoid that, but if we keep it to a minimum maybe we can live with it.

anyway I am now start adding the Joints.


My major problem now is that I have not figure out how to step in the script in debug mode, are you able to do that?
I guess one way if to recreate the project in MonoDeveloper only for debug purposes.
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 23, 2016 2:04 pm

the last 56 bytes are allocated here
Code: Select all
    public void OnApplyForceAndTorque(float timestep)
    {
        if (m_body != null)
        {
            if (m_actions.Length >= 1)
            {
//                m_forceAcc = new Vector3(0.0f, 0.0f, 0.0f);
//                m_torqueAcc = new Vector3(0.0f, 0.0f, 0.0f);
//                foreach (NewtonBodyForceAction action in m_actions)
//                {
//                    action.ApplyForceAction(this, timestep);
//                }
                Marshal.StructureToPtr(m_forceAcc, m_forceAccPtr, false);
                Marshal.StructureToPtr(m_torqueAcc, m_torqueAccPtr, false);
//                m_body.AddForceAndTorque(m_forceAccPtr, m_torqueAccPtr);
            }
        }
    }


is no in every frame, byt somehow this funtions
Marshal.StructureToPtr(m_forceAcc, m_forceAccPtr, false);
Marshal.StructureToPtr(m_torqueAcc, m_torqueAccPtr, false);

seem to uses the garbage collector to allocate memory. That's a problem, because then there is not way control memory.
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 23, 2016 2:51 pm

Oh wow, apparently Monodeveloper can open projects created by visual studio.

I was try to recreate the project and I the sultion has the same name. so I open the visual studion solution and guess what is opened and compiled fine.

I have not tested yet but if it works, maybe we can debug the pluggin from the Monodeveler IDE since I have not found a way to debug it from visual studio.
we need to find out wha up with the memory and serialization.

edit:
reading this: Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\pdb2mdb.exe
it seem that to debug form visual studio we must first run this
Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\pdb2mdb.exe

has you done that?
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 24, 2016 2:05 am

I tried it but got an error when trying convert the pdb to mdb
Sweenie
 
Posts: 503
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: plugin for unity 3D

Postby Julio Jerez » Fri Jun 24, 2016 10:11 am

well as it turn out, the tool Unity for visual studio does automatically if it finds the pdb file in the same directory as the DLL. I made the change to the project and is does generate the debug file for mono.

however it does no fixed the crash I get the moment I try to attack to the script, I get thjsio crash

Exception thrown at 0x00007FFE622E1F28 in Unity.exe: Microsoft C++ exception: ExternalProcessException at memory location 0x000000001CD7F540.
Exception thrown at 0x00007FFE622E1F28 in Unity.exe: Microsoft C++ exception: ExternalProcessException at memory location 0x000000001CD7F3F8.
[00:00:01] Enlighten: Finished 1 Bake Runtime job (0.04s execute, 0.00s integrate, 0.05s wallclock)
[00:00:01] Enlighten: Bake took 0.054460 seconds.
[00:00:01] Enlighten: Reflection Probes started.
[00:00:01] Enlighten: Finished 1 Ambient Probes job (0.00s execute, 0.00s integrate, 0.05s wallclock)
[00:00:01] Enlighten: Finished 1 Reflection Probes job (0.00s execute, 0.00s integrate, 0.06s wallclock)
Exception thrown at 0x000000001D3809C3 in Unity.exe: 0xC0000005: Access violation reading location 0x0000000001AF0000.


I try to mast some of the keyword in google to see if anyone has the same problem but answer yet.

As the plug in become more elaborated debugging with Debug.Log become unmanageable.
we need to figure out why this isn't working.


the closer info I get Is these:
https://blogs.msdn.microsoft.com/visual ... unity-1-9/
https://visualstudiogallery.msdn.micros ... f95156aba9
but does no say anything about crash of if it even works for VS 2015.
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 24, 2016 10:32 am

in this link:
https://msdn.microsoft.com/en-us/library/dn940020.aspx

they explain how to debug, but to me it does no work in tow different machines, what I fine weird is that no one seem to have the same problem.
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 24, 2016 10:45 am

Oh I figured out.
if you run Unity from visual studio, the command attach to Unity simple crash.

Instead you need to launch Unity, and then from Visual studio can attach to a Unity instance that is already running. This is the strangest set up I have ever seen.

but at least it work, maybe now we can determine some of the lose end that I see so far, like why serialization does not work as I expect to.
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 24, 2016 1:12 pm

this bug report seem to say some about custom classes and serialization.
http://bugshake.com/?p=239

I tried by that does not works either, apparently the serialization system in unity is a Major, major problem that they even refuse to fix.
https://www.reddit.com/r/Unity3D/commen ... uly_fubar/

if this is the case then that mean that using custom edit, is useless since you have to set all the parameter exact time you start the scene.
That seems a major, major show stopping bug to me.

here is the closer description of what I am seeing.
http://forum.unity3d.com/threads/serial ... st.155352/
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 2 guests

cron