public void Test1() { m_NewtonContactBegin = new Newton.NewtonContactBegin(InvokeContactBegin); }
public void SetCollisionCallback(CMaterial pId0, CMaterial pId1, CMaterialPhysicsSpecialEffect pSpecialEffect, EventHandler<CContactBeginEventArgs> pContactBegin, EventHandler<CContactProcessEventArgs> pContactProcess, EventHandler<CContactEndEventArgs> pContactEnd) { IntPtr aKey = IntPtr.Zero; if (pSpecialEffect != null) { aKey = (IntPtr)pSpecialEffect.GetHashCode(); CHashTables.MaterialsUserData.Add(aKey, pSpecialEffect); } // Takes in a delegate, stores it, but then hooks m_ContactBegin = pContactBegin; m_NewtonContactBegin = new Newton.NewtonContactBegin(InvokeContactBegin); m_ContactProcess = pContactProcess; m_NewtonContactProcess = new Newton.NewtonContactProcess(InvokeContactProcess); m_ContactEnd = pContactEnd; m_NewtonContactEnd = new Newton.NewtonContactEnd(InvokeContactEnd); Newton.NewtonMaterialSetCollisionCallback(m_World.Handle, pId0.ID, pId1.ID, aKey, m_NewtonContactBegin, m_NewtonContactProcess, m_NewtonContactEnd); }
public void SetCollisionCallback(CMaterial pId0, CMaterial pId1, CMaterialPhysicsSpecialEffect pSpecialEffect, EventHandler <CContactBeginEventArgs> pContactBegin, EventHandler <CContactProcessEventArgs> pContactProcess, EventHandler <CContactEndEventArgs> pContactEnd) { IntPtr aKey = IntPtr.Zero; if (pSpecialEffect != null) { aKey = (IntPtr)pSpecialEffect.GetHashCode(); CHashTables.MaterialsUserData.Add(aKey, pSpecialEffect); } // Takes in a delegate, stores it, but then hooks m_ContactBegin = pContactBegin; m_NewtonContactBegin = new Newton.NewtonContactBegin(InvokeContactBegin); m_ContactProcess = pContactProcess; m_NewtonContactProcess = new Newton.NewtonContactProcess(InvokeContactProcess); m_ContactEnd = pContactEnd; m_NewtonContactEnd = new Newton.NewtonContactEnd(InvokeContactEnd); Newton.NewtonMaterialSetCollisionCallback(m_World.Handle, pId0.ID, pId1.ID, aKey, m_NewtonContactBegin, m_NewtonContactProcess, m_NewtonContactEnd); }
/// <summary> /// Creates an instance of the Newton physics simulation engine. /// </summary> public NewtonPhysics() { gravity = 9.8f; gravityDir = Vector3Helper.Get(0, -1, 0); useBoundingBox = false; worldSize = new BoundingBox(Vector3.One * -100, Vector3.One * 100); objectIDs = new Dictionary<IPhysicsObject, IntPtr>(); scaleTable = new Dictionary<IntPtr, Vector3>(); reverseIDs = new Dictionary<IntPtr, IPhysicsObject>(); collisionCallbacks = new Dictionary<CollisionPair, CollisionCallback>(); materialIDs = new Dictionary<String, int>(); materialPairs = new List<string>(); materials = new Dictionary<IntPtr, NewtonMaterial>(); joints = new Dictionary<IntPtr, Joint>(); jointsToBeAdded = new List<Joint>(); pickedObjects = new List<PickedObject>(); forces = new Dictionary<IntPtr, Stack<Vector3>>(); torques = new Dictionary<IntPtr, Stack<Vector3>>(); setTransformMap = new Dictionary<IPhysicsObject,Newton.NewtonSetTransform>(); applyForceMap = new Dictionary<IPhysicsObject,Newton.NewtonApplyForceAndTorque>(); treeCollisionMap = new Dictionary<IPhysicsObject,Newton.NewtonTreeCollision>(); collisionMesh = new List<List<Vector3>>(); numSubSteps = 1; pauseSimulation = false; simulationTimeStep = 0.016f; transformCallback = delegate(IntPtr body, float[] pMatrix) { if (!reverseIDs.ContainsKey(body) || reverseIDs[body].Manipulatable) return; ShapeType shape = reverseIDs[body].Shape; tmpVec1 = scaleTable[body]; if ((shape == ShapeType.Box) || (shape == ShapeType.Sphere) || (shape == ShapeType.ConvexHull) || (shape == ShapeType.TriangleMesh)) { Matrix.CreateScale(ref tmpVec1, out tmpMat1); MatrixHelper.FloatsToMatrix(pMatrix, out tmpMat2); Matrix.Multiply(ref tmpMat1, ref tmpMat2, out tmpMat1); reverseIDs[body].PhysicsWorldTransform = tmpMat1; } else { Matrix.CreateScale(ref tmpVec1, out tmpMat1); Matrix.CreateRotationZ(-MathHelper.PiOver2, out tmpMat2); Matrix.Multiply(ref tmpMat1, ref tmpMat2, out tmpMat1); MatrixHelper.FloatsToMatrix(pMatrix, out tmpMat2); Matrix.Multiply(ref tmpMat1, ref tmpMat2, out tmpMat1); reverseIDs[body].PhysicsWorldTransform = tmpMat1; } }; applyForceAndTorqueCallback = delegate(IntPtr pNewtonBody) { if (!reverseIDs.ContainsKey(pNewtonBody)) return; float[] force = new float[3]; force[0] = force[1] = force[2] = 0; if (reverseIDs[pNewtonBody].ApplyGravity) { float Ixx = 0, Iyy = 0, Izz = 0, mass = 0; Newton.NewtonBodyGetMassMatrix(pNewtonBody, ref mass, ref Ixx, ref Iyy, ref Izz); Vector3.Multiply(ref gravityDir, gravity * mass, out tmpVec1); force = Vector3Helper.ToFloats(ref tmpVec1); } Vector3 tmp; if (forces.ContainsKey(pNewtonBody) && forces[pNewtonBody].Count > 0) { Stack<Vector3> _forces = forces[pNewtonBody]; while (_forces.Count > 0) { tmp = _forces.Pop(); force[0] += tmp.X; force[1] += tmp.Y; force[2] += tmp.Z; } } Newton.NewtonBodyAddForce(pNewtonBody, force); if (torques.ContainsKey(pNewtonBody) && torques[pNewtonBody].Count > 0) { float[] torque = new float[3]; torque[0] = torque[1] = torque[2] = 0; Stack<Vector3> _torques = torques[pNewtonBody]; while (_torques.Count > 0) { tmp = _torques.Pop(); torque[0] += tmp.X; torque[1] += tmp.Y; torque[2] += tmp.Z; } Newton.NewtonBodyAddTorque(pNewtonBody, torque); } }; rayCastFilterCallback = delegate(IntPtr pNewtonBody, float[] pHitNormal, int pCollisionID, IntPtr pUserData, float pIntersetParam) { if (!reverseIDs.ContainsKey(pNewtonBody)) return -1; IPhysicsObject physObj = reverseIDs[pNewtonBody]; if (physObj.Pickable) { PickedObject pickedObject = new PickedObject(physObj, pIntersetParam); pickedObjects.Add(pickedObject); } return pIntersetParam; }; materialContactBeginCallback = delegate(IntPtr pMaterial, IntPtr pNewtonBody0, IntPtr pNewtonBody1) { if (!materials.ContainsKey(Newton.NewtonMaterialGetMaterialPairUserData(pMaterial))) return 1; NewtonMaterial physMat = materials[Newton.NewtonMaterialGetMaterialPairUserData(pMaterial)]; if (physMat.ContactBeginCallback == null) return 1; if (reverseIDs.ContainsKey(pNewtonBody0) && reverseIDs.ContainsKey(pNewtonBody1)) physMat.ContactBeginCallback(reverseIDs[pNewtonBody0], reverseIDs[pNewtonBody1]); return 1; }; materialContactProcessCallback = delegate(IntPtr pMaterial, IntPtr pContact) { if (!materials.ContainsKey(Newton.NewtonMaterialGetMaterialPairUserData(pMaterial))) return 1; NewtonMaterial physMat = materials[Newton.NewtonMaterialGetMaterialPairUserData(pMaterial)]; if (physMat.ContactProcessCallback == null) return 1; float contactNormalSpeed = Newton.NewtonMaterialGetContactNormalSpeed(pMaterial, pContact); float[] contactPos = new float[3]; float[] contactNormal = new float[3]; Newton.NewtonMaterialGetContactPositionAndNormal(pMaterial, contactPos, contactNormal); float colObj1ContactTangentSpeed = Newton.NewtonMaterialGetContactTangentSpeed(pMaterial, pContact, 0); float colObj2ContactTangentSpeed = Newton.NewtonMaterialGetContactTangentSpeed(pMaterial, pContact, 1); float[] colObj1ContactTangentDir = new float[3]; float[] colObj2ContactTangentDir = new float[3]; Newton.NewtonMaterialGetContactTangentDirections(pMaterial, colObj1ContactTangentDir, colObj2ContactTangentDir); physMat.ContactProcessCallback(Vector3Helper.Get(contactPos[0], contactPos[1], contactPos[2]), Vector3Helper.Get(contactNormal[0], contactNormal[1], contactNormal[2]), contactNormalSpeed, colObj1ContactTangentSpeed, colObj2ContactTangentSpeed, Vector3Helper.Get(colObj1ContactTangentDir[0], colObj1ContactTangentDir[1], colObj1ContactTangentDir[2]), Vector3Helper.Get(colObj2ContactTangentDir[0], colObj2ContactTangentDir[1], colObj2ContactTangentDir[2])); return 1; }; materialContactEndCallback = delegate(IntPtr pMaterial) { if (!materials.ContainsKey(Newton.NewtonMaterialGetMaterialPairUserData(pMaterial))) return; NewtonMaterial physMat = materials[Newton.NewtonMaterialGetMaterialPairUserData(pMaterial)]; if (physMat.ContactEndCallback == null) return; physMat.ContactEndCallback(); }; collisionIterator = delegate(IntPtr pNewtonBody, int vertexCount, float[] faceArray, int faceID) { List<Vector3> verts = new List<Vector3>(); int max = faceArray.Length / 3; if (vertexCount > max) Log.Write("More faceArray needed for drawing the collision mesh: " + vertexCount); for (int i = 0; i < vertexCount && i < max; i++) verts.Add(Vector3Helper.Get(faceArray[i * 3], faceArray[i * 3 + 1], faceArray[i * 3 + 2])); collisionMesh.Add(verts); }; }