Newton .Net Wrapper for 2.0 and MDX

Share with us how are you using the powerrrr of the force

Moderator: Alain

Postby Mindcrime » Mon Jul 18, 2005 11:08 pm

pizzooid wrote:Hmm... but why should the function get garbage collected, it has still a link from NObject, and only stuff that is not linked from anywhere gets collected

or do you mean, that because the garbage collector has been called the function has moved its point on the heap and the pointer points on his old location?

i don't think that this is the case either

or did i completely misunderstand you?

thx for the fast response


your code example above only does a new() on the delegate and passes that as a parameter:
Code: Select all
Newton.BodySetForceAndTorqueCallback(obj.body,
            new SetForceAndTorqueCB(obj.ApplyForceAndTorque));


i was basing my response off of that. if you've done something differently, then its possible there is something else wrong.

but for the above code, the delegate object is instatiated and immediately passed to the function, and then since there are no references to it, it eventually gets garbage collected.

if you have updated code go ahead and post that. there could be something else going on.
Mindcrime
 
Posts: 3
Joined: Mon Jun 06, 2005 9:22 pm

Postby SnprBoB86 » Tue Jul 19, 2005 12:09 am

Again, I suggest you see my code:
http://newtondynamics.com/forum/viewtopic.php?t=1264

The trick I employ is to use unmanaged function pointers as callbacks which invoke events on through the user data which is a gcroot. The user data can then be wrapped. See body.h
SnprBoB86
 
Posts: 26
Joined: Mon Jan 24, 2005 5:53 pm

Postby pizzooid » Tue Jul 19, 2005 4:10 pm

Oh no.. Mindcrime, you are so right ^^ I oversaw, that the delegate is the object that gets collected and not the function.

I now solved the problem like this:

Code: Select all
class NObject
    {
        private int m_world;
        private int body;
        private int collision;

        public Vector3 omega = new Vector3();
        public Vector3 force = new Vector3();

       
        // This event is fired when the SetForecAndTorqueCB is fired for this object
        public event SetForceAndTorqueCB OnApplyForceAndTorque;

        /// <summary>
        /// Creates a Box as NObject
        /// </summary>
        /// <param name="world"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        /// <param name="transform"></param>
        /// <returns></returns>
        public static NObject CreateBox(Device device, int world, float x,
        float y, float z, Matrix transform)
        {
            // temporary holder for the matrix
            NObject obj = new NObject();

            //Create a referenc to the object for the Callback
            // |
            // ---> Create the Directx Mesh

            obj.mesh = Mesh.Box(device, x, y, z);

            // |
            // ---> Create the Newton Object
            // save the world for obj object
            obj.m_world = world;
            // Create the Collision holder
            obj.trans = transform;
            obj.collision = Newton.CreateBox( world, x, y, z, null);
            // Create the Rigid body for the collision
            obj.body = Newton.CreateBody(world, obj.collision);
            // Get rid of the collision
            Newton.ReleaseCollision(world, obj.collision);
            // Set Mass and interia
            Newton.BodySetMassMatrix(obj.body, 1f, 1f, 1f, 1f);
            // Set the transformation matrix
            obj.mat = transform;
            Newton.BodySetMatrix(obj.body, ref obj.mat);

            obj.SetupHandlers();

            return obj;
        }

#region eventSetup
        /// <summary>
        /// Sets up the events
        /// </summary>
        private void SetupHandlers()
        {
            OnApplyForceAndTorqueCB = new SetForceAndTorqueCB
            (ApplyForceAndTorque);
            Newton.BodySetForceAndTorqueCallback(body,
            OnApplyForceAndTorqueCB);
            OnApplyForceAndTorque += new SetForceAndTorqueCB
            (mApplyForceAndTorque);
        }
        private SetForceAndTorqueCB OnApplyForceAndTorqueCB;
        private void ApplyForceAndTorque(int pBody)
        {
            OnApplyForceAndTorque(pBody);
        }
#endregion

        /// <summary>
        /// Applies force and torgue to the pBody
        /// </summary>
        /// <param name="pBody"></param>
        private void mApplyForceAndTorque(int pBody)
        {
            Newton.BodySetOmega(pBody, ref omega);
            Newton.BodySetForce(pBody, ref force);
        }
}


with this i dont need a static delegate, because each object stores its own one
pizzooid
 
Posts: 5
Joined: Sun Jul 17, 2005 8:08 pm

Postby Rune Hunter » Sat Jul 23, 2005 5:55 pm

The wrapper looks nice but how about a little sample code of this wrapper in work?

Maybe just a couple of cubes that fall from the sky and hit ground (a square block)
Rune Hunter
 
Posts: 8
Joined: Fri Jul 22, 2005 12:07 am
Location: USA

Postby Haddd » Sun Jul 24, 2005 1:05 pm

Actually i have a full physics class that controls the behaviour of the physics objects. So i can not give you any example, because it's fully integrated into our engine. But i'm using the wrapper to access newton of course, so simply get some example and translate it into C# and Managed DX. If you have any questions, i will help you :lol:
Haddd
 
Posts: 35
Joined: Wed Mar 16, 2005 5:06 am

Postby Rune Hunter » Mon Jul 25, 2005 6:24 pm

