CollisionManager
public class CollisionManager
{
//...
This class contains a good number of static functions to check for collisions between various shapes, entities and solids
Methods
public static SRect GetSweptBroadphaseBox(PhysicsComponent e)
Gets the rectangle which wraps around the physicsComponents collisionBox and where it would hypothetically end up after applying velocity without being interrupted by collisions
public static bool AABBCheck(SRect b1, SRect b2)
Returns whether 2 rectangles are overlapping or not
public static bool CircleCheck(SCircle c1, SCircle c2)
Returns whether 2 circles are overlapping or not
public static bool AABBvsCircleCheck(SRect r, SCircle c)
Returns whether a rectangle and a circle overlap or not
public static bool LineVsObjectRects(SLine line, List<ObjectRect> objectRects, ref SVector closestIntersection, ref sfloat closestCollisionTime, ref GameObject gameObject)
Returns whether a line intersects any objectRects. If it does intersect, then closestIntersection point, closestCollisionTime(normalised to give a value between 0 and 1. 0 being the start of the line and 1 being the end) and which gameObject its colliding with are output via reference. This is similar to the concept of raycasting, we’re checking if a line interests some things and we want information on the thing we hit.
public static bool LineVsAABB(SLine line, SRect rect, ref SVector closestIntersection, ref sfloat closestCollisionTime)
Returns whether a line intersects a AxisAlignedBoundingBox(AABB, or rectangle in lamens speak). If it does, closestIntersection point and closestCollisionTime(between 0 and 1. 0 being the start of the line and 1 being the end) are output via reference.
public static bool LineCheck(SLine line1, SLine line2, ref SVector intersection, ref sfloat t)
Returns whether a line intersects another line or not. If so, the intersection point and intersection time (between 0 and 1. 0 being the start of line1 and 1 being the end of line1) are output via reference.
public static sfloat SweptAABB(PhysicsComponent e1, SRect otherBox, ref sfloat normalx, ref sfloat normaly)
Returns a collision Time between 0 and 1(0 meaning collision immediately, 1 meaning no collision along this things velocity path). This function takes a physics component to work out a hypothetical shape representing the physics components collisionbox’s starting position this frame and where it would be next frame with velocity if no collisions(the swept shape). It also takes otherBox, a rectangle from the world. If If these 2 shapes overlap, then our physicsComponent will collide with the otherBox rectangle. The collision time is returned and the normalx and y representing which side of the otherBox we ran into and passed out via reference.
This function is called from other functions and usually multiple times in a loop. For example a player object may have a physcisComponent and we may check to see if their movements collide with any of the many rectangles representing solid walls in the game world or even other solid entities like enemies.
public static void MendAnyNetworkEntityNaNSfloats(PhysicsComponent e1)
There are ways to break a soft float and have its value equal to NaN(not a number). This function makes sure if anything like this happened, then those numbers are zeroed out so the game does not crash on various calculations.
public static bool DoesRectCollideWithLevelSolids(SRect rect)
Returns whether this rectangle overlaps another of the solid rectangles from the current loaded level or not
public static void MoveEntityWithCollisions(PhysicsComponent e1)
When this version of the function is called, it collects all potential other solid objects from the level e.g walls, enemies, vehicles etc. It then passes e1(our physics component) and all these solid shapes onto the next version of this function. See below for more details
public static void MoveEntityWithCollisions(PhysicsComponent e1, List<SRect> rects)
Moves a gameobject via its physicsComponent’s information(using velocity or other forces etc) with respect to solid rectangles in the level(static or game entity). This function does the actual movement of the entity holding this physics component. So it will try move them until they hit an object.