private static void DrawCell(this Graphics g, CollisionCell cell, Vector2f indexes, Camera camera)
        {
            var location = indexes * CollisionCell.Size - camera.Body.Position;
            var locationPoint = new Point((int)location.X, (int)location.Y);
            var shape = new Rectangle(locationPoint, new Size(CollisionCell.Size, CollisionCell.Size));

            var pen = new Pen(cell.ObjectsInside.Count > 0 ? Color.DarkRed : Color.DarkCyan);
            pen.Width = 1.5f;

            g.DrawRectangle(pen, shape);
        }
Example #2
0
            /// <summary>
            /// Instantiate a new CollisionGrid.
            /// </summary>
            /// <param name="numXCells">The number of cells horizontal within the grid.</param>
            /// <param name="numYCells">The number of cells vertically within the grid.</param>
            public CollisionGrid(int numXCells, int numYCells)
            {
                NumXCells = numXCells;
                NumYCells = numYCells;
                NumTotalCells = NumXCells * NumYCells;

                Cells = new CollisionCell[NumXCells, NumYCells];
                for (int i = 0; i < numXCells; i++)
                {
                    for (int j = 0; j < numYCells; j++)
                    {
                        Cells[i, j] = new CollisionCell();
                    }
                }
            }
        public PhysicalProcessor(Scene scene, Vector2f gravity, uint iterationsCount, PhysicsMode mode)
        {
            IterCount = iterationsCount;
            StaticResistance = 0.3;
            var sceneSize = scene.Size;
            Gravity = gravity;
            this.mode = mode;
            master = scene;

            var colonsCount = (int)(sceneSize.X / CollisionCell.Size) + 1;
            var rowsCount = (int)(sceneSize.Y / CollisionCell.Size) + 1;
            cellMap = new CollisionCell[colonsCount, rowsCount];

            for (var x = 0; x < cellMap.GetLength(0); x++)
                for (var y = 0; y < cellMap.GetLength(1); y++)
                    cellMap[x, y] = new CollisionCell();
        }
Example #4
0
        private void CreateCollsionTuples()
        {
            ErrorCode error;
            Event eve;
            IntPtr agentPtrSize = (IntPtr)0;
            var ptrSize = (IntPtr)Marshal.SizeOf(typeof(Mem));

            int globalWorkSize = constants.numThreadsPerBlock * constants.numBlocks;
            int localWorkSize = constants.numThreadsPerBlock;

            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)globalWorkSize };
            IntPtr[] localWorkGroupSizePtr = new IntPtr[] { (IntPtr)localWorkSize };
            Event clevent;
            collisionTuples = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)( constants.numTotalElements * 8 * Marshal.SizeOf(typeof(CollisionTupel))),
                out error);
            CheckErr(error, "Createbuffer");

            error = Cl.EnqueueWriteBuffer(cqCommandQueue, sharedIdxMem, Bool.True, IntPtr.Zero, (IntPtr)(4),
                 0, 0, null, out eve);
            Console.WriteLine(" Num Elements {0} total {1}", sharedIdx[0], constants.numTotalElements);
            CheckErr(error, "EnqBuffer");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 0, ptrSize, cellData);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 1, ptrSize, collisionList);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 2, ptrSize, collisionTuples);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 3, ptrSize, sharedIdxMem);

            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 4, (IntPtr)(Marshal.SizeOf(typeof(CollisionConstants))), constants);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 5, (IntPtr)4, sharedIdx[0]);
            CheckErr(error, "Cl.SetKernelArg");

            error = Cl.EnqueueNDRangeKernel(cqCommandQueue, ckCreateCollsionTuples, 1, null, workGroupSizePtr, localWorkGroupSizePtr, 0, null, out clevent);
            CheckErr(error, "Cl.EnqueueNDRangeKernel");

            error = Cl.Finish(cqCommandQueue);
            CheckErr(error, "Cl.Finish");

            error = Cl.EnqueueReadBuffer(cqCommandQueue, sharedIdxMem, Bool.True, IntPtr.Zero, (IntPtr)(4),
                sharedIdx, 0, null, out eve);
            CheckErr(error, "Cl.EnqueueReadBuffer");

            error = Cl.EnqueueReadBuffer(cqCommandQueue, collisionTuples, Bool.True, IntPtr.Zero, (IntPtr)(sharedIdx[0] * Marshal.SizeOf(typeof(CollisionTupel))),
            tupelList, 0, null, out eve);
            CheckErr(error, "Cl.EnqueueReadBuffer");

                for (int i = 0; i < readCellIdList.Count(); i++)
                {
                    if (cellMap.ContainsKey(readCellIdList[i]))
                    {
                        cellPosList[i] = cellMap[readCellIdList[i]];
                    }
                    else
                    {
                        cellPosList[i] = new CollisionCell();
                    }
                }
                if (DEBUG)
                {
                DebugHelper.PrintCollisionTuples(tupelList, readCellIdList, readCellData, cellPosList, "Collision tuples to check", sharedIdx[0]);
            }

            error = Cl.ReleaseMemObject(collisionList);
            CheckErr(error, "Cl.ReleaseMem");
        }
