Unity plugin progress?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 13, 2017 4:27 pm

ha ok I have 5.5.0 for some reason when I update to 5.5.1 it tells me the download is corrupted, but that's fine it is working now.
Yesterday I started experimenting to see if we can get rid of the Marshal stuff, and I beleive is possible, check this out
Code: Select all
m_body.AddForce(new dVector(1.0f, 2.0f, 3.0f));
m_body.AddForce(m_forceAcc.x, m_forceAcc.y, m_forceAcc.z);

before we had a marshall command that copy the data to a local memory buffer, that that very ugly and now that we start adding more objects, the constructors star to get more complicatted because the lack of strong types.

so I thougth that the reason we can not pass a reference is because the object live on c# memory manager and we have to make a copy. the Mashall command make a memory buffer some where and copy that data, but in addition of the being ugly it can't be efficient either because it will trash memory in the long run.

The solution is to pass object by value, basically make a copy of the objet on eh stack and pass the entire object. C# we you make a local
my firs try I make the sing function that passed the atomic member of the vector, the first function I m passing the complete vector and it seems to work.
I will try wit the matrices and of it work we can remove the marshall for many place and use constructors that take data with actual type rather than void pointers of float pointers.
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 13, 2017 4:35 pm

I committed a version that is not using marshal for the force and torque call back.
I am now going to try removing it for body constructors, see how that goes.
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 13, 2017 4:59 pm

yes it works very well now our function looks like this

Code: Select all
    public void InitRigidBody(int sceneIndex)
    {
        m_sceneIndex = sceneIndex;
        m_collision = new NewtonBodyCollision(this);

        Matrix4x4 entMatrix = Matrix4x4.identity;
        entMatrix.SetTRS(transform.position, transform.rotation, Vector3.one);
        //IntPtr floatsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(matrix));
        //Marshal.StructureToPtr(matrix, floatsPtr, false);

        dMatrix matrix = new dMatrix();
        matrix.m_front = new dVector(entMatrix.m00, entMatrix.m10, entMatrix.m20, entMatrix.m30);
        matrix.m_up =    new dVector(entMatrix.m01, entMatrix.m11, entMatrix.m21, entMatrix.m31);
        matrix.m_right = new dVector(entMatrix.m02, entMatrix.m12, entMatrix.m22, entMatrix.m32);
        matrix.m_posit = new dVector(entMatrix.m03, entMatrix.m13, entMatrix.m23, entMatrix.m33);
        m_body = new dNewtonDynamicBody(m_world.GetWorld(), m_collision.GetShape(), matrix, m_mass);
        //Marshal.FreeHGlobal(floatsPtr);
    }


no marshall stuff needed. now all we nee to do is a helper function that copy a Matrix4x4 to a dMatrix and then the script will be more elegant and readable because the function will have strong types.
I also claim is more efficient as well
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 13, 2017 5:19 pm

ok it is more concise now, but for some reason I can not turn this function

Code: Select all
    static public dMatrix TodMatrix(Matrix4x4 entMatrix)
    {
        dMatrix matrix = new dMatrix();
        matrix.m_front = new dVector(entMatrix.m00, entMatrix.m10, entMatrix.m20, entMatrix.m30);
        matrix.m_up = new dVector(entMatrix.m01, entMatrix.m11, entMatrix.m21, entMatrix.m31);
        matrix.m_right = new dVector(entMatrix.m02, entMatrix.m12, entMatrix.m22, entMatrix.m32);
        matrix.m_posit = new dVector(entMatrix.m03, entMatrix.m13, entMatrix.m23, entMatrix.m33);
        return matrix;
    }


into a global utility function that we can call form every where, do you know to do that.
the second part is in the return data, we are return point and using marshal to read the data, by I think if we return a copy of the object, then the object will be on the stack and should be any problem reading as a legitimate c# class.

I mean stuff like this

void* GetPosition();

could be converted to

dVector GetPosition();
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 13, 2017 5:23 pm

It looks cleaner indeed, but I wonder how this affects the garbage collector.
When you create the dVector class in C# it creates a native dVector instance instead and keeps a reference to it inside the managed dVector class.
Question is, does c# dispose of it immediately as it goes out of scope or is it garbage collected later on?

In most C# engines i've seen, types such as Vectors, Matrices etc, usually never are defined as classes since they are reference types and therefore garbage collected. They are almost always defined as structs, since structs are value types in C#

It works of course, but most C# coders usually avoid creating lots of classes in intensive loops, such as renderloops etc. to avoid stressing the garbage collector.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 13, 2017 6:43 pm

yes you may be right, but those function are no used in the update loop they are all initialization routines. the code now looks like this :D
Code: Select all
    public void InitRigidBody(int sceneIndex)
    {
        m_sceneIndex = sceneIndex;
        m_collision = new NewtonBodyCollision(this);
        m_body = new dNewtonDynamicBody(m_world.GetWorld(), m_collision.GetShape(), Utils.ToMatrix(transform.position, transform.rotation), m_mass);
    }