Well I started to make "my own" wrapper in C# 2.0. Basicly copied yours on teh first or so post to see if it would work. And well I get stuck here and there so I am giving c++ anoutehr try.

But thanks anyways.
Rune Hunter
 
Posts: 8
Joined: Fri Jul 22, 2005 12:07 am
Location: USA

Postby alphabeta » Sat Sep 24, 2005 11:02 pm

Hate to bump an old thread, but I was curious whether anyone has had success wrapping the usermesh methods and structures?

Seems a bit complicated to me, with the pointer members and all
alphabeta
 
Posts: 2
Joined: Mon Mar 07, 2005 12:04 am

Postby SnprBoB86 » Sun Sep 25, 2005 11:04 am

I haven't attempted it yet; real life took over, my wrapper has progressed very little from what is published and I plan to rewrite a lot of it.

However, the UserMesh stuff doesn't seem too difficult. My code (which should still be up for download) has enough to help you figure it out with MC++. The trick probably lies in the unmanaged gcroot<T> object as well as the unmanaged to managed callback tricks I employed. There may be a better way to do this with the Marshall class (like I know there has got to be a better way to do the float[] to float* conversion without pin pointers).

I still plan on finishing this wrapper.... some day... maybe... :)
SnprBoB86
 
Posts: 26
Joined: Mon Jan 24, 2005 5:53 pm

Postby pizzooid » Sun Sep 25, 2005 3:51 pm

@alphabeta:

I tryed to write also a custom mesh wrapper, but it did not propperly work (my objects did not bounce properly...), and then I went to my big (2months) trip to brasil

but since i will return by october i'll give it another try

cu pietro
pizzooid
 
Posts: 5
Joined: Sun Jul 17, 2005 8:08 pm

Postby pizzooid » Sun Nov 06, 2005 1:27 pm

I tried to enhance my newton wrapper again, but still the user mesh collision does not work correctly:

I believe that it is not a wrapper specific problem but I am doing something wrong with the Newton-Setup



NObject only holds a mesh and the body object
Code: Select all
public static NObject FromMesh(Device device, int world, string Source, Matrix transform)
        {
            [...DIRECTX CODE]

            // Copy the vertex data into a simple vector array
            Vector3[] verts = new Vector3[lockedVerts.Length];
            for (int i = 0; i < lockedVerts.Length; ++i)
            {
                // Is this the right method verts[i] = lockedVerts[i] or should I use
                //verts[i] = lockedVerts[lockedVerts.Length-1- i].Position;
                 verts[i] = lockedVerts[i].Position;
            }


            obj.mesh.UnlockVertexBuffer();


            // |
            // ---> Create the Newton Object ----------------------
            // save the world for obj object
            obj.m_world = world;
            // Create the Collision holder
            obj.mat = Matrix.Identity;
            obj.collision = Newton.CreateConvexHull(world, verts.Length, verts, obj.mesh.NumberVertices, ref obj.mat);
            // Create the Rigid body for the collision
            obj.body = Newton.CreateBody(world, obj.collision);
           
            // Calculate and Set Mass and inertia tensor
            Vector3 inertia = new Vector3();
            for (int i = 0; i < lockedVerts.Length; ++i)
            {
                inertia.X = lockedVerts[i].Position.Y * lockedVerts[i].Position.Y
                 + lockedVerts[i].Position.Z * lockedVerts[i].Position.Z;
            }
            for (int i = 0; i < lockedVerts.Length; ++i)
            {
                inertia.Y = lockedVerts[i].Position.X * lockedVerts[i].Position.X
                 + lockedVerts[i].Position.Z * lockedVerts[i].Position.Z;
            }
            for (int i = 0; i < lockedVerts.Length; ++i)
            {
                inertia.Z = lockedVerts[i].Position.Y * lockedVerts[i].Position.Y
                + lockedVerts[i].Position.X * lockedVerts[i].Position.X;
            }
           
            Newton.BodySetMassMatrix(obj.body, 2f, inertia.X, inertia.Y, inertia.Z);

            // Set the transformation matrix
            obj.mat = transform;
            Newton.BodySetMatrix(obj.body, ref obj.mat);

            obj.SetupHandlers(); // SEE BELOW

            // Get rid of the collision
            Newton.ReleaseCollision(world, obj.collision);

            return obj;
        }


Code: Select all
private void SetupHandlers()
        {
            OnApplyForceAndTorqueCB = new SetForceAndTorqueCB(ApplyForceAndTorque);
            Newton.BodySetForceAndTorqueCallback(body, OnApplyForceAndTorqueCB);
            OnApplyForceAndTorque += new SetForceAndTorqueCB(mApplyForceAndTorque);
        }
pizzooid
 
Posts: 5
Joined: Sun Jul 17, 2005 8:08 pm

Re: Newton .Net Wrapper for 2.0 and MDX

Postby Aldeminor » Fri Jul 23, 2010 3:07 pm

There are exists wrapper for Newton double-precision dll?
Aldeminor
 
Posts: 31
Joined: Tue Jun 29, 2010 6:37 am

Previous

Return to User Gallery

Who is online

Users browsing this forum: No registered users and 9 guests