{"id":364,"date":"2013-02-10T11:01:13","date_gmt":"2013-02-10T11:01:13","guid":{"rendered":"http:\/\/strikelimit.co.uk\/m\/?p=364"},"modified":"2013-02-17T12:09:54","modified_gmt":"2013-02-17T12:09:54","slug":"generating-planets-part-one-theory","status":"publish","type":"post","link":"http:\/\/strikelimit.co.uk\/m\/?p=364","title":{"rendered":"Generating Planets &#8211; Part One &#8211; Theory"},"content":{"rendered":"<p>Over the last two days I have been working on some code to generate planets in MonoGame, the content of this post however is general enough to be transferred to any language you wish to use.<\/p>\n<p>When I first started investigating planet generation I hoped to be able to transfer the code I developed for my square grid heightmap terrain (for which a biome map is pictured below) onto a sphere. The problem with this however is that you can&#8217;t wrap a square grid around a sphere without some distortion as you go towards the poles.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_222\" style=\"width: 515px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/strikelimit.co.uk\/m\/?attachment_id=222\" rel=\"attachment wp-att-222\"><img aria-describedby=\"caption-attachment-222\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-222 \" alt=\"A fully generated biome map\" src=\"http:\/\/strikelimit.co.uk\/m\/wp-content\/uploads\/2011\/08\/biomemap.png\" width=\"515\" height=\"334\" \/><\/a><p id=\"caption-attachment-222\" class=\"wp-caption-text\">A fully generated biome map<\/p><\/div>\n<p>Such distortion could be avoided if you want to wrap a texture around a sphere by stretching the image as you go towards the poles of the planet. If this is what you want to do, then check out <a title=\"Wikipedia - UV mapping\" href=\"http:\/\/en.wikipedia.org\/wiki\/UV_mapping\">this page<\/a>. As shown <a href=\"http:\/\/libnoise.sourceforge.net\/tutorials\/tutorial8.html\">here<\/a>, you could also use this to apply perlin noise to a planet.<\/p>\n<p>After some more digging however I found a method for making spheres which is much more suited\u00a0 to what I wanted &#8211; icosahedron subdivision.<\/p>\n<p>The icosahedron is the regular 3D solid with 20 faces, all of which are equilateral triangles. These shapes have several interesting properties, including that in the edge length 1 icosahedron the vertices lie on the corners of three golden rectangles.<\/p>\n<div style=\"width: 202px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" loading=\"lazy\" alt=\"\" src=\"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/9\/9c\/Icosahedron-golden-rectangles.svg\" width=\"202\" height=\"186\" \/><p class=\"wp-caption-text\">The vertices of an edge length 1 icosahedron lie on the corners of three golden rectangles.<\/p><\/div>\n<p>The important thing for us about this shape however is that each of these faces can be subdivided into four smaller equilateral triangles, and with some jiggery-pokery, we can push these out to the edges of a sphere. And because we subdivided the faces into more equilateral triangles, these too can be subdivided, and so on and so forth, in an iterative process that gets us ever closer to a sphere.<\/p>\n<div id=\"attachment_365\" style=\"width: 384px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/strikelimit.co.uk\/m\/?attachment_id=365\" rel=\"attachment wp-att-365\"><img aria-describedby=\"caption-attachment-365\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-365\" alt=\"icosahedron\" src=\"http:\/\/strikelimit.co.uk\/m\/wp-content\/uploads\/2013\/02\/icosahedron.png\" width=\"384\" height=\"96\" srcset=\"http:\/\/strikelimit.co.uk\/m\/wp-content\/uploads\/2013\/02\/icosahedron.png 384w, http:\/\/strikelimit.co.uk\/m\/wp-content\/uploads\/2013\/02\/icosahedron-300x75.png 300w\" sizes=\"(max-width: 384px) 100vw, 384px\" \/><\/a><p id=\"caption-attachment-365\" class=\"wp-caption-text\">An icosahedron and its first two subdivisions. Source: RJ Mathar (http:\/\/www.mpia-hd.mpg.de\/~mathar\/) via Wikipedia.<\/p><\/div>\n<p>A point on a surface of a sphere lies so that\u00a0<span style=\"font-size: xx-small;\">x<sup>2<\/sup> + y<sup>2<\/sup> + z<sup>2<\/sup> = r<sup>2<\/sup><\/span> so using this knowledge we can take the points on our icosahedron, and push them out to the surface of the sphere, allowing us to keep our subdivided shape spherical, opposed to a icosahedron but with a lot of triangles making up its faces.<\/p>\n<p>Some really nice C# code for this generation process can be found over at Andreas Kahler&#8217;s blog, <a href=\"http:\/\/blog.andreaskahler.com\/2009\/06\/creating-icosphere-mesh-in-code.html\">here<\/a>.<\/p>\n<p>My goal is to generate a planet, rather than a smooth sphere, and so here icosahedron subdivision has an important advantage over using distorted squares, and that is it can be used to generate fractal terrain as you subdivide, rather like how the diamond square or similar algorithms work for square grid based terrain &#8211; there will be more on this in my next post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the last two days I have been working on some code to generate planets in MonoGame, the content of this post however is general enough to be transferred to any language you wish to use. When I first started investigating planet generation I hoped to be able to transfer the code I developed for &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/strikelimit.co.uk\/m\/?p=364\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[40,42,43,45,44,41,5],"_links":{"self":[{"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=\/wp\/v2\/posts\/364"}],"collection":[{"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=364"}],"version-history":[{"count":3,"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=\/wp\/v2\/posts\/364\/revisions"}],"predecessor-version":[{"id":367,"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=\/wp\/v2\/posts\/364\/revisions\/367"}],"wp:attachment":[{"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=364"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/strikelimit.co.uk\/m\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}