private void FindAllCells() { _grid = new SpatialGrid(); _grid.radius = _meshRenderer.bounds.size.x / gridSizeX; _grid.radius /= 2; float multiplier = gridSizeX % 2 == 0 ? 1.5f : 2; float startPointX = _grid.radius * multiplier * (gridSizeX / 2); float startPointY = ((_meshRenderer.bounds.size.y / (_grid.radius * 2)) / 2) * multiplier * _grid.radius; float startPointZ = ((_meshRenderer.bounds.size.z / (_grid.radius * 2)) / 2) * multiplier * _grid.radius; float interval = _grid.radius * 2; for (float x = -startPointX; x <= startPointX; x += interval) { for (float y = -startPointY; y <= startPointY; y += interval) { for (float z = -startPointZ; z <= startPointZ; z += interval) { Vector3 position = new Vector3(x, y, z) + _meshRenderer.bounds.center; Collider[] hitColliders = Physics.OverlapSphere(position, _grid.radius); foreach (Collider collider in hitColliders) { if (collider.gameObject == gameObject) { AddCellToGrid(position); } } } } } }
public void Shutdown() { WorldSettings.Shutdown(); WorldSettings = null; Camera = null; foreach (IRenderer RenderAsset in Assets.Values) { RenderAsset.Shutdown(); } foreach (SpatialGrid grid in navigationGrids) { grid?.Shutdown(); } navigationGrids = null; translokatorGrid?.Shutdown(); translokatorGrid = null; selectionBox.Shutdown(); selectionBox = null; TranslationGizmo.Shutdown(); TranslationGizmo = null; clouds.Shutdown(); clouds = null; sky.Shutdown(); sky = null; Assets = null; D3D?.Shutdown(); D3D = null; }
public MoveSystem(SpatialGrid spatialGrid, CollisionMessageManager collisionMessageManager) { this.spatialGrid = spatialGrid; this.collisionMessageManager = collisionMessageManager; entityQuery = new SortedSet <EGID>(); }
public static Vector3 GetGridLocalMax(SpatialGrid grids, SpatialGridIndex index) { Vector3 localMax = new Vector3(0, 0, 0); localMax.x = grids.min.x + grids.w * (index.x + 1); localMax.y = grids.min.y + grids.h * (index.y + 1); localMax.z = grids.min.z + grids.d * (index.z + 1); return(localMax); }
private void OnEnable() { style = new GUIStyle(); style.alignment = TextAnchor.MiddleCenter; style.fontSize = 20; grid = FindObjectOfType <SpatialGrid>(); maxSize = new Vector2(500, 375); minSize = new Vector2(500, 375); }
public static Vector3 GetGridLocalMin(SpatialGrid grids, SpatialGridIndex index) { Vector3 localMin = new Vector3(0, 0, 0); localMin.x = grids.min.x + grids.w * index.x; localMin.y = grids.min.y + grids.h * index.y; localMin.z = grids.min.z + grids.d * index.z; return(localMin); }
public CollisionDetectionSystem(EntityWorld entityWorld) { this.entityWorld = entityWorld; this.UseSpatialHashing = true; this.targets = new IndexedList <Entity>(); this.spatialGrid = new SpatialGrid <Entity>(GameConfig.CellSize, GameConfig.NumCells); this.checkedPairs = new HashSet <ulong>(); this.DesiredComponentIds = entityWorld.ComponentManager.GetComponentId <CollidableComponent>() | entityWorld.ComponentManager.GetComponentId <TransformComponent>(); }
public GraphicsClass() { InitObjectStack = new Dictionary <int, IRenderer>(); Profile = new Profiler(); Assets = new Dictionary <int, IRenderer>(); selectionBox = new RenderBoundingBox(); translokatorGrid = new SpatialGrid(); navigationGrids = new SpatialGrid[0]; OnSelectedObjectUpdated += OnSelectedObjectHasUpdated; }
public static void GetDeltaToBoundary(SpatialGridIndex entryGridIndex, SpatialGrid grids, Ray entryToBoundaryRay, GridStep steps, ref RayHit rayHitX, ref RayHit rayHitY, ref RayHit rayHitZ) { InnerDistanceToBoundary( entryToBoundaryRay, steps, GetGridLocalMin(grids, entryGridIndex), GetGridLocalMax(grids, entryGridIndex), ref rayHitX, ref rayHitY, ref rayHitZ); }
private void Awake() { mySpatialGrid = GetComponent <SpatialGrid>(); AllEntities = mySpatialGrid.GetEntities().ToList(); obstaculos = AllEntities .Where(x => x.GetComponent <Obstacle>() != null) .Select(x => x.GetComponent <Obstacle>()).ToList(); enemigos = AllEntities .Where(x => x.GetComponent <Enemy>() != null) .Select(x => x.GetComponent <Enemy>()).ToList(); }
public Arena(int width, int height, Game game) { if (width < 1 || height < 1) { throw new InvalidOperationException("Cannot create an arena with width or height less than 1."); } this.width = width; this.height = height; gameref = game; //Create the arena grid, max grid size = 50 grid = new SpatialGrid<Component>(width, height, 75); background = new ParallaxBackground(game.Resources.GetTexture("starscape1"), 0.3, game.Display); }
//TODO there is an issue where either collision entities or sprites are not being correctly removed from the dictionary of objects requiring updates public override void Update(GameTime gameTime) { UpdateGameBounds(); _spatialGrid = new SpatialGrid(_gameBounds, 200); _keyboardState = Keyboard.GetState(); _mouseState = Mouse.GetState(); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { GameManager.PauseGame(); } if (_keyboardState.IsKeyDown(Keys.K) && !_previousKeyboardState.IsKeyDown(Keys.K)) { GameManager.ToggleDebug(); } ApplyMouseWheelZoom(); Camera.Location = new Vector2(_player.Position.X, _player.Position.Y); UpdateLazersCollsions(); SpawnEnemies(); SpawnBoulders(); DespawnSprites(); _player.Update(gameTime); var enemies = _enemies.ToArray(); for (int i = 0; i < enemies.Length; i++) { var fleeDistance = 1000f; if ((_player.Center - enemies[i].Center).Length() < fleeDistance) { enemies[i].Flee(_player.Center, fleeDistance); } enemies[i].Update(gameTime); } foreach (var boulder in _boulders) { boulder.Update(gameTime); } CheckAndResolveCollisions(gameTime); _previousMouseState = _mouseState; _previousKeyboardState = _keyboardState; }
public static SpatialGridIndex GetGridIndexAtPoint(SpatialGrid grid, Vector3 pt) { int d = grid.dimension; SpatialGridIndex index; index.x = Mathf.FloorToInt((pt.x - grid.min.x) / (grid.max.x - grid.min.x) * d); index.x = Mathf.Clamp(index.x, 0, d - 1); index.y = Mathf.FloorToInt((pt.y - grid.min.y) / (grid.max.y - grid.min.y) * d); index.y = Mathf.Clamp(index.y, 0, d - 1); index.z = Mathf.FloorToInt((pt.z - grid.min.z) / (grid.max.z - grid.min.z) * d); index.z = Mathf.Clamp(index.z, 0, d - 1); return(index); }
public TreeNode SetNavigationGrid(ResourceTypes.Navigation.OBJData[] data) { TreeNode[] Grids = new TreeNode[data.Length]; navigationGrids = new SpatialGrid[data.Length]; for (int i = 0; i < navigationGrids.Length; i++) { navigationGrids[i] = new SpatialGrid(data[i].runtimeMesh); navigationGrids[i].Initialise(D3D.Device, D3D.DeviceContext); Grids[i] = navigationGrids[i].GetTreeNodes(); Grids[i].Text = string.Format("Grid: {0}", i); } TreeNode Parent = new TreeNode("Navigation Grids"); Parent.Nodes.AddRange(Grids); return(Parent); }
private void Update() { Vector3 origin = transform.position; Vector3 direction = transform.forward; if (origin != cacheOrigin || direction != cacheDirection) { Ray cameraRay = new Ray(origin, direction); SpatialGrid spatialGrid = SpatialGrid.CreateSpatialGrid(CubeGlobalData.Instance.boxMin, CubeGlobalData.Instance.boxMax, CubeGlobalData.Instance.GetDimension()); m_bestRayHit = SpatialGridTrace.Trace(cameraRay, spatialGrid, _GeometryCountList, _GeometryGridList, -1); cacheOrigin = origin; cacheDirection = direction; } voxelsHitList.ForEach((controller => { controller.ApplyColor(Color.red); })); }
public GraphicsClass() { InitObjectStack = new Dictionary <int, IRenderer>(); Profile = new Profiler(); Assets = new Dictionary <int, IRenderer>(); selectionBox = new RenderBoundingBox(); translokatorGrid = new SpatialGrid(); navigationGrids = new SpatialGrid[0]; OurPrimitiveManager = new PrimitiveManager(); OnSelectedObjectUpdated += OnSelectedObjectHasUpdated; // Create bespoke batches for any lines or boxes passed in via the construct stack string LineBatchID = string.Format("Graphics_LineBatcher_{0}", RefManager.GetNewRefID()); LineBatch = new PrimitiveBatch(PrimitiveType.Line, LineBatchID); string BBoxBatchID = string.Format("Graphics_BBoxBatcher_{0}", RefManager.GetNewRefID()); BBoxBatch = new PrimitiveBatch(PrimitiveType.Box, BBoxBatchID); OurPrimitiveManager.AddPrimitiveBatch(LineBatch); OurPrimitiveManager.AddPrimitiveBatch(BBoxBatch); }
public static RayHit Trace(Ray cameraRay, SpatialGrid spatialGrid, List <int> geometryIndexList, List <RTTriangle_t> geometryList, int exclude) { RayHit bestHit = RayHit.CreateRayHit(); float t0 = float.NegativeInfinity; float t1 = float.PositiveInfinity; if (RayBoxIntersectionCompute.RayBoxIntersection(cameraRay, spatialGrid.min, spatialGrid.max, ref t0, ref t1)) { Vector3 entry = cameraRay.GetPoint(t1); GridStep step = DetermineGridStep(cameraRay); Ray entryToBoundaryRay = CreateRay(entry, cameraRay.direction); SpatialGridIndex entryGridIndex = SpatialGridCompute.GetGridIndexAtPoint(spatialGrid, entry); RayHit deltaX = new RayHit(); RayHit deltaY = new RayHit(); RayHit deltaZ = new RayHit(); SpatialGridCompute.GetDeltaToBoundary( entryGridIndex, spatialGrid, entryToBoundaryRay, step, ref deltaX, ref deltaY, ref deltaZ); RayHit distToNextX = new RayHit(); RayHit distToNextY = new RayHit(); RayHit distToNextZ = new RayHit(); SpatialGridCompute.DistanceBetweenBoundary( cameraRay, step, entryGridIndex, spatialGrid, deltaX.hitPoint, deltaY.hitPoint, deltaZ.hitPoint, ref distToNextX, ref distToNextY, ref distToNextZ ); float tx = deltaX.distance; float ty = deltaY.distance; float tz = deltaZ.distance; SpatialGridIndex current = entryGridIndex; #region Debug CubeController currentGridController = CubeGlobalData.Instance.GetGridAtIndex(current); JoeRayCaster.voxelsHitList.Clear(); JoeRayCaster.voxelsHitList.Add(currentGridController); #endregion //Debug bool hasHit = false; bool outside = false; do { bestHit = LocalGridTrace( cameraRay, current, geometryIndexList, geometryList, spatialGrid, -1); if (tx < ty) { if (tx < tz) { // Move On X current.x = current.x + step.x; tx += distToNextX.distance; } else { // Move On Z current.z = current.z + step.z; tz += distToNextZ.distance; } } else { if (ty < tz) { // Move On Y current.y = current.y + step.y; ty += distToNextY.distance; } else { // Move On Z current.z = current.z + step.z; tz += distToNextZ.distance; } } #region Debug currentGridController = CubeGlobalData.Instance.GetGridAtIndex(current); if (currentGridController != null) { JoeRayCaster.voxelsHitList.Add(currentGridController); } #endregion hasHit = (bestHit.distance < float.PositiveInfinity); outside = SpatialGridCompute.IsGridIndexOutsideGrid(spatialGrid, current); }while(!hasHit && !outside); } else { JoeRayCaster.voxelsHitList.Clear(); } return(bestHit); }
void SetupSpatialGrid() { mGrid = new SpatialGrid(mSpatialGridCellSize); }
private void Awake() { targetGrid = FindObjectOfType <SpatialGrid>(); queryManager = FindObjectOfType <QueryManager>(); queryManager.AddQuery(this); }
public TreeNode SetTranslokatorGrid(TranslokatorLoader translokator) { translokatorGrid = new SpatialGrid(translokator); translokatorGrid.Initialise(D3D.Device, D3D.DeviceContext); return(translokatorGrid.GetTreeNodes()); }
public void AddRegion(SpatialGrid sp) { regions.Add(sp); }
public static void DistanceBetweenBoundary(Ray ray, GridStep steps, SpatialGridIndex entryGridIndex, SpatialGrid grids, Vector3 firstHitX, Vector3 firstHitY, Vector3 firstHitZ, ref RayHit rayHitX, ref RayHit rayHitY, ref RayHit rayHitZ) { Vector3 min = GetGridLocalMin(grids, entryGridIndex); Vector3 max = GetGridLocalMax(grids, entryGridIndex); Ray rX = SpatialGridTrace.CreateRay(firstHitX, ray.direction); Ray rY = SpatialGridTrace.CreateRay(firstHitY, ray.direction); Ray rZ = SpatialGridTrace.CreateRay(firstHitZ, ray.direction); if (steps.x >= 0) { rayHitX = RayPlaneIntersectionCompute.RayPlaneIntersection(rX, GridNormalLeft, max + GridNormalRight); } else { rayHitX = RayPlaneIntersectionCompute.RayPlaneIntersection(rX, GridNormalRight, min + GridNormalLeft); } if (steps.y >= 0) { rayHitY = RayPlaneIntersectionCompute.RayPlaneIntersection(rY, GridNormalDown, max + GridNormalUp); } else { rayHitY = RayPlaneIntersectionCompute.RayPlaneIntersection(rY, GridNormalUp, min + GridNormalDown); } if (steps.z >= 0) { rayHitZ = RayPlaneIntersectionCompute.RayPlaneIntersection(rZ, GridNormalBack, max + GridNormalForward); } else { rayHitZ = RayPlaneIntersectionCompute.RayPlaneIntersection(rZ, GridNormalForward, min + GridNormalBack); } }
public BoxSelection(SpatialGrid targetGrid, float width, float height) { this.targetGrid = targetGrid; this.width = width; this.height = height; }
public static RayHit LocalGridTrace(Ray ray, SpatialGridIndex index, List <int> geomGridList, List <RTTriangle_t> triangles, SpatialGrid grids, int excludeGeometry) { RayHit bestHit = RayHit.CreateRayHit(); RayHit currentHit = RayHit.CreateRayHit(); int start = 0; int count = 0; GetNumberOfGeometryInGrid(index, geomGridList, grids.dimension, out start, out count); if (count == 0) { return(bestHit); } Vector3 localMin = SpatialGridCompute.GetGridLocalMin(grids, index); Vector3 localMax = SpatialGridCompute.GetGridLocalMax(grids, index); for (int t = 0; t < count; t++) { if (triangles[start + t].id == excludeGeometry) { continue; } RTTriangle.IntersectTriangle(ray, ref currentHit, triangles[start + t]); if (!(localMin.x <= currentHit.hitPoint.x && currentHit.hitPoint.x <= localMax.x)) { currentHit = bestHit; // Reset currentHit continue; } if (!(localMin.y <= currentHit.hitPoint.y && currentHit.hitPoint.y <= localMax.y)) { currentHit = bestHit; // Reset currentHit continue; } if (!(localMin.z <= currentHit.hitPoint.z && currentHit.hitPoint.z <= localMax.z)) { currentHit = bestHit; // Reset currentHit continue; } bestHit = currentHit; } return(bestHit); }
public static bool IsGridIndexOutsideGrid(SpatialGrid grid, SpatialGridIndex index) { return((index.x >= grid.dimension || index.x < 0) || (index.y >= grid.dimension || index.y < 0) || (index.z >= grid.dimension || index.z < 0)); }
public CircleSelection(SpatialGrid targetGrid, float radius) { this.targetGrid = targetGrid; this.radius = radius; }