I am working on a platformer game to play around with physics. Right now I just have collisions mostly working (they’re broken but fixing it is trivial) at https://c.gethopscotch.com/p/12xzzqnz24 and I intend to get characters and physics working soon.
I made this topic to have a single place to explain how I am thinking the project will work. To start that off, here is an explanation of how I am checking for collisions:
Each object has a position, size, and rotation. Each object is also a rectangle, though that is mostly for ease of coding and the collision system I use can be easily expanded to any size of convex polygon (I believe I have a slow but working implementation of this up to hexagons in https://c.gethopscotch.com/p/12ig8qg1up). Every time the object’s size or rotation changes, calculates where its vertices (corners) are relative to its center.
It then uses these corner offsets to determine the size of the smallest axis-aligned rectangle that covers it.
Each object that wants to allow other objects to check for collisions with it is then responsible for informing them about its location and size.
Each object that wants to check collision against something will somehow get this data and then will check for a collision by doing the following:
- Check if its axis aligned bounding box is overlapping the one of the object it is checking. I am not sure if this step is necessary but custom rules make it easy to remove if it ends up not being needed. (although this is harder with Custom rules completely ruin the order that rules are run in)
- If it is, then it checks if the objects are actually colliding (since they could be rotated) by using the “separating axis theorem”
At this point that is as far as I have gotten, but after this is where I will use the collision result to push characters out of walls and floors.