/// <summary> /// Tests if this object collides with another object based on the list of collision circles /// </summary> /// <returns>true</returns> /// <c>false</c> /// <param name="TestRegions">Test regions.</param> public bool TestCollision(IEnumerable <CollisionRegion> TestRegions) { List <CollisionRegion> MyRegionsList = (List <CollisionRegion>)GetCollisionRegions(); foreach (CollisionRegion CurrRegion in TestRegions) { foreach (CollisionRegion MyRegion in MyRegionsList) { if (MyRegion.Type == CollideType.Circle) { if (CurrRegion.Type == CollideType.Circle) { if (MGMath.TestCircleCollisions(CurrRegion.Origin, CurrRegion.Radius, MyRegion.Origin, MyRegion.Radius) == true) { return(true); } } else //CurrRegion is a rectangle (MyRegion is Circle) { if (MGMath.TestCircleRectangleCollision(CurrRegion.Origin, CurrRegion.RectOffsets, MyRegion.Origin, MyRegion.Radius) == true) { return(true); } } } else //My Region is a rectangle { if (CurrRegion.Type == CollideType.Circle) { if (MGMath.TestCircleRectangleCollision(MyRegion.Origin, MyRegion.RectOffsets, CurrRegion.Origin, CurrRegion.Radius) == true) { return(true); } } else //CurrRegion is a rectangle (MyRegion is Rectangle) { if (MGMath.TestRectangleCollisions(CurrRegion.Origin, CurrRegion.RectOffsets, MyRegion.Origin, MyRegion.RectOffsets) == true) { return(true); } } } } } return(false); }
/// <summary> /// Add a new particle to be managed by this instance of the class /// </summary> /// <param name="Image">Image to display for the particle.</param> /// <param name="Top">Top coordinate for the particle's initial position.</param> /// <param name="Left">Left coordinate of hte particle's initial position.</param> /// <param name="Height">Initial Height in pixes of the particle.</param> /// <param name="Width">Initial Width of the particle.</param> /// <param name="Direction">Angle in radians of the Direction the particle is travelling.</param> /// <param name="Speed">Initial Speed of the particle.</param> /// <param name="Tint">Tint or color overlay for the particle.</param> public void AddParticle(Texture2D Image, float Top, float Left, int Height, int Width, float Direction, int Speed, Color Tint) { Vector2 SpeedComponents = MGMath.CalculateXYMagnitude(Direction, Speed); AddParticle(Image, Top, Left, Height, Width, SpeedComponents.X, SpeedComponents.Y, Tint); }
/// <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); }