you are definitely right, when returning an object from the low level wrapper, it does in fact call alloc to create a piece of memory, worse I did not see where it frees it, so it does not work the other way. Is better to return pointer and primitive types.
but at least this is more clear.
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 13, 2017 6:51 pm

But what about the force callback? That would be called every frame.

I might worry for nothing though, i'm not an expert regarding this. :)

Maybe you seen this before but it's a nice paper on creating a wrapper.
Especially since the paper is about wrapping a physics engine ;)
https://comserv.cs.ut.ee/home/files/tra ... 703F9A9B7F
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Mon Feb 13, 2017 7:20 pm

in the force call back I had it passing float x, y, z then changed to new dVector3 (x, y, z)
you maybe right we should revert it back to passing the floats x, y, z,

I am not changing more function other than the joint constructors because is tedious and error prompt to do all that mashall stuff, I made several errors already because different joint build the pin matrix sligtly different.

NO I had no seem that paper, but let me guess a PhysX or Bullet papel?
we should just continue re shaping our effort the best way we could.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 14, 2017 2:45 am

I had no seem that paper, but let me guess a PhysX or Bullet papel?
we should just continue re shaping our effort the best way we could.


well, the paper doesn't really talk about the engine, it's focused on c++ & .NET and how to pass data the most efficient way back and forth.

But you're right, what we have now is working well and we could optimize things later if we need to. :)
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 14, 2017 11:29 am

I added the slider and slider actuator. I am trying to put the joint into the joint demo.

here is a question, in unity if you attach a game object as a child of another game object,
then how do you select the parent without affection the children?
I am sure there most be a way, but I do not see how.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 14, 2017 12:46 pm

If you select the parent Unity will render the orange border on the parent and all children.
Likewise OnDrawGizmosSelected will be called for the parent and all children as well.

While you can make a rigid body child of another I think the recommendation is to keep GameObjects with rigid bodies as siblings instead. It's easier to edit the parent if you don't see the child's debug render at the same time.

So instead of this hieararchy...

HingeMotor(Gameobject with rigid body)
|-->child(Gameobject with rigid body)

Do this instead...

HingeMotor(empty gameobject)
|-->Object A(Gameobject with rigid body)
|-->Object B(Gameobject with rigid body)

That way you can edit Object A and Object B separetely, but you can also select the parent Game object to move them both and also see both child objects debug rendering.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 14, 2017 1:24 pm

yes I know that, but them how do they form rag dolls?
is hard to make something like that all in global space.

there has to be a way to select a single object, I refuse to believe that a toll like Unity can goes for years and year and no one has ever asked for such basic operation.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 14, 2017 1:33 pm

O was think that a nice way to differential a model for another is by having a root object as representative of the model and every articulate part as a child in a hierarchy.

of Couse we can do that by ether using a dummy root, or by using the root body and everything else a level one child, but both solution seem solution in search of a problem that soudl no be ther in the first place.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Unity plugin progress?

Postby Sweenie » Tue Feb 14, 2017 2:04 pm

Well, you can have rigid bodies in a hierarchy and for ragdolls it would be necessary.

When you select the parent, the gizmos of the children are rendered, but it's still only the parent that is selected.
It looks like all children are selected but it's only the parent.
Look in the upper right corner, the inspector tells you which object you have selected.

Also, don't forget the transform tool button. The little button on the top of the screen that says, "Center". Click it to change it to "Pivot".
That will place the transform tool directly on the parent instead of the center of the parent and it's childrens combined bounding boxes.

I guess we could make a similar tool like Unity have to make it easier as well.
https://www.youtube.com/watch?v=Hw1UK85sARc

Is it the rendering of the childrens gizmos you don't like or do you mean you want to be able to move the parent without the children moving?

There is a way to prevent child gizmos from rendering, but that is only available to the UnityEditor plugin. It might work moving the OnDrawGizmosSelected over to NewtonEditorPlugin.
If so, we could use this code at the top of OnDrawGizmosSelected
Code: Select all
if(Selection.ActiveGameObject != transform.GameObject)
  return;
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: Unity plugin progress?

Postby Julio Jerez » Tue Feb 14, 2017 5:02 pm

well is ok, I can managed.
I see you make the robot nicer, I added the slider actuator, maybe you can elaborate the arm more now.
the challenge will be how to make some of these
https://www.bing.com/images/search?q=ro ... 522&ch=846

we now nee to start learning the lingo of some of the mechanical device that are commonly used for designing mechanism, the most poplar is the so called Four-bar linkage
https://en.wikipedia.org/wiki/Four-bar_linkage

this is and arrange met of link design for a point to fallow a path that could be a straight line or an arch. you can get crazy trajectories bi the simpler on is an almost straight line.
The mathematic is complex and you only learn in graduate courses in Analytical Mechanic.
but I believe now we can emulate some of them by simply Putin stuff together mimicking designed that are already common like some of the grippers in the top link.
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 19 guests

cron