We will need to make a decision how we want to handle parameter passing to and from the wrapper.dll
If we take the Body Constructor as example...
- Code: Select all
public dNewtonBody(dNewtonWorld world, SWIGTYPE_p_dFloat mass, SWIGTYPE_p_dNewtonCollision collision, SWIGTYPE_p_dFloat matrix, dNewtonBody.dBodyType m_type, dNewtonBody parent)
The first argument[world] is the world, SWIG has successfully wrapped that object and will pass the underlying object swigCPtr(which basically is a void* to the world object)
The second argument[mass], here SWIG doesn't know what a dFloat is and has automatically wrapped dFloat as a class. That won't work. We will need to "teach" SWIG to remap dFloat to a normal float which we pass by value.
The third argument[collision] will work as the first as soon as SWIG has wrapped the dNewtonCollision class.
The fourth argument[matrix], here we need to decide what to do. Newton uses float* to pass matrices but SWIG doesn't handle float* by default. It will try to wrap float* as a class.
I know of three options we have here...
1. "Teach" SWIG to remap float* (or any ptr) to IntPtr(C# void*).
Then Marshall the values to and from IntPtr in C#
2. Let float* still be float* and use Unsafe Mode in c# to work directly with pointers.(This is the fastest method)
3. Remap to a structure known by C#, in this case Unitys Matrix4x4 would work since it is a 16 floats structure. Then pass it as ref Matrix4x4 matrix.
This works like nr 1, but C# will take care of the Marshalling since he knows the size of the structure.
The fifth argument[m_type], this is just an int, no problems here.
The sixth argument[parent], same as argument 1 & 3, no problems.
So, to sum it up... We need Swig to generate one of these C# functions.
Marshalling using IntPtr
- Code: Select all
public dNewtonBody(dNewtonWorld world, float mass, dNewtonCollision collision, IntPtr matrix, dNewtonBody.dBodyType m_type, dNewtonBody parent)
Standard pointers(requires compiling plugin in Unsafe mode)
- Code: Select all
public dNewtonBody(dNewtonWorld world, float mass, dNewtonCollision collision, float* matrix, dNewtonBody.dBodyType m_type, dNewtonBody parent)
Marshalling using known structure
- Code: Select all
public dNewtonBody(dNewtonWorld world, float mass, dNewtonCollision collision, ref Matrix4x4 matrix, dNewtonBody.dBodyType m_type, dNewtonBody parent)