/// <summary> /// Will update the object in order to allow editing of the collision vertexes /// Can be replaced in the inheritting class. It only needs called if the vertex editing is /// needed feature, otherwise it can be skipped. /// /// The return value must retain the purpose described here. The ObjectManager and /// Particle engines will attempt to purge this object if it returns false. /// </summary> /// <param name="CurrTime"></param> /// <returns>True indicates the object still exists, false means the update decided the /// object can be removed</returns> public virtual bool Update(GameTime CurrTime) { Rectangle Handle; int nVertCtr; Vector2 vVertPos; MouseState CurrMouse = Mouse.GetState(); if (cbVertexEdits == true) { if ((CurrMouse.LeftButton == ButtonState.Pressed) && (cnMouseVertex >= 0)) { //User is draging a vertex around vVertPos.X = CurrMouse.X; vVertPos.Y = CurrMouse.Y; cPolyGon.UpdateVertex(cnMouseVertex, vVertPos); CenterPoint = CenterPoint; } if ((CurrMouse.LeftButton == ButtonState.Pressed) && (cPriorMouse.LeftButton == ButtonState.Released)) { //User just clicked, see if they got a polygon vertex List <CollisionRegion> CollList = new List <CollisionRegion>(cPolyGon.GetCollisionRegions()); Handle.Width = nHandleWidth; Handle.Height = nHandleWidth; nVertCtr = 0; foreach (Vector2 vCurrVert in CollList[0].Vertexes) { Handle.X = (int)(vCurrVert.X - (nHandleWidth / 2)); Handle.Y = (int)(vCurrVert.Y - (nHandleWidth / 2)); if (MGMath.IsPointInRect(CurrMouse.Position, Handle) == true) { cnMouseVertex = nVertCtr; break; } nVertCtr += 1; } } if (CurrMouse.LeftButton == ButtonState.Released) { cnMouseVertex = -1; } } cPriorMouse = CurrMouse; //Raise the event handler for external logic if (Updating != null) { return(Updating.Invoke(this, CurrTime)); } else { return(true); //Returning true means the object should stick around } }
/// <summary> /// Tests if this object overlaps or collides with another region /// </summary> /// <param name="TestRegions">List of regions to test for collisions with</param> /// <returns>True if a collisions has occurred, false otherwise</returns> public bool TestCollision(IEnumerable <CollisionRegion> TestRegions) { foreach (CollisionRegion CurrReg in TestRegions) { switch (CurrReg.Type) { case CollideType.ConvexPolygon: Int32 nMeCtr, nThemCtr; //See if this polygon is hitting the other one foreach (Vector2 Vertex in cCollisionList.Vertexes) { if (MGMath.PointInConvexPolygon(Vertex, CurrReg.Vertexes) == true) { return(true); } } //See if the other polygon is inside this one foreach (Vector2 Vertex in CurrReg.Vertexes) { if (MGMath.PointInConvexPolygon(Vertex, cCollisionList.Vertexes) == true) { return(true); } } //Need a test for when edges completely span the other polygon. //Vertexes are all outside, edges intersect for (nMeCtr = 1; nMeCtr < cCollisionList.Vertexes.Count; nMeCtr += 1) { for (nThemCtr = 1; nThemCtr < CurrReg.Vertexes.Count; nThemCtr += 1) { if (MGMath.LineSegmentIntesection(cCollisionList.Vertexes[nMeCtr], cCollisionList.Vertexes[nMeCtr - 1], CurrReg.Vertexes[nThemCtr], CurrReg.Vertexes[nThemCtr - 1]) == true) { return(true); } } } return(false); case CollideType.Rectangle: Point Coord; Vector2 Corner; //See if this polygon is hitting the rectangle foreach (Vector2 Vertex in cCollisionList.Vertexes) { Coord.X = (int)Vertex.X; Coord.Y = (int)Vertex.Y; if (MGMath.IsPointInRect(Coord, CurrReg.RectOffsets) == true) { return(true); } } Corner.X = CurrReg.RectOffsets.X; Corner.Y = CurrReg.RectOffsets.Y; if (MGMath.PointInConvexPolygon(Corner, cCollisionList.Vertexes) == true) { return(true); } Corner.X = CurrReg.RectOffsets.X + CurrReg.RectOffsets.Width; Corner.Y = CurrReg.RectOffsets.Y; if (MGMath.PointInConvexPolygon(Corner, cCollisionList.Vertexes) == true) { return(true); } Corner.X = CurrReg.RectOffsets.X; Corner.Y = CurrReg.RectOffsets.Y + CurrReg.RectOffsets.Height; if (MGMath.PointInConvexPolygon(Corner, cCollisionList.Vertexes) == true) { return(true); } Corner.X = CurrReg.RectOffsets.X + CurrReg.RectOffsets.Width; Corner.Y = CurrReg.RectOffsets.Y + CurrReg.RectOffsets.Height; if (MGMath.PointInConvexPolygon(Corner, cCollisionList.Vertexes) == true) { return(true); } return(false); case CollideType.Circle: //Need to work out this math. //If any vertex of the polygon is closer to the circle center than the radius, collides //If a line from the center perpendicular to a side of the polygon hits the side and //is shorter than the radius of the circle, collide return(false); default: return(false); } } return(false); }