//returns true if the bbox described by other intersects with this one public bool isOverlappedWith(InvertedAABBox2D other) { return(!((other.Top() > Bottom()) || (other.Bottom() < Top()) || (other.Left() > Right()) || (other.Right() < Left()))); }
//----------------------- CalculateNeighbors ---------------------------- // // This must be called to create the collection of neighbors. This method // examines each cell within range of the target, If the // cells contain entities then they are tested to see if they are situated // within the target's neighborhood region. If they are they are added to // neighbor list //------------------------------------------------------------------------ public void CalculateNeighbors(Vector2D TargetPos, double QueryRadius) { //create the query box that is the bounding box of the target's query area InvertedAABBox2D QueryBox = new InvertedAABBox2D(TargetPos - new Vector2D(QueryRadius, QueryRadius), TargetPos + new Vector2D(QueryRadius, QueryRadius)); m_Neighbors.Clear(); Double dblDoubleRadius = QueryRadius * QueryRadius; //iterate through each cell and test to see if its bounding box overlaps //with the query box. If it does and it also contains entities then make further proximity tests. foreach (Cell curCell in m_Cells) { if ((curCell.Members.Count != 0) && curCell.BBox.isOverlappedWith(QueryBox)) { //add any entities found within query radius to the neighbor list foreach (MovingEntity objEntity in curCell.Members) { if (Vector2D.Vec2DDistanceSq(objEntity.Pos, TargetPos) < dblDoubleRadius) { m_Neighbors.Add(objEntity); } } } } // next cell }
private void renderBox(InvertedAABBox2D objBox, Graphics objGraphics, Pen objPen) { objGraphics.DrawLine(objPen, (int)objBox.Left(), (int)objBox.Top(), (int)objBox.Right(), (int)objBox.Top()); objGraphics.DrawLine(objPen, (int)objBox.Left(), (int)objBox.Bottom(), (int)objBox.Right(), (int)objBox.Bottom()); objGraphics.DrawLine(objPen, (int)objBox.Left(), (int)objBox.Top(), (int)objBox.Left(), (int)objBox.Bottom()); objGraphics.DrawLine(objPen, (int)objBox.Right(), (int)objBox.Top(), (int)objBox.Right(), (int)objBox.Bottom()); }
//this method calculates all a target's neighbors and stores them in //the neighbor vector. After you have called this method use the begin, //next and end methods to iterate through the vector. public void CalculateNeighbors(Vector2 targetPos, float queryRadius) { if (neighbors_.Length <= 0) { Debug.LogError("CellSpacePartition::CalculateNeighbors: invalid neighbors since it is empty"); return; } //create an iterator and set it to the beginning of the neighbor vector int currNeighborIndex = 0; //NavGraphNode curNbor = neighbors_.begin(); //create the query box that is the bounding box of the target's query //area InvertedAABBox2D queryBox = new InvertedAABBox2D(targetPos - new Vector2(queryRadius, queryRadius), targetPos + new Vector2(queryRadius, queryRadius)); //iterate through each cell and test to see if its bounding box overlaps //with the query box. If it does and it also contains entities then //make further proximity tests. Cell currCell = null; NavGraphNode currNode = null; for (int i = 0; i < cells_.Count; ++i) { currCell = cells_[i]; //test to see if this cell contains members and if it overlaps the //query box if (currCell.bbox.isOverlappedWith(queryBox) && currCell.members.Count > 0) { //add any entities found within query radius to the neighbor list //std::list<NavGraphNode>::iterator it = currCell->memebers.begin(); for (int j = 0; j < currCell.members.Count; ++j) { currNode = currCell.members[j]; if ((currNode.Position() - targetPos).sqrMagnitude < queryRadius * queryRadius) { neighbors_[currNeighborIndex++] = currNode; //*curNbor++ = *it; } } } } //next cell //mark the end of the list with a zero. neighbors_[currNeighborIndex] = null; }
//returns true if the bbox described by other intersects with this one public bool isOverlappedWith( InvertedAABBox2D other) { return !( (other.Top() > Bottom()) || (other.Bottom() < Top()) || (other.Left() > Right()) || (other.Right() < Left())); }
public Cell(Vector2 topleft, Vector2 botright) { bbox = new InvertedAABBox2D(topleft, botright); members = new List<NavGraphNode>(); }
//this method calculates all a target's neighbors and stores them in //the neighbor vector. After you have called this method use the begin, //next and end methods to iterate through the vector. public void CalculateNeighbors(Vector2 targetPos, float queryRadius) { if ( neighbors_.Length <= 0 ) { Debug.LogError ( "CellSpacePartition::CalculateNeighbors: invalid neighbors since it is empty" ); return ; } //create an iterator and set it to the beginning of the neighbor vector int currNeighborIndex = 0; //NavGraphNode curNbor = neighbors_.begin(); //create the query box that is the bounding box of the target's query //area InvertedAABBox2D queryBox = new InvertedAABBox2D( targetPos - new Vector2(queryRadius, queryRadius), targetPos + new Vector2(queryRadius, queryRadius)); //iterate through each cell and test to see if its bounding box overlaps //with the query box. If it does and it also contains entities then //make further proximity tests. Cell currCell = null; NavGraphNode currNode = null; for ( int i=0; i<cells_.Count; ++i ) { currCell = cells_[i]; //test to see if this cell contains members and if it overlaps the //query box if (currCell.bbox.isOverlappedWith(queryBox) && currCell.members.Count > 0) { //add any entities found within query radius to the neighbor list //std::list<NavGraphNode>::iterator it = currCell->memebers.begin(); for ( int j=0; j<currCell.members.Count; ++j ) { currNode = currCell.members[j]; if ( ( currNode.Position() - targetPos ).sqrMagnitude < queryRadius * queryRadius ) { neighbors_[currNeighborIndex++] = currNode; //*curNbor++ = *it; } } } } //next cell //mark the end of the list with a zero. neighbors_[currNeighborIndex] = null; }
public Cell(Vector2 topleft, Vector2 botright) { bbox = new InvertedAABBox2D(topleft, botright); members = new List <NavGraphNode>(); }
public Cell(Vector2D topleft, Vector2D botright) { BBox = new InvertedAABBox2D(topleft, botright); Members = new List <MovingEntity>(); }
public void Render(Graphics objGraphics) { if (isFollowPathOn()) { MovingEntity objSharkie = getVehicleByID(m_intSharkieID); RenderPath2D(objSharkie.Steering().GetPath(), objGraphics, objPathPen); } foreach (MovingEntity objVehicle in m_Vehicles) { Pen objDrawPen = objVehiclePen; if (isPursuitOn() && (objVehicle.ID() == m_intVictimID)) { objDrawPen = objTargetPen; } if (m_blnNonePenetrationOn) { List <MovingEntity> ListTouched = MovingEntity.EnforceNonPenetrationConstraint(objVehicle, m_Vehicles); if (ListTouched.Count > 0) { if (objVehicle.ID() == m_intSharkieID) { objDrawPen = objRedPen; } else { objDrawPen = objDarkPen; } } } RenderVehicle(objVehicle, objGraphics, objDrawPen); if (m_blnRenderAids) { if ((objVehicle.ID() == m_intSharkieID)) { Vector2D vecForce = (Vector2D)(objVehicle.Steering().Force() / SteerParams.Instance.SteeringForceTweaker); vecForce.X = (vecForce.X * 2) * objVehicle.Scale().X; vecForce.Y = (vecForce.Y * 2) * objVehicle.Scale().Y; objGraphics.DrawLine(objRedPen, (PointF)objVehicle.Pos, (PointF)(objVehicle.Pos + vecForce)); objGraphics.DrawEllipse(objVehiclePen, (int)(objVehicle.Pos.X - SteerParams.Instance.ViewDistance), (int)(objVehicle.Pos.Y - SteerParams.Instance.ViewDistance), (int)SteerParams.Instance.ViewDistance * 2, (int)SteerParams.Instance.ViewDistance * 2); renderDetectionBox(objVehicle, objGraphics, objVehiclePen); } else if ((objVehicle.ID() == m_intVictimID) && GameWorld.Instance.SpacePartitioningOn && isPursuitOn()) { InvertedAABBox2D box = new InvertedAABBox2D(objVehicle.Pos - new Vector2D(SteerParams.Instance.ViewDistance, SteerParams.Instance.ViewDistance), objVehicle.Pos + new Vector2D(SteerParams.Instance.ViewDistance, SteerParams.Instance.ViewDistance)); renderBox(box, objGraphics, objTargetPen); GameWorld.Instance.CellSpaces.CalculateNeighbors(objVehicle.Pos, SteerParams.Instance.ViewDistance); foreach (MovingEntity objNeighbour in GameWorld.Instance.CellSpaces.ListOfNeighbours()) { if (objNeighbour.ID() != m_intSharkieID) { RenderObstacle(objNeighbour, objGraphics, objGrayPen); } } objGraphics.DrawEllipse(objGrayPen, (int)(objVehicle.Pos.X - SteerParams.Instance.ViewDistance), (int)(objVehicle.Pos.Y - SteerParams.Instance.ViewDistance), (int)SteerParams.Instance.ViewDistance * 2, (int)SteerParams.Instance.ViewDistance * 2); } } } if (GameWorld.Instance.SpacePartitioningOn) { foreach (Cell objCell in m_pCellSpace.ListOfCells()) { renderCell(objCell, objGraphics, objCellPen, true); } } foreach (Wall2D objWall in m_Walls) { RenderWall2D(objWall, objGraphics, objWallPen, true); } foreach (BaseGameEntity objObs in m_Obstacles) { RenderObstacle(objObs, objGraphics, objObstaclePen); } if (!isPursuitOn() && !isFollowPathOn()) { RenderTarget(GameWorld.Instance.TargetPos, objGraphics, objTargetPen); } }