This is a C# math library. It is built as the bare minimum to get up and running with a 2D game in C#. It is compatible with or without monogame. To compile with monogame, use the compiler directive "NOT_MONOGAME". This will provide versions of Microsoft.XNA.Framework.Vector2 and Microsoft.XNA.Framework.Point that do not require any external libraries (with reduced functionality).
usingSharpMath2;usingMicrosoft.XNA.Framework;vartriangle1=newPolygon2(new[]{newVector2(0,0),newVector2(1,1),newVector2(2,0)});vartriangle2=ShapeUtils.CreateCircle(1,segments=3);// this is not the same triangle as triangle1, this will be equilateralvaroctogon=ShapeUtils.CreateCircle(1,segments=8);// Check intersection of two entities, both of which have the same triangle bounds but one is// rotated at rotation1 and located at position1, where the other is rotated at rotation2 and // located at position2vartriangle=newPolygon2(new[]{newVector2(0,0),newVector2(1,1),newVector2(2,0)});// Rotation2 caches Math.Sin and Math.Cos of the given angle, so if you know you are going to reuse// rotations often (like 0) they should be cached (Rotation2.Zero is provided)varrotation1=Rotation2.Zero;varrotation2=Rotation2.Zero;// new Rotation2((float)(Math.PI / 6)) would be 30degreesvarposition1=newVector2(5,3);varposition2=newVector2(6,3);// Determine if the polygons overlap or touch: Polygon2.Intersects(triangle,triangle,position1,position2,rotation1,rotation2,false);// True// Determine if the polygons overlapPolygon2.Intersects(triangle,triangle,position1,position2,rotation1,rotation2,true);// False// Note that in the special case of no rotation (rotation1 == rotation2 == Rotation2.Zero) we can// use the shorter function definition by omitting the rotation parametersPolygon2.Intersects(triangle,triangle,position1,position2,true);// False// Suppose we have two entities, entity1 and entity2, both of which use the polygon "triangle" and are at position1, rotation1 and // position2, rotation2 respectively. If we are updating entity1 and we want to detect and handle collision with entity2 we would// do:// note we do not check for intersection first - while intersection is faster to check than intersection + MTV, it is not // faster to check intersection then intersection + MTV if you will need the MTV.// Note strict is not an option for MTV - if two triangles are touching but not overlapping then// it doesn't make sense to try and get an MTVTuple<Vector2,float>mtv=Polygon2.IntersectMTV(triangle,triangle,position1,position2,rotation1,rotation2);if(mtv!=null){// The two entites are colliding.position1+=mtv.Item1*mtv.Item2;// Polygon2.Intersects(triangle, triangle, position1, position2, rotation1, rotation2, true); -> False// Polygon2.Intersects(triangle, triangle, position1, position2, rotation1, rotation2, false); -> True}It is very common to need to check polygons against unrotated rectangles in square-grid systems. In this case
there are functions in Shape2 that provide these comparisons that is slightly faster than complete polygon to
polygon collision that you would get from ShapeUtils.CreateRectangle(width, height) rather than new Rect(minx, miny, maxx, maxy)
vartriangle=ShapeUtils.CreateCircle(1,segments=3);vartile=newRect2(0,0,1,1);// minX, minY, maxX, maxY NOT x, y, w, h.vartriPos=newVector2(3.3,4.1);vartriRot=newRotation2((float)(Math.PI/6));Vector2tmp=Vector2.Zero;// Vector2 is a struct so this is safeintxMin=(int)triPos.x;intxMax=(int)Math.Ceiling(triPos.x+triangle.LongestAxisLength);intyMin=(int)triPos.y;intyMax=(int)Math.Ceiling(triPos.y+triangle.LongestAxisLength);for(inty=yMin;y<=yMax;y++){tmp.Y=y;for(intx=xMin;x<=xMax;x++){tmp.X=x;varintersectsTileAtXY=Shape2.Intersects(triangle,tile,triPos,tmp,triRot,true);Console.Write($"({x},{y})={intersectsTileAtXY}")if(intersectsTileAtXY)Console.Write(" ");// true is 1 letter shorter than falseelseConsole.Write(" ");}Console.WriteLine();}Note that this is only faster for fairly complicated polygons (theoretical breakeven at 6 unique normals each). Further note that it's almost never faster for rotated polygons - finding the AABB for rotated polygons is not supported (though not complicated).
The provided AABB is most often used in UI elements which do not anticipate rotation and can have somewhat complicated polygons but don't have rotation, which is where AABBs shine.
varcomplicatedShape=ShapeUtils.CreateCircle(5);// radius 5, 32 segments// Note we are not providing rotation - rect2 does not support rotation // (use ShapeUtils.CreateRectangle for that, which returns a Polygon2)Rect2.Intersects(complicatedShape.AABB,complicatedShape.AABB,Vector2.Zero,newVector2(3,0),true);// TrueCircles have similiar functions to polygons. The only thing to note is that all API functions will use the top-left of the bounding box of the circle for the circles position, rather than the center of the circle. This makes switching things between circles and polygons easier in return for a very small performance cost.
varcircle=newCircle2(3);// The only argument is the radius of the circle.varanotherCircle=newCircle2(5);vartriangle=ShapeUtils.CreateCircle(2,segments=3);// Circle -> Circle collision using the same underlying circle objectCircle2.Intersects(circle,circle,Vector2.Zero,newVector(1,0),true);// True// Circle -> Circle collision can be done using just the radiusCircle2.Intersects(3.0f,3.0f,Vector2.Zero,newVector2(1,0),true);// Identical to above// Circle -> Polygon collision must pass in a circle, not the radius of the circleShape2.Intersects(circle,triangle,Vector2.Zero,newVector2(1,1),true);// True// Circle -> AABB collisionShape2.Intersects(circle,triangle.AABB,Vector2.Zero,newVector2(10,0),true);// FalseThis library is designed for when:
- You have only a few different polygon types
- You need to check collision on those polygon types when they are in rapidly changing positions and rotations.
For example in a 2D game where everything is either a triangle or hexagon, in this library you would only need to construct two polygons, then reuse those two polygons everywhere else. This allows the library to cache certain operations.
The library is designed such that changing rotations or position is fast, but the downside is when rotation or position does not change there is only a minor improvement in performance.
