How to create your own game engine with no special tools
A rigid body is just a shape defined by the user (that is, by you, the developer) that is implicitly defined to be non-deformable. In order to achieve such effects and follow along with natural intuition of how objects behave we'll use rigid bodies and a fair bit of math. Simple example of what impulse resolution can achieve. We also want objects with infinite mass to not move at all. In order to do this the mass, position, and velocity of each object must be taken into account somehow: we want large objects colliding with smaller ones to move a little bit during collision, and to send the small objects flying away. The idea behind impulse resolution is to use an impulse (instantaneous change in velocity) to separate objects found colliding. In this article we implicitly restrict rotation and use just AABBs and Circles, so the only degree of freedom we really need to consider is movement along the xy plane.īy resolving detected collisions we place a restriction upon movement such that objects cannot remain intersecting one another. In general an object within a physics engine has three main degrees of freedom (in two dimensions): movement in the xy plane and rotation. Collision resolution is the act of taking two objects who are found to be intersecting and modifying them in such a way as to not allow them to remain intersecting. Impulse resolution is a particular type of collision resolution strategy. In general multiplication is a much cheaper operation than taking the square root of a value. Return r < Distance( a.position, b.position )īool CirclevsCircleOptimized( Circle a, Circle b ) Return sqrt( (a.x - b.x)^2 + (a.y - b.y)^2 )īool CirclevsCircleUnoptimized( Circle a, Circle b ) Testing for whether or not two circles intersect is very simple: take the radii of the two circles and add them together, then check to see if this sum is greater than the distance between the two circles.Īn important optimization to make here is get rid of any need to use the square root operator: float Distance( Vec2 a, Vec2 b ) Here is what your circle structure ought to look like: struct Circle No separating axis found, therefor there is at least one overlapping axisĪ circle is represented by a radius and point. Exit with no intersection if found separated along an axis Here's a quick test taken from Real-Time Collision Detection by Christer Ericson, which makes use of the SAT: bool AABBvsAABB( AABB a, AABB b ) In order to tell whether two AABB shapes are intersecting you will need to have a basic understanding of the Separating Axis Theorem (SAT). The min point represents the lower bounds of the x and y axis, and max represents the higher bounds - in other words, they represent the top left and bottom right corners. This form allows an AABB to be represented by two points. There are a few different ways to represent an AABB, however this is my favorite: struct AABB However in the case of most games the AABB is used as a fundamental shape, and does not actually bound anything else. The AABB of a complex shape can be used as a simple test to see if more complex shapes inside the AABBs can possibly be intersecting. In general it is referred to as a "bounding box" because AABBs are used to bound other more complex shapes. This means it is a box that cannot rotate, and is always squared off at 90 degrees (usually aligned with the screen). Axis Aligned Bounding BoxesĪn Axis Aligned Bounding Box (AABB) is a box that has its four axes aligned with the coordinate system in which it resides. Knowing this, I would like to run through the topic very quickly as this section is not the focus of this article. There are quite a few articles and tutorials throughout the internet, including here on Tuts+, that cover collision detection. A basic understanding of simple vector math.A couple prerequisites for this article are: This article involves a fair amount of mathematics and geometry, and to a much lesser extent actual coding. Note: Although this tutorial is written using C++, you should be able to use the same techniques and concepts in almost any game development environment.