Functions and Features:
This 2D physic system is a sub system of the engine project I've worked on since the beginning of the Fall 2020 semester.
The system contains functions and features mainly from two parts, 2D collision checking and 2D motion.
2D Collision:
Supports collision checking for rectangular and circular shaped colliders.
Resolving collisions.
Nonblocking trigger type overlapping checking for rectangular and circular shaped colliders.
Collision callbacks functions after collision.
2D Motion (manipulating the physics properties of rigid bodies):
Adding/setting linear force to a game object.
Adding/setting linear velocity to a game object.
Adding linear impulse to a game object.
Adding/setting angular velocity to a game object.
Setting the mass of a game object.
Moving objects in the scene according their velocity.
Resolving collisions.
Check out the sample breakout game to see how it can be used in a game:
To ensure this project is platform-independent, when choosing variable types, I made sure I only use types with a specified size.
To make the project user-friendly, I choose to allow users to create instances of colliders using a human-readable format. For more information on the human readable, see “How to Use”.
It was an interesting experience working on this project. I was able to apply some physics knowledge I learn before into practice, which is quite satisfying.
By working on the collision system, I’ve learned new algorithm on checking collisions, especially for the collision checking between a circle and a rectangle and resolving collisions. I had no good idea on how to do those before I did research. Right now, I can confidently say I can implement the whole system again using my own knowledge. The collision resolving part has a good amount of physics involved. I struggled quite a few hours on it with my rusted physics knowledge. But after I refreshed myself a bit on impulse and momentum, I finally figured it out.
Editing human-readable collider files: This physics engine supports two different types of colliders. The rectangular type and the circular type. Both types are instances of the sCollider struct, but their human-readable files are slightly different.
The following is a sample file of a rectangular collider. Other than some common fields like shape, isStatic, isTrigger and bounciness, a rectangular type collider file also specifies the bottom-left vertex and the top-right vertex of the rectangle.
The following is a sample file of a circular collider. Other than the common fields I mentioned above, a circular type collider file also specifies the origin and the radius of the circle.
By default, collider files should be named with a <.mcollider> extension. All collider files should be put under <MyGame_\Content\Colliders>.
Creating collider instances: Call the static factory function defined in sCollider struct to create a collider instance. After creation, you need to attach this collider to some GameObject instance.
To register or deregister a collider to the physics system. See functions RegisterCollider() and DeregisterCollider() defined in Physics.cpp.
Collision checking: Note that collision between static colliders is not checked. Collision won’t be resolved if any of the two colliders are trigger type (which means colliding with a trigger type collider won’t affect the state of a rigid body).
Collision callbacks: Two virtual methods are defined within the GameObject class. OnCollision() will be called when collision happens between two colliders. OnTrigger() will be called only for the trigger collider(s) when collision happens between a trigger type collision and another collider. To create your customized version of those function, you will need to create a sub class of the GameObject class and override those two functions.
2D motion: I’ve expanded the original sRigidBodyState struct for adding more interfaces of controlling the physics properties of a physics body. See sRigidBodyState.h and .cpp for more information.
If you have any other questions on how the system works or experience any bugs, contact me directly on slack.