Example #5
0
        public void Commit()
        {
            var before = DateTime.Now;
            // Block all incoming requests until the calculation
            gpuActive = true;
            // Wait till the processing add and move operations finished
            while (m_ActiveOperationCount > 0)
            {
                Thread.Sleep(5);
            }

            // Total number of elements to calculate
             constants.numTotalElements = envActEnvObjs.Count + envExploreObjs.Count + envFreshAddedObjs.Count;
            /*
             constants.numTotalElements--;
             constants.numTotalElements |=  constants.numTotalElements >> 1;
             constants.numTotalElements |=  constants.numTotalElements >> 2;
             constants.numTotalElements |=  constants.numTotalElements >> 4;
             constants.numTotalElements |=  constants.numTotalElements >> 8;
             constants.numTotalElements |=  constants.numTotalElements >> 16;
             constants.numTotalElements++;
            */

            if (DEBUG)
            {
                debugReadInput = new long[constants.numTotalElements];
                debugCollisionList = new long[constants.numTotalElements * 8];
            }
            cellPosList = new CollisionCell[constants.numTotalElements * 8];
            tupelList = new CollisionTupel[constants.numTotalElements * 8];
            readCellData = new long[constants.numTotalElements * 8];
            readCellIdList = new int[constants.numTotalElements * 8];

            clShapeArray = envActEnvObjs.Values.ToList();
            clShapeArray.AddRange(envExploreObjs.Values.ToList());
            clShapeArray.AddRange(envFreshAddedObjs.Values.ToList());
              //  int fillCount =  constants.numTotalElements - clShapeArray.Count;
            /*long[] dummys = new long[fillCount];
            dummys.i*/

            //shapes.
            clObjArray = envActEnvObjs.Keys.ToList();
            clObjArray.AddRange(envExploreObjs.Keys.ToList());
            clObjArray.AddRange(envFreshAddedObjs.Keys.ToList());
            constants.numTotalElements = clObjArray.Count;

            #region CreateBuffers
            // Create the Buffers
            CreateBuffers();
            #endregion

            Console.WriteLine("Execution time Create Buffers =" + (DateTime.Now - before).TotalMilliseconds);
            before = DateTime.Now;

            #region createCellList
            CreateCellId();
            #endregion

            Console.WriteLine("Execution time CreateCellIdArray =" + (DateTime.Now - before).TotalMilliseconds);
            before = DateTime.Now;

            #region RadixSort
            // Radix Sort
            sort.sortKeysValue(cellIds, cellData, constants.numTotalElements * 8);
            //sort.sortKeysValue();
            ErrorCode error;
            Event eve;

            error = Cl.EnqueueReadBuffer(cqCommandQueue, cellData, Bool.True, IntPtr.Zero, (IntPtr)(constants.numTotalElements * 8 * 8),
            readCellData, 0, null, out eve);
            CheckErr(error, "Cl.EnqueueReadBuffer");

            if (DEBUG)
            {
                error = Cl.EnqueueReadBuffer(cqCommandQueue, cellIds, Bool.True, IntPtr.Zero, (IntPtr)(constants.numTotalElements * 4 * 8),
                readCellIdList, 0, null, out eve);
                CheckErr(error, "Cl.EnqueueReadBuffer");

                for (int i = 0; i < readCellIdList.Count(); i++)
                {
                    if (cellMap.ContainsKey(readCellIdList[i]))
                    {
                        cellPosList[i] = cellMap[readCellIdList[i]];
                    }
                    else
                    {
                        cellPosList[i] = new CollisionCell();
                    }
                }

                DebugHelper.PrintCellIdBufferExtended(debugReadInput, readCellIdList, readCellData, cellPosList, "Sorted Keys", constants.numTotalElements);

            }

            #endregion

            Console.WriteLine("Execution time RadixSort =" + (DateTime.Now - before).TotalMilliseconds);
            before = DateTime.Now;

            #region CollisionList
            CreateCollisionList();
            #endregion

            Console.WriteLine("Execution time CreateCollisionList =" + (DateTime.Now - before).TotalMilliseconds);
            before = DateTime.Now;

            #region CreateCollisionTuples
            CreateCollsionTuples();
            #endregion
            Console.WriteLine("Execution time CreateCollisionTuples =" + (DateTime.Now - before).TotalMilliseconds);
            before = DateTime.Now;
            #region CheckCollisions
            if (sharedIdx[0] > 0)
            {
                CheckCollsions();
                Console.WriteLine("Execution time CheckCollisions =" + (DateTime.Now - before).TotalMilliseconds);
                before = DateTime.Now;

                //TODO: MAybe do this threadwise to get some more performance.
                for (int i = 0; i < sharedIdx[0]; i++)
                {
                    if (collisionMap.ContainsKey(tupelList[i].obj1))
                    {
                        collisionMap[tupelList[i].obj1].Add(tupelList[i].obj2);
                    }
                    else
                    {
                        HashSet<int> tmp = new HashSet<int>();
                        tmp.Add(tupelList[i].obj2);
                        collisionMap.Add(tupelList[i].obj1, tmp);
                    }
                    if (collisionMap.ContainsKey(tupelList[i].obj2))
                    {
                        collisionMap[tupelList[i].obj2].Add(tupelList[i].obj1);
                    }
                    else
                    {
                        HashSet<int> tmp = new HashSet<int>();
                        tmp.Add(tupelList[i].obj1);
                        collisionMap.Add(tupelList[i].obj2, tmp);
                    }
                }

                // Check Results
            }

            // RemoveSet
            var itemsToRemove = new HashSet<int>();
            // Call all delegates
            foreach (var actDele in m_AddDelegates)
            {
                int tmpId = (int) (actDele.Item1 & 0xFFFFFFFF);
                if (collisionMap.ContainsKey(tmpId))
                {
                    // Add collided -> dont add item to environment
                    List<ISpatialEntity> deleList = new List<ISpatialEntity>();//collisionMap[tmpId].Select(actObj => objIdSpatialMap[actObj]).ToList();
                    foreach (var actobjId in collisionMap[tmpId].ToList())
                    {
                        deleList.Add(objIdSpatialMap[actobjId]);
                    }
                    var ret = new MovementResult(deleList);
                    actDele.Item2.Invoke(ret);
                    itemsToRemove.Add(tmpId);
                }
                else
                {
                    // Add successful -> Add Element to environment and invoke corresponding delegate
                    actDele.Item2.Invoke(new MovementResult());
                    envActEnvObjs.Add(((long)tmpId) | FLAG_MOVE,objIdClShapeMap[tmpId]);
                }
            }
            foreach (var actDele in m_MoveDelegates)
            {
                int tmpId = (int)(actDele.Item1 & 0xFFFFFFFF);
                if (collisionMap.ContainsKey(tmpId))
                {
                    List<ISpatialEntity> deleList = collisionMap[tmpId].Select(actObj => objIdSpatialMap[actObj]).ToList();
                    var ret = new MovementResult(deleList);
                    actDele.Item2.Invoke(ret);
                }
                else
                {
                    actDele.Item2.Invoke(new MovementResult());
                }
            }
            foreach (var actDele in m_ExploreDelegates)
            {
                int tmpId = (int)(actDele.Item1 & 0xFFFFFFFF);
                if (collisionMap.ContainsKey(tmpId))
                {
                    List<ISpatialEntity> deleList = collisionMap[tmpId].Select(actObj => objIdSpatialMap[actObj]).ToList();
                    actDele.Item2.Invoke(deleList);
                }
                else
                {
                    actDele.Item2.Invoke(new List<ISpatialEntity>());
                }
            }
            foreach (var act in itemsToRemove)
            {
                RemoveByObjId(act);
            }
            #endregion
            m_AddDelegates.Clear();
            m_MoveDelegates.Clear();
            m_ExploreDelegates.Clear();
            envFreshAddedObjs.Clear();
            envExploreObjs.Clear();
            layerTickWait.Set();
            Thread.Sleep(10);
            layerTickWait.Reset();
            gpuActive = false;

            gpuActiveWait.Set();
            Thread.Sleep(10);
            gpuActiveWait.Reset();
            Console.WriteLine("Execution time Call Delegates and Cleanup =" + (DateTime.Now - before).TotalMilliseconds);
               // before = DateTime.Now;
        }
Example #6
0
        private void DrawCollisionCell(RenderTarget target, CollisionCell cell, int x, int y, Vector2f offset)
        {
            Transform transf = Transform.Identity;
            transf.Translate(new Vector2f(x * CollisionCell.Size, y * CollisionCell.Size) - offset);
            var shape = new RectangleShape(new Vector2f(CollisionCell.Size - 3, CollisionCell.Size - 3));

            shape.FillColor = Color.Transparent;
            shape.OutlineColor = cell.ObjectsInside.Count > 0 ? Color.Red : Color.Green;
            shape.OutlineThickness = 1.5f;

            target.Draw(shape, new RenderStates(transf));
        }