예제 #1
0
        public void DestroyEdgeMesh(ObiEdgeMeshHandle handle)
        {
            if (handle != null && handle.isValid && handle.index < handles.Count)
            {
                var header = headers[handle.index];

                // Update headers:
                for (int i = 0; i < headers.count; ++i)
                {
                    var h = headers[i];
                    if (h.firstEdge > header.firstEdge)
                    {
                        h.firstNode   -= header.nodeCount;
                        h.firstEdge   -= header.edgeCount;
                        h.firstVertex -= header.vertexCount;
                        headers[i]     = h;
                    }
                }

                // update handles:
                foreach (var pair in handles)
                {
                    if (pair.Value.index > handle.index)
                    {
                        pair.Value.index--;
                    }
                }

                // Remove nodes, triangles and vertices
                bihNodes.RemoveRange(header.firstNode, header.nodeCount);
                edges.RemoveRange(header.firstEdge, header.edgeCount);
                vertices.RemoveRange(header.firstVertex, header.vertexCount);

                // remove header:
                headers.RemoveAt(handle.index);

                // remove the collider from the dictionary:
                handles.Remove(handle.owner);

                // Invalidate our handle:
                handle.Invalidate();
            }
        }
예제 #2
0
        public override bool UpdateIfNeeded()
        {
            EdgeCollider2D edgeCollider = collider as EdgeCollider2D;

            // retrieve collision world and index:
            var world = ObiColliderWorld.GetInstance();
            int index = source.Handle.index;

            // get or create the mesh:
            if (handle == null || !handle.isValid)
            {
                handle = world.GetOrCreateEdgeMesh(edgeCollider);
                handle.Reference();
            }

            // update collider:
            var shape = world.colliderShapes[index];

            shape.is2D                  = 1;
            shape.type                  = ColliderShape.ShapeType.EdgeMesh;
            shape.phase                 = source.Phase;
            shape.flags                 = edgeCollider.isTrigger ? 1 : 0;
            shape.rigidbodyIndex        = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
            shape.materialIndex         = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
            shape.center                = edgeCollider.offset;
            shape.contactOffset         = source.Thickness + edgeCollider.edgeRadius;
            shape.dataIndex             = handle.index;
            world.colliderShapes[index] = shape;

            // update bounds:
            var aabb = world.colliderAabbs[index];

            aabb.FromBounds(edgeCollider.bounds, shape.contactOffset, true);
            world.colliderAabbs[index] = aabb;

            // update transform:
            var trfm = world.colliderTransforms[index];

            trfm.FromTransform(edgeCollider.transform, true);
            world.colliderTransforms[index] = trfm;

            return(true);
        }
예제 #3
0
        public ObiEdgeMeshHandle GetOrCreateEdgeMesh(EdgeCollider2D source)
        {
            ObiEdgeMeshHandle handle;

            if (!handles.TryGetValue(source, out handle))
            {
                Vector2[] sourceVertices = source.points;
                int[]     sourceEdges    = new int[source.edgeCount * 2];

                for (int i = 0; i < source.edgeCount; ++i)
                {
                    sourceEdges[i * 2]     = i;
                    sourceEdges[i * 2 + 1] = i + 1;
                }

                // Build a bounding interval hierarchy from the edges:
                IBounded[] t = new IBounded[source.edgeCount];
                for (int i = 0; i < source.edgeCount; ++i)
                {
                    t[i] = new Edge(i, i + 1, sourceVertices[i], sourceVertices[i + 1]);
                }
                var sourceBih = BIH.Build(ref t);

                Edge[] edgs = Array.ConvertAll(t, x => (Edge)x);

                handle = new ObiEdgeMeshHandle(source, headers.count);
                handles.Add(source, handle);
                headers.Add(new EdgeMeshHeader(bihNodes.count, sourceBih.Length, edges.count, edgs.Length, vertices.count, sourceVertices.Length));

                bihNodes.AddRange(sourceBih);
                edges.AddRange(edgs);
                vertices.AddRange(sourceVertices);
            }

            return(handle);
        }
 public void DestroyEdgeMesh(ObiEdgeMeshHandle meshHandle)
 {
     edgeMeshContainer.DestroyEdgeMesh(meshHandle);
 }