{"id":2324,"date":"2024-02-06T19:11:40","date_gmt":"2024-02-06T19:11:40","guid":{"rendered":"https:\/\/writingagame.com\/?p=2324"},"modified":"2026-03-23T04:16:16","modified_gmt":"2026-03-23T04:16:16","slug":"chapter-10-collision-detection","status":"publish","type":"post","link":"https:\/\/writingagame.com\/index.php\/2024\/02\/06\/chapter-10-collision-detection\/","title":{"rendered":"Chapter 10. Collision detection"},"content":{"rendered":"\n<p>Collision detection is the heartbeat of game logic. Essentially, any game is just a set of rules defining how objects behave when they interact. Think of a shell and a tank: when they meet, the shell is destroyed, the tank explodes, and the game state changes.<\/p>\n\n\n\n<p>But how do we mathematically determine if two objects have actually met?<\/p>\n\n\n\n<p><strong>The Search for Precision<\/strong><\/p>\n\n\n\n<p>The first approach that usually comes to mind is measuring the <strong>center-to-center distance<\/strong>. if the distance is less than the sum of the objects&#8217; radii, you have a collision. This is a perfect solution for billiard balls, but it fails for almost any other shape.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>What about <strong>Bounding Box<\/strong> overlap? This is a standard industry shortcut, but it remains very approximate. Consider this scenario:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b04\/c10\/01.jpg\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>As the example shows, the boxes overlap, but the objects do not. So we need something better. My preferred solution is to measure the distance between the <strong>principal axes<\/strong> (the longest spans) of the objects. It&#8217;s called <em>Axis-Aligned Bounding Boxes (AABB)<\/em>.<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b04\/c10\/02.jpg\"><\/p>\n\n\n\n<p>If the distance between these segments is less than the sum of the objects&#8217; radii, we have a definitive collision.<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/writingagame.com\/img\/b04\/c10\/03.jpg\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Implementation &amp; Logic<\/strong><\/p>\n\n\n\n<p>You can find the full source code on:&nbsp;<a href=\"https:\/\/github.com\/bkantemir\/_wg_410\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/bkantemir\/_wg_410<\/a><\/p>\n\n\n\n<p>In the demo below, cars are placed randomly, causing many to overlap. The program detects these collisions and physically &#8220;resolves&#8221; them by pulling the objects apart:<\/p>\n\n\n\n<iframe loading=\"lazy\" width=\"100%\" height=\"400\" src=\"https:\/\/www.youtube.com\/embed\/nDV50SSAbeg?controls=0&amp;autoplay=1&amp;loop=1&amp;playlist=nDV50SSAbeg\" title=\"Collision detection demo\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>The Workflow:<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>When loading model <em>ModelLoader::loadModelStandard()<\/em>, in the end, it calls function <em>buildGabaritesFromDrawJobs()<\/em> and stores initial dimensions in <em>SceneSubj::gabaritesOnLoad<\/em> <\/li>\n\n\n\n<li>In the main cycle, in <em>TheApp::drawFrame()<\/em> for each object it calls <em>Gabarites::fillGabarites()<\/em>, which calculates longest axis position, size and orientation.<\/li>\n\n\n\n<li>And finally, we call <em>SceneSubj::checkCollisions()<\/em>, which compares them, and in case of collision calls corresponding reaction.<\/li>\n<\/ol>\n\n\n\n<p>Everything is pretty straight forward, though implementation sometimes doesn&#8217;t look simple, particularly distance calculation between line segments.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"mb-2\">Collision detection is the heartbeat of game logic. Essentially, any game is just a set of rules defining how objects behave when they interact. Think of a shell and a tank: when they meet, the shell is destroyed, the tank explodes, and the game state changes. But how do we mathematically determine if two objects [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2387,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cross-platform-3d"],"_links":{"self":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/2324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/comments?post=2324"}],"version-history":[{"count":16,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/2324\/revisions"}],"predecessor-version":[{"id":3599,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/posts\/2324\/revisions\/3599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/media\/2387"}],"wp:attachment":[{"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/media?parent=2324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/categories?post=2324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/writingagame.com\/index.php\/wp-json\/wp\/v2\/tags?post=2324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}