diff --git a/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs b/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs index 00a7998..b437fca 100644 --- a/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs +++ b/Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs @@ -74,10 +74,12 @@ public void Reset() public class AlignedNodeArray : IList { private IntPtr _native; + private Dictionary _nodeObjects; internal AlignedNodeArray(IntPtr native) { _native = native; + _nodeObjects = new Dictionary(); } public int IndexOf(Node item) @@ -103,7 +105,17 @@ public Node this[int index] { throw new ArgumentOutOfRangeException("index"); } - return new Node(btAlignedSoftBodyNodeArray_at(_native, index)); + + IntPtr nodePtr = btAlignedSoftBodyNodeArray_at(_native, index); + long nodePtrInt = nodePtr.ToInt64(); + Node node; + if (!_nodeObjects.TryGetValue(nodePtrInt, out node)) + { + node = new Node(nodePtr); + _nodeObjects.Add(nodePtrInt, node); + } + + return node; } set { @@ -111,6 +123,15 @@ public Node this[int index] } } + public IntPtr GetNodePointer(int index) + { + if ((uint)index >= (uint)Count) + { + throw new ArgumentOutOfRangeException("index"); + } + return btAlignedSoftBodyNodeArray_at(_native, index); + } + public void Add(Node item) { btAlignedSoftBodyNodeArray_push_back(_native, item._native); diff --git a/Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs b/Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs index 8b93d81..e2d5b69 100644 --- a/Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs +++ b/Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs @@ -2335,13 +2335,13 @@ public Vector3 Velocity [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] static extern IntPtr btSoftBody_Node_getLeaf(IntPtr obj); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] - static extern void btSoftBody_Node_getN(IntPtr obj, [Out] out Vector3 value); + public static extern void btSoftBody_Node_getN(IntPtr obj, [Out] out Vector3 value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] static extern void btSoftBody_Node_getQ(IntPtr obj, [Out] out Vector3 value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] static extern void btSoftBody_Node_getV(IntPtr obj, [Out] out Vector3 value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] - static extern void btSoftBody_Node_getX(IntPtr obj, [Out] out Vector3 value); + public static extern void btSoftBody_Node_getX(IntPtr obj, [Out] out Vector3 value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] static extern void btSoftBody_Node_setArea(IntPtr obj, float value); [DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity] diff --git a/Scripts/SoftBody/BSoftBody.cs b/Scripts/SoftBody/BSoftBody.cs index 53be399..98d1f85 100644 --- a/Scripts/SoftBody/BSoftBody.cs +++ b/Scripts/SoftBody/BSoftBody.cs @@ -96,18 +96,24 @@ public void DumpDataFromBullet() if (isInWorld) { SoftBody m_BSoftBody = (SoftBody)m_collisionObject; - if (verts.Length != m_BSoftBody.Nodes.Count) + int nodeCount = m_BSoftBody.Nodes.Count; + if (verts.Length != nodeCount) { - verts = new Vector3[m_BSoftBody.Nodes.Count]; + verts = new Vector3[nodeCount]; } if (norms.Length != verts.Length) { - norms = new Vector3[m_BSoftBody.Nodes.Count]; + norms = new Vector3[nodeCount]; } - for (int i = 0; i < m_BSoftBody.Nodes.Count; i++) + for (int i = 0; i < nodeCount; i++) { - verts[i] = m_BSoftBody.Nodes[i].Position.ToUnity(); - norms[i] = m_BSoftBody.Nodes[i].Normal.ToUnity(); + BulletSharp.Math.Vector3 pos; + Node.btSoftBody_Node_getX(m_BSoftBody.Nodes.GetNodePointer(i), out pos); + verts[i] = pos.ToUnity(); + + BulletSharp.Math.Vector3 normal; + Node.btSoftBody_Node_getN(m_BSoftBody.Nodes.GetNodePointer(i), out normal); + norms[i] = normal.ToUnity(); } } }