private void DetectSoftSoft(SoftBody body1, SoftBody body2) { List <int> my = potentialTriangleLists.GetNew(); List <int> other = potentialTriangleLists.GetNew(); body1.dynamicTree.Query(other, my, body2.dynamicTree); for (int i = 0; i < other.Count; i++) { SoftBody.Triangle myTriangle = body1.dynamicTree.GetUserData(my[i]); SoftBody.Triangle otherTriangle = body2.dynamicTree.GetUserData(other[i]); JVector point, normal; float penetration; bool result; result = XenoCollide.Detect(myTriangle, otherTriangle, ref JMatrix.InternalIdentity, ref JMatrix.InternalIdentity, ref JVector.InternalZero, ref JVector.InternalZero, out point, out normal, out penetration); if (result) { int minIndexMy = FindNearestTrianglePoint(body1, my[i], ref point); int minIndexOther = FindNearestTrianglePoint(body2, other[i], ref point); RaiseCollisionDetected(body1.VertexBodies[minIndexMy], body2.VertexBodies[minIndexOther], ref point, ref point, ref normal, penetration); } } my.Clear(); other.Clear(); potentialTriangleLists.GiveBack(my); potentialTriangleLists.GiveBack(other); }
private void BuildVertices() { //Console.WriteLine("clothBuilt"); vertices = new VertexPositionNormalTexture[cloth.VertexBodies.Count]; indices = new int[cloth.Triangles.Count * 3]; for (int i = 0; i < cloth.Triangles.Count; i++) { SoftBody.Triangle t = cloth.Triangles[i]; indices[3 * i + 0] = t.Indices.I0; indices[3 * i + 1] = t.Indices.I1; indices[3 * i + 2] = t.Indices.I2; /*t.Indices.I0 = indices[3 * i + 2]; * t.Indices.I1 = indices[3 * i + 0]; * t.Indices.I2 = indices[3 * i + 1];*/ } UpdatePositionAndNormal(); int sqrt = (int)Math.Sqrt(vertices.Length); for (int i = 0; i < sqrt; i++) { for (int e = 0; e < sqrt; e++) { vertices[i * sqrt + e].TextureCoordinate = new Vector2(1.0f / (float)(sqrt - 1) * (float)i, 1.0f / (float)(sqrt - 1) * (float)e); } } //vertices[0].Position = Vector3.Forward + Vector3.Left; //vertices[0].TextureCoordinate = new Vector2(0.0f, 1.0f); //vertices[1].Position = Vector3.Backward + Vector3.Left; //vertices[1].TextureCoordinate = new Vector2(0.0f, 0.0f); //vertices[2].Position = Vector3.Forward + Vector3.Right; //vertices[2].TextureCoordinate = new Vector2(1.0f, 1.0f); //vertices[3].Position = Vector3.Backward + Vector3.Right; //vertices[3].TextureCoordinate = new Vector2(1.0f, 0.0f); //for (int i = 0; i < vertices.Length; i++) //{ // vertices[i].Normal = Vector3.Up; // vertices[i].Position *= size; // vertices[i].TextureCoordinate *= size; //} //indices[5] = 0; indices[4] = 1; indices[3] = 2; //indices[2] = 2; indices[1] = 1; indices[0] = 3; }
public static int FindNearestTrianglePoint(SoftBody sb, int id, ref JVector point) { SoftBody.Triangle triangle = sb.dynamicTree.GetUserData(id); JVector p; p = sb.points[triangle.indices.I0].position; JVector.Subtract(ref p, ref point, out p); float length0 = p.LengthSquared(); p = sb.points[triangle.indices.I1].position; JVector.Subtract(ref p, ref point, out p); float length1 = p.LengthSquared(); p = sb.points[triangle.indices.I2].position; JVector.Subtract(ref p, ref point, out p); float length2 = p.LengthSquared(); if (length0 < length1) { if (length0 < length2) { return(triangle.indices.I0); } else { return(triangle.indices.I2); } } else { if (length1 < length2) { return(triangle.indices.I1); } else { return(triangle.indices.I2); } } }
private void DetectSoftRigid(RigidBody rigidBody, SoftBody softBody) { if (rigidBody.Shape is Multishape) { Multishape ms = (rigidBody.Shape as Multishape); ms = ms.RequestWorkingClone(); JBBox transformedBoundingBox = softBody.BoundingBox; transformedBoundingBox.InverseTransform(ref rigidBody.position, ref rigidBody.orientation); int msLength = ms.Prepare(ref transformedBoundingBox); List <int> detected = potentialTriangleLists.GetNew(); softBody.dynamicTree.Query(detected, ref rigidBody.boundingBox); foreach (int i in detected) { SoftBody.Triangle t = softBody.dynamicTree.GetUserData(i); JVector point, normal; float penetration; bool result; for (int e = 0; e < msLength; e++) { ms.SetCurrentShape(e); result = XenoCollide.Detect(ms, t, ref rigidBody.orientation, ref JMatrix.InternalIdentity, ref rigidBody.position, ref JVector.InternalZero, out point, out normal, out penetration); if (result) { int minIndex = FindNearestTrianglePoint(softBody, i, ref point); if (this.RaisePassedNarrowphase(rigidBody, softBody.points[minIndex], ref point, ref normal, penetration)) { RaiseCollisionDetected(rigidBody, softBody.points[minIndex], ref point, ref point, ref normal, penetration); } } } } detected.Clear(); potentialTriangleLists.GiveBack(detected); ms.ReturnWorkingClone(); } else { List <int> detected = potentialTriangleLists.GetNew(); softBody.dynamicTree.Query(detected, ref rigidBody.boundingBox); foreach (int i in detected) { SoftBody.Triangle t = softBody.dynamicTree.GetUserData(i); JVector point, normal; float penetration; bool result; result = XenoCollide.Detect(rigidBody.Shape, t, ref rigidBody.orientation, ref JMatrix.InternalIdentity, ref rigidBody.position, ref JVector.InternalZero, out point, out normal, out penetration); if (result) { int minIndex = FindNearestTrianglePoint(softBody, i, ref point); if (this.RaisePassedNarrowphase(rigidBody, softBody.points[minIndex], ref point, ref normal, penetration)) { RaiseCollisionDetected(rigidBody, softBody.points[minIndex], ref point, ref point, ref normal, penetration); } } } detected.Clear(); potentialTriangleLists.GiveBack(detected); } }