{"id":656,"date":"2013-11-23T17:41:16","date_gmt":"2013-11-23T16:41:16","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=656"},"modified":"2013-11-23T18:02:08","modified_gmt":"2013-11-23T17:02:08","slug":"vb-net-and-xna-article-12-pixel-collision-on-rotated-shapes","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=656","title":{"rendered":"VB.Net and XNA : Article 12 &#8211; Pixel collision on rotated shapes"},"content":{"rendered":"<p>We have seen in <a href=\"https:\/\/labalec.fr\/erwan\/?p=593\" target=\"_blank\">previous article 8<\/a> how to perform pixel collision.<br \/>\nAltough it worked perfectly, it only worked on non rotated shapes.<\/p>\n<p>But in some cases, your shape will rotate (like a car in a racing game &#8211; see <a href=\"https:\/\/labalec.fr\/erwan\/?p=615\" target=\"_blank\">article \u00ab\u00a0A racing car game\u00a0\u00bb<\/a>).<\/p>\n<p>To perform pixel collision for a rotated shape, here below the steps :<\/p>\n<p>-you first need to draw a rotated rectangle around your shape<br \/>\n-you then need to draw a bounding rectanle around that rotated rectangle (also called AABB : Axis Aligned Bounding Box)<br \/>\n-finally you can check whether 2 AABB intersects, and if so, if in the intersected region, we have 2 non transparent pixels (colliding) or not<\/p>\n<p>The source code : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA_DEMO_18.zip\">XNA_DEMO_18<\/a><br \/>\nNote : the project contains a class named primitives2d which is used only to draw lines\/rectangles around my objects.<\/p>\n<p>Lets illustrate this with some screenshots.<\/p>\n<p><strong>Lets rotate our rectangle along with my rocket<\/strong><br \/>\n<a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel2-300x234.png\" alt=\"pixel2\" width=\"300\" height=\"234\" class=\"alignnone size-medium wp-image-660\" srcset=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel2-300x234.png 300w, https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel2.png 816w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Lets define our bounding box<\/strong><br \/>\n<a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel3-300x234.png\" alt=\"pixel3\" width=\"300\" height=\"234\" class=\"alignnone size-medium wp-image-659\" srcset=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel3-300x234.png 300w, https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel3.png 816w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Despite bounding boxes intersecting, we dont detect collision yet<\/strong><br \/>\n<a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel4-300x234.png\" alt=\"pixel4\" width=\"300\" height=\"234\" class=\"alignnone size-medium wp-image-658\" srcset=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel4-300x234.png 300w, https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel4.png 816w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Here we go ! In the intersection region, there are 2 non transparent pixels<\/strong><br \/>\n<a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel5.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel5-300x234.png\" alt=\"pixel5\" width=\"300\" height=\"234\" class=\"alignnone size-medium wp-image-657\" srcset=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel5-300x234.png 300w, https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/pixel5.png 816w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have seen in previous article 8 how to perform pixel collision. Altough it worked perfectly, it only worked on non rotated shapes. But in some cases, your shape will rotate (like a car in a racing game &#8211; see article \u00ab\u00a0A racing car game\u00a0\u00bb). To perform pixel collision for a rotated shape, here below <a href='https:\/\/labalec.fr\/erwan\/?p=656' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,35],"tags":[],"class_list":["post-656","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-xna","category-34-id","category-35-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=656"}],"version-history":[{"count":9,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/656\/revisions"}],"predecessor-version":[{"id":671,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/656\/revisions\/671"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}