public PhysicsWorld(int width, int height, int partitionSize) { Instance = this; ColliderPartition = new CellSpacePartition <ICollider>(width, height, partitionSize); _collidersInArea = new List <ICollider>(); }
private Vector2 rightDown; //右下角 #endregion private void Awake() { _instance = this; navGraph = new NavGraph(); //进行方向向量的初始化 pathFinder = new PathFinder(); leftUp = (Vector2.up + Vector2.left).normalized; rightUp = (Vector2.up + Vector2.right).normalized; rightDown = (Vector2.right + Vector2.down).normalized; leftDown = (Vector2.left + Vector2.down).normalized; cellSpacePartition = new CellSpacePartition <NavGraphNode>(Mathf.Abs(upBorder.position.x - downBorder.position.x) , Mathf.Abs(upBorder.position.y - downBorder.position.y) , 8, 8); CreateMap(); Debug.Log("总共有" + NavGraph.TotalV + "个顶点"); Debug.Log("总共有" + NavGraph.TotalE + "条边"); ToDebug(); }
/// <summary> /// Checks if entity is within bounds /// </summary> /// <param name="checkedCell">index of cell to check</param> /// <param name="partition"> partition to check </param> /// <returns></returns> public bool EntityWithinBounds(int checkedCell, CellSpacePartition partition) { if (checkedCell >= 0 && checkedCell < partition.cellLength) { return(true); } return(false); }
private void UpdateCameraBoundaries() { TopLeftPosition = Vector2.Transform(new Vector2(0, 0), Matrix.Invert(transform)); TopRightPosition = Vector2.Transform(new Vector2(viewPort.Width, 0), Matrix.Invert(transform)); BottomLeftPosition = Vector2.Transform(new Vector2(0, viewPort.Height), Matrix.Invert(transform)); BottomRightPosition = Vector2.Transform(new Vector2(viewPort.Width, viewPort.Height), Matrix.Invert(transform)); _topLeft = CellSpacePartition.GetTopLeftPartition(TopLeftPosition); _topRight = CellSpacePartition.GetTopRightPartition(TopRightPosition); _bottomLeft = CellSpacePartition.GetBottomLeftPartition(BottomLeftPosition); _bottomRight = CellSpacePartition.GetBottomRightPartition(BottomRightPosition); }
void Start() { var b = entity.GetComponent <Collider>().bounds; var ls = entity.localScale; width = b.size.x; height = b.size.z; cellSpace = new CellSpacePartition <Vehicle>(Vector3.zero, new Vector3(width, 1, height), new Vector3(10, 1, 8), 50); InitVehicle(); InitObstacles(); InitWalls(); crosshairView = GameObject.Find("crossHair"); cellSpace.DebugDrawOn(); }
public SearchAStar(MovingEntity entity, Cell goal) { ConsideredCells = new List <Cell>(); graph = entity.gameWorld.GridLogic; from = entity.InCell; to = goal; m_GCosts = new Dictionary <int, double>(graph.TotalNumberOfCells); m_FCosts = new Dictionary <int, double>(graph.TotalNumberOfCells); m_shortestPathTree = new Dictionary <int, Edge>(graph.TotalNumberOfCells); m_searchFrontier = new Dictionary <int, Edge>(graph.TotalNumberOfCells); for (int i = 0; i < graph.TotalNumberOfCells; i++) { m_GCosts[i] = 0.0; m_FCosts[i] = 0.0; m_searchFrontier[i] = null; m_shortestPathTree[i] = null; } calculate = ManhattanHeuristic.Calculate; }
public World(int w, int h) { Width = w; Height = h; TriangleModeActive = false; VelocityVisible = false; DebugMode = true; _goblinSpace = new CellSpacePartition <MovingEntity>(w, h, 5, 5); _goblins = new List <MovingEntity>(); goblinColors = new List <Color>(); goblinColors.Add(Color.Green); goblinColors.Add(Color.ForestGreen); goblinColors.Add(Color.DarkOliveGreen); goblinColors.Add(Color.DarkGreen); SpawnObstacles(); SpawnWalls(); Graph = GraphUtil.CreateGraphForMap(w, h, 50, _obstacles, _walls); populate(); }
public void OpenWorld(float cx, float cy, float angle = 0, int cellX = 0, int cellY = 0, int numAgents = 0) { cxClient = cx; cyClient = cy; Quaternion rotation = Quaternion.Euler(0, angle, 0); Vector3 position = Vector3.zero; position.x = -1 * cxClient * 0.5f; position.z = -1 * cyClient * 0.5f; space = Matrix4x4.TRS(position, rotation, Vector3.one); if (cellX > 0 && cellY > 0) { Prm.NumCellsX = cellX; Prm.NumCellsY = cellY; Prm.NumAgents = numAgents; //setup the spatial subdivision class m_pCellSpace = new CellSpacePartition <SteeringBehaviour>(cx, cy, Prm.NumCellsX, Prm.NumCellsY, Prm.NumAgents); } }
private void InitializeILocalCellPartition() { LocalSpacePartition = new CellSpacePartition(Diameter, Diameter, 2); }
private void InitializeCellSpacePartition() { CellSpacePartition = new Systems.CellSpacePartition(Diameter, Diameter, 4); int[,] newTileMap = GetProperTextures(); for (int y = 0; y < tileMap.GetLength(1); y++) { for (int x = 0; x < tileMap.GetLength(0); x++) { if (tileMap[x, y] != -1) { //Console.WriteLine(tileMap[x,y]); Tile tempTile = new Tile(); switch (tileMap[x, y]) { case 0: { Console.WriteLine("Using Old Tile, please check "); break; } case 1: { Console.WriteLine("Using Old Tile, please check "); break; } case 3: { switch (newTileMap[x, y]) { case 1: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 10, Tile.TileType.Solid); break; } case 2: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 11, Tile.TileType.Solid); break; } case 3: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 12, Tile.TileType.Solid); break; } case 4: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 13, Tile.TileType.Solid); break; } case 5: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 14, Tile.TileType.Solid); break; } case 6: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 15, Tile.TileType.Solid); break; } case 7: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 16, Tile.TileType.Solid); break; } case 8: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 17, Tile.TileType.Solid); break; } case 9: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 18, Tile.TileType.Solid); break; } case 10: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 19, Tile.TileType.Solid); break; } case 11: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), 20, Tile.TileType.Solid); break; } default: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), tileMap[x, y], Tile.TileType.Solid); break; } } break; } default: { tempTile = new Tile(new Vector2(position.X + (x * 128), position.Y + (y * 128)), tileMap[x, y], Tile.TileType.NonSolid); break; } } if (Utilities.Debug.debug) { Console.WriteLine(tempTile.id + " added tile type " + tempTile.tileType.ToString() + " with " + tempTile.components.Count() + " components"); } switch (tempTile.tileType) { case Tile.TileType.NonSolid: { CellSpacePartition.AddStaticEntity(tempTile); break; } case Tile.TileType.Solid: { CellSpacePartition.AddEntity(tempTile); break; } } } } } nodeMesh.MakeMap(3, tileMap); tileMap = null; }
public World() { //GetWeather(); GridLogic = new CellSpacePartition(GlobalVars.cellSize); }
public void UpdateDynamicCellPartition(GameTime gameTime) { if (EntityWithinBounds(CellSpacePartition.GetCenterPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetCenterPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetCenterPartition(Camera.Instance.center)].Update(gameTime); } if (EntityWithinBounds(CellSpacePartition.GetTopLeftPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetTopLeftPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetTopLeftPartition(Camera.Instance.center)].Update(gameTime); } if (EntityWithinBounds(CellSpacePartition.GetTopPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetTopPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetTopPartition(Camera.Instance.center)].Update(gameTime); } if (EntityWithinBounds(CellSpacePartition.GetTopRightPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetTopRightPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetTopRightPartition(Camera.Instance.center)].Update(gameTime); } if (EntityWithinBounds(CellSpacePartition.GetRightPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetRightPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetRightPartition(Camera.Instance.center)].Update(gameTime); } if (EntityWithinBounds(CellSpacePartition.GetLeftPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetLeftPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetLeftPartition(Camera.Instance.center)].Update(gameTime); } if (EntityWithinBounds(CellSpacePartition.GetBottomLeftPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetBottomLeftPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetBottomLeftPartition(Camera.Instance.center)].Update(gameTime); } if (EntityWithinBounds(CellSpacePartition.GetBottomPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetBottomPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetBottomPartition(Camera.Instance.center)].Update(gameTime); } if (EntityWithinBounds(CellSpacePartition.GetBottomRightPartition(Camera.Instance.center))) { AddCellToUpdate(CellSpacePartition.GetBottomRightPartition(Camera.Instance.center)); //cellSpacePartition.dynamicCells[CellSpacePartition.GetBottomRightPartition(Camera.Instance.center)].Update(gameTime); } for (int i = 0; i < _partitionsToUpdate.Count; i++) { cellSpacePartition.dynamicCells[_partitionsToUpdate[i]].Update(gameTime); } // clear the partitions after every update _partitionsToUpdate.Clear(); }
public World(int width, int height) { Instance = this; PhysicsWorld = new PhysicsWorld(width, height, 4); cellSpacePartition = new CellSpacePartition(width, height); }
// Use this for initialization void Start() { counter = 1; isHidden = false; windowRect = new Rect(20, 20, Screen.width - 100, Screen.height - 100); isPaused = false; textFieldString = "34"; goThru = false; runDump = true; cp = GameObject.Find("Container").gameObject.GetComponent <Eat> (); cell = GameObject.Find("Container").gameObject.GetComponent <CellSpacePartition> (); domDictionary = GameObject.Find("Container").gameObject.GetComponent <CellSpacePartition> ().getDictionary(); showFL = false; //fModule = cp.getFuzzyModule (); createDictionary = false; tempDouble = new double[2]; tempDouble = cell.getTempDouble(); //m_Vehicles = GameObject.Find ("Container").gameObject.GetComponent<CellSpacePartition> ().getVehicles (); //m_WanderList = GameObject.Find ("Container").gameObject.GetComponent<CellSpacePartition> ().getWanderList (); //m_PlantList = GameObject.Find ("Container").gameObject.GetComponent<CellSpacePartition> ().getPlantList (); for (int j = 0; j < 4; j++) { variables[j] = ""; } for (int d = 0; d < 25; d++) { tempNum[d] = ""; } for (int d = 0; d < 25; d++) { aNumber[d] = 0.0d; } consequentDictionary.Add("eTarget_Close, VeryHungry, HighLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Close, VeryHungry, MediumLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Close, VeryHungry, NoLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Close, Hungry, HighLibido", "Desirable"); consequentDictionary.Add("eTarget_Close, Hungry, MediumLibido", "Desirable"); consequentDictionary.Add("eTarget_Close, Hungry, NoLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Close, NotHungry, HighLibido", "Undesirable"); consequentDictionary.Add("eTarget_Close, NotHungry, MediumLibido", "Undesirable"); consequentDictionary.Add("eTarget_Close, NotHungry, NoLibido", "Undesirable"); consequentDictionary.Add("eTarget_Medium, VeryHungry, HighLibido", "Desirable"); consequentDictionary.Add("eTarget_Medium, VeryHungry, MediumLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Medium, VeryHungry, NoLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Medium, Hungry, HighLibido", "Desirable"); consequentDictionary.Add("eTarget_Medium, Hungry, MediumLibidoy", "Desirable"); consequentDictionary.Add("eTarget_Medium, Hungry, NoLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Medium, NotHungry, HighLibido", "Undesirable"); consequentDictionary.Add("eTarget_Medium, NotHungry, MediumLibido", "Undesirable"); consequentDictionary.Add("eTarget_Medium, NotHungry, NoLibido", "Undesirable"); consequentDictionary.Add("eTarget_Far, VeryHungry, HighLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Far, VeryHungry, MediumLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Far, VeryHungry, NoLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Far, Hungry, HighLibido", "Desirable"); consequentDictionary.Add("eTarget_Far, Hungry, MediumLibido", "Desirable"); consequentDictionary.Add("eTarget_Far, Hungry, NoLibido", "VeryDesirable"); consequentDictionary.Add("eTarget_Far, NotHungry, HighLibido", "Undesirable"); consequentDictionary.Add("eTarget_Far, NotHungry, MediumLibido", "Undesirable"); consequentDictionary.Add("eTarget_Far, NotHungry, NoLibido", "Undesirable"); //sex consequentDictionary.Add("mTarget_Close, VeryHungry, HighLibido", "MediumSex"); consequentDictionary.Add("mTarget_Close, VeryHungry, MediumLibido", "MediumSex"); consequentDictionary.Add("mTarget_Close, VeryHungry, NoLibido", "NoSex"); consequentDictionary.Add("mTarget_Close, Hungry, HighLibido", "HighSex"); consequentDictionary.Add("mTarget_Close, Hungry, MediumLibido", "MediumSex"); consequentDictionary.Add("mTarget_Close, Hungry, NoLibido", "NoSex"); consequentDictionary.Add("mTarget_Close, NotHungry, HighLibido", "HighSex"); consequentDictionary.Add("mTarget_Close, NotHungry, MediumLibido", "HighSex"); consequentDictionary.Add("mTarget_Close, NotHungry, NoLibido", "NoSex"); consequentDictionary.Add("mTarget_Medium, VeryHungry, HighLibido", "HighSex"); //***this one affects program alot!!! consequentDictionary.Add("mTarget_Medium, VeryHungry, MediumLibido", "NoSex"); consequentDictionary.Add("mTarget_Medium, VeryHungry, NoLibido", "NoSex"); consequentDictionary.Add("mTarget_Medium, Hungry, HighLibido", "HighSex"); consequentDictionary.Add("mTarget_Medium, Hungry, MediumLibido", "MediumSex"); consequentDictionary.Add("mTarget_Medium, Hungry, NoLibido", "NoSex"); consequentDictionary.Add("mTarget_Medium, NotHungry, HighLibido", "HighSex"); consequentDictionary.Add("mTarget_Medium, NotHungry, MediumLibido", "MediumSex"); consequentDictionary.Add("mTarget_Medium, NotHungry, NoLibido", "NoSex"); consequentDictionary.Add("mTarget_Far, VeryHungry, HighLibido", "MediumSex"); consequentDictionary.Add("mTarget_Far, VeryHungry, MediumLibido", "NoSex"); consequentDictionary.Add("mTarget_Far, VeryHungry, NoLibido", "NoSex"); consequentDictionary.Add("mTarget_Far, Hungry, HighLibido", "MediumSex"); consequentDictionary.Add("mTarget_Far, Hungry, MediumLibido", "MediumSex"); consequentDictionary.Add("mTarget_Far, Hungry, NoLibido", "NoSex"); consequentDictionary.Add("mTarget_Far, NotHungry, HighLibido", "HighSex"); consequentDictionary.Add("mTarget_Far, NotHungry, MediumLibido", "MediumSex"); consequentDictionary.Add("mTarget_Far, NotHungry, NoLibido", "NoSex"); fileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory); fileName += "/Myfile.txt"; File.WriteAllText(fileName, "Starting Data Dump!!!\n"); m_Vehicles = GameObject.Find("Container").gameObject.GetComponent <CellSpacePartition> ().getVehicles(); m_WanderList = GameObject.Find("Container").gameObject.GetComponent <CellSpacePartition> ().getWanderList(); hotLight = GameObject.Find("Container").gameObject.GetComponent <CellSpacePartition> ().getHotSphere(); }
//returns the distance between two cells based on the manhatan heuristic public static double Calculate(CellSpacePartition graph, Cell from, Cell to) { return(Math.Abs(to.ID % graph.NumberOfCellsHeight - from.ID % graph.NumberOfCellsHeight) + Math.Abs(from.ID / graph.NumberOfCellsHeight - to.ID / graph.NumberOfCellsHeight)); }
public SteeringScenario(int cx, int cy) { m_cxClient = cx; m_cyClient = cy; m_Vehicles.Clear(); m_Obstacles.Clear(); m_Walls.Clear(); GameWorld.Instance.cxClient = m_cxClient; GameWorld.Instance.cyClient = m_cyClient; GameWorld.Instance.Wrap = true; GameWorld.Instance.SpacePartitioningOn = true; objVehiclePen = new Pen(Color.Black); objDarkPen = new Pen(Color.SeaGreen, 2); objWallPen = new Pen(Color.DarkOrange); objObstaclePen = new Pen(Color.Orange); objTargetPen = new Pen(Color.Blue, 1); objRedPen = new Pen(Color.Red, 2); objCellPen = new Pen(Color.LightSkyBlue, 1); objGrayPen = new Pen(Color.LightSlateGray, 2); objPathPen = new Pen(Color.Blue, 2); objPathPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; objPathPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; m_pCellSpace = new CellSpacePartition(m_cxClient, m_cyClient, SteerParams.Instance.NumCellsX, SteerParams.Instance.NumCellsY, SteerParams.Instance.NumAgents); CreateObstacles(); CreateWalls(); CreateRandomPath(); //setup the agents for (int a = 0; a < SteerParams.Instance.NumAgents; ++a) { //determine a random starting position Vector2D SpawnPos = new Vector2D(cx / 2.0 + Utils.RandomClamped() * cx / 2.0, cy / 2.0 + Utils.RandomClamped() * cy / 2.0); double rotation = Utils.RandFloat() * Utils.TwoPi; MovingEntity pVehicle = new MovingEntity(SpawnPos, //initial position SteerParams.Instance.VehicleScale, new Vector2D(0, 0), SteerParams.Instance.MaxSpeed, new Vector2D(Math.Sin(rotation), -Math.Cos(rotation)), SteerParams.Instance.VehicleMass, new Vector2D(SteerParams.Instance.VehicleScale, SteerParams.Instance.VehicleScale), SteerParams.Instance.MaxTurnRatePerSecond, SteerParams.Instance.AppliedMaxSteeringForce()); pVehicle.Steering().FlockingOn(); pVehicle.Steering().WallAvoidanceOn(); pVehicle.Steering().ObstacleAvoidanceOn(); m_Vehicles.Add(pVehicle); //add it to the cell subdivision m_pCellSpace.AddEntity(pVehicle); } // Turn the last one into a shark! int sharkie = SteerParams.Instance.NumAgents - 1; m_Vehicles[sharkie].Steering().FlockingOff(); m_Vehicles[sharkie].SetScale(new Vector2D(6, 6)); m_Vehicles[sharkie].MaxSpeed = 80; m_intSharkieID = m_Vehicles[sharkie].ID(); setNextPursuitTarget(); for (int a = 0; a < SteerParams.Instance.NumAgents - 1; ++a) { m_Vehicles[a].Steering().EvadeOn(m_Vehicles[sharkie]); } GameWorld.Instance.Agents = m_Vehicles; GameWorld.Instance.Walls = m_Walls; GameWorld.Instance.Obstacles = m_Obstacles; GameWorld.Instance.CellSpaces = m_pCellSpace; }