コード例 #1
0
ファイル: BroadPhase.cs プロジェクト: willnode/Qu3e-Sharp
        // Generates the contact list. All previous contacts are returned to the allocator
        // before generation occurs.
        public void UpdatePairs()
        {
            PairBuffer.Clear();

            // Query the tree with all moving boxs
            for (int i = 0; i < MoveBuffer.Count; ++i)
            {
                CurrentIndex = MoveBuffer[i];
                AABB aabb = Tree.GetFatAABB(CurrentIndex);

                // @TODO: Use a static and non-static tree and query one against the other.
                //        This will potentially prevent (gotta think about this more) time
                //        wasted with queries of static bodies against static bodies, and
                //        kinematic to kinematic.
                Tree.Query(this, aabb);
            }

            // Reset the move buffer
            MoveBuffer.Clear();

            // Sort pairs to expose duplicates
            PairBuffer.Sort(ContactPairSorter.Default);

            // Queue manifolds for solving
            {
                int i = 0;
                while (i < PairBuffer.Count)
                {
                    // Add contact to manager
                    ContactPair pair = PairBuffer[i];
                    Box         A    = (Box)Tree.GetUserData(pair.A);
                    Box         B    = (Box)Tree.GetUserData(pair.B);
                    Manager.AddContact(A, B);

                    ++i;

                    // Skip duplicate pairs by iterating i until we find a unique pair
                    while (i < PairBuffer.Count)
                    {
                        ContactPair potentialDup = PairBuffer[i];

                        if (pair.A != potentialDup.A || pair.B != potentialDup.B)
                        {
                            break;
                        }

                        ++i;
                    }
                }
            }

//            Tree.Validate();
        }
コード例 #2
0
        public unsafe void BuildTreeAndOverlap([Values(2, 10, 33, 100)] int elementCount)
        {
            elementCount *= 2;
            int numNodes            = elementCount / 3 * 2 + 4;
            var points              = new NativeArray <PointAndIndex>(elementCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            var aabbs               = new NativeArray <Aabb>(elementCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            var filters             = new NativeArray <CollisionFilter>(elementCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            var respondsToCollision = new NativeArray <bool>(elementCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            InitInputWithCopyArrays(points, aabbs, filters, respondsToCollision);

            var nodes = new NativeArray <Node>(numNodes, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            var bvh = new BoundingVolumeHierarchy(nodes);

            bvh.Build(points, aabbs, out int numNodesOut);
            bvh.CheckIntegrity();

            var buffer = new PairBuffer {
                Pairs = new List <BodyIndexPair>()
            };

            buffer.MaxId = elementCount - 1;

            Node *nodesPtr = (Node *)nodes.GetUnsafePtr();

            BoundingVolumeHierarchy.TreeOverlap(ref buffer, nodesPtr, nodesPtr);

            int numCollidingPairs = buffer.Pairs.Count;

            //Debug.Log($"Num colliding pairs: {buffer.Pairs.Count}");
            Assert.AreEqual(elementCount / 2, numCollidingPairs);

            filters.Dispose();
            respondsToCollision.Dispose();
            points.Dispose();
            aabbs.Dispose();
            nodes.Dispose();
        }
コード例 #3
0
        public unsafe void BuildTreeAndOverlap([Values(2, 10, 33, 100)] int elementCount)
        {
            elementCount *= 2;
            int numNodes = elementCount / 3 * 2 + 4;
            var points   = new NativeArray <BoundingVolumeHierarchy.PointAndIndex>(elementCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            var aabbs    = new NativeArray <Aabb>(elementCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            var filters  = new NativeArray <CollisionFilter>(elementCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            InitializeInputWithCopyArrays(points, aabbs, filters);

            var nodes = new NativeArray <BoundingVolumeHierarchy.Node>(numNodes, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            var bvh = new BoundingVolumeHierarchy(nodes);

            bvh.Build(points, aabbs, out int numNodesOut);
            bvh.CheckIntegrity();

            var buffer = new PairBuffer {
                Pairs = new NativeList <PhysicsBody.IndexPair>(0, Allocator.Temp)
            };

            buffer.MaxId = elementCount - 1;

            BoundingVolumeHierarchy.Node *nodesPtr = (BoundingVolumeHierarchy.Node *)nodes.GetUnsafePtr();
            BoundingVolumeHierarchy.TreeOverlap(ref buffer, nodesPtr, nodesPtr);

            int numCollidingPairs = buffer.Pairs.Length;

            Assert.AreEqual(elementCount / 2, numCollidingPairs);

            buffer.Dispose();
            filters.Dispose();
            points.Dispose();
            aabbs.Dispose();
            nodes.Dispose();
        }