{"author_link":"\/users\/daanvanyperen","author_name":"daanvanyperen","author_uid":"daanvanyperen","comments":[],"epoch":1494109486,"event":"LD38","format":"md","ldjam_node_id":30413,"likes":9,"metadata":{"p_key":"92967","p_author":"daanvanyperen","p_authorkey":"1006977","p_urlkey":"305885","p_title":"Little Fortune Planet Breakdown - Part 2: Gravity of the simulation, man!","p_cat":"LDJam ","p_event":"LD38","p_time":"1494109486","p_likes":"9","p_comments":"0","p_status":"WAYBACK","us_key":"1006977","us_name":"daanvanyperen","us_username":"daanvanyperen","event_start":"1492732800","event_key":"69","event_name":"LD 38"},"node":{"_collation":{"body_sanitizer":"TextUtils::SanitizeHTML via existing importer","event":"LD38","removed_author":false},"_superparent":9405,"_trust":1,"author":6977,"body":"**We're eager to get started after several hours of [Part 1: Brainstorming](https:\/\/ldjam.com\/events\/ludum-dare\/38\/little-fortune-planet\/little-fortune-plane-breakdown-part-1-brainstorming)** so lets consider what it takes to simulate a world in an [Entity-component system (ECS)](https:\/\/github.com\/junkdog\/artemis-odb\/wiki\/Introduction-to-Entity-Systems)?\n\n**Developers starting with ECS typically struggle what to implement as entities, and what to exclude.**\n\nWe could simulate each particle with an entity and use a physics engine, but since we\u2019d have to check neighboring cells constantly and we\u2019d lose a lot of time understanding a third party physics engine, a simple two dimensional array feels a lot less risky. The grid size is static, which means no real lifecycle management is necessary.\n\nOur world simulation system iterates over each cell, passing cell state to a behavior subsystem for magma, air, dirt, etc, and apply changes in a second pass. Subsystems add modularity in exchange for a small performance hit.\n\nBut since most simulation requires some gravity, **we first need to figure out what direction is \u2018down\u2019 for each location.**\nCalculating gravity live for 250000+ cells could become a bit of a performance problem, so instead we could precalculate a 2d array mask with the direction of the core of the planet. A bit premature perhaps, but with limited time got to go with the guts!\n\n![image15.png](\/\/\/raw\/14b\/1\/z\/4052.png)\n\n**Initial naive solution** we could calculate the direct angle by subtracting each cell x,y coordinates from the planet center, and determining the angle of the resulting vector. In effect, we end up with a grid of \u2018downward\u2019 directions. So let's run it:\n\n![image5.gif](\/\/\/raw\/14b\/1\/z\/4053.gif)\n\n**Uh-oh. The planet ended up as a octagon.**\n\nWe end up with 8 Intercardinal sectors making gravity direction (for example) always left in the right section, while if you\u2019d draw a pixel line from a cell to the core it might go left left, up, left left, up. Sort of like what you would get with Bresenham's line algorithm.\n\n![832px-Bresenham.svg.png](\/\/\/raw\/14b\/1\/z\/4056.png)\n\nSince we basically want things to fall from circumference of the planet to the center, why don\u2019t we draw loads of lines using the Bresenham's line algorithm from core to circumference and use each previous pixel as the direction of gravity?\n\n\n![image16.png](\/\/\/raw\/14b\/1\/z\/4054.png)\n\n**We end up with this:**\n\n![image20.gif](\/\/\/raw\/14b\/1\/z\/4055.gif)\n\n**A lot better! It\u2019s worth getting quick-and-dirty when not too mathematically inclined.**\n\nNow we have gravity, we can move on to simulating something interesting!\n\n**[Read part 3: Simulation time](https:\/\/ldjam.com\/events\/ludum-dare\/38\/little-fortune-planet\/little-fortune-planet-breakdown-part-3-simulation-time) or [play it here](https:\/\/ldjam.com\/events\/ludum-dare\/38\/little-fortune-planet)!**","comments":3,"comments-timestamp":"2017-05-07T00:47:35Z","created":"2017-05-06T22:02:35Z","files":[],"files-timestamp":0,"id":30413,"love":9,"love-timestamp":"2017-05-09T17:38:38Z","meta":[],"modified":"2017-05-09T17:38:38Z","name":"Little Fortune Planet Breakdown - Part 2: Gravity of the simulation, man!","node-timestamp":"2017-05-07T08:53:50Z","parent":21490,"parents":[1,5,9,9405,21490],"path":"\/events\/ludum-dare\/38\/little-fortune-planet\/little-fortune-planet-breakdown-part-2-gravity-of-the-simulation-man","published":"2017-05-06T22:24:46Z","scope":"public","slug":"little-fortune-planet-breakdown-part-2-gravity-of-the-simulation-man","subsubtype":"","subtype":"","type":"post","version":79994},"node_metadata":{"n_key":"30413","n_urlkey":"305885","n_parent":"21490","n_path":"\/events\/ludum-dare\/38\/little-fortune-planet\/little-fortune-planet-breakdown-part-2-gravity-of-the-simulation-man","n_slug":"little-fortune-planet-breakdown-","n_type":"post","n_subtype":"","n_subsubtype":"","n_author":"6977","n_created":"1494108155","n_modified":"1494351518","n_version":"79994","n_status":"WAYBACK"},"source_url":"https:\/\/ldjam.com\/events\/ludum-dare\/38\/little-fortune-planet\/little-fortune-planet-breakdown-part-2-gravity-of-the-simulation-man","text":"**We're eager to get started after several hours of [Part 1: Brainstorming](https:\/\/ldjam.com\/events\/ludum-dare\/38\/little-fortune-planet\/little-fortune-plane-breakdown-part-1-brainstorming)** so lets consider what it takes to simulate a world in an [Entity-component system (ECS)](https:\/\/github.com\/junkdog\/artemis-odb\/wiki\/Introduction-to-Entity-Systems)?\n\n**Developers starting with ECS typically struggle what to implement as entities, and what to exclude.**\n\nWe could simulate each particle with an entity and use a physics engine, but since we\u2019d have to check neighboring cells constantly and we\u2019d lose a lot of time understanding a third party physics engine, a simple two dimensional array feels a lot less risky. The grid size is static, which means no real lifecycle management is necessary.\n\nOur world simulation system iterates over each cell, passing cell state to a behavior subsystem for magma, air, dirt, etc, and apply changes in a second pass. Subsystems add modularity in exchange for a small performance hit.\n\nBut since most simulation requires some gravity, **we first need to figure out what direction is \u2018down\u2019 for each location.**\nCalculating gravity live for 250000+ cells could become a bit of a performance problem, so instead we could precalculate a 2d array mask with the direction of the core of the planet. A bit premature perhaps, but with limited time got to go with the guts!\n\n![image15.png](\/\/\/raw\/14b\/1\/z\/4052.png)\n\n**Initial naive solution** we could calculate the direct angle by subtracting each cell x,y coordinates from the planet center, and determining the angle of the resulting vector. In effect, we end up with a grid of \u2018downward\u2019 directions. So let's run it:\n\n![image5.gif](\/\/\/raw\/14b\/1\/z\/4053.gif)\n\n**Uh-oh. The planet ended up as a octagon.**\n\nWe end up with 8 Intercardinal sectors making gravity direction (for example) always left in the right section, while if you\u2019d draw a pixel line from a cell to the core it might go left left, up, left left, up. Sort of like what you would get with Bresenham's line algorithm.\n\n![832px-Bresenham.svg.png](\/\/\/raw\/14b\/1\/z\/4056.png)\n\nSince we basically want things to fall from circumference of the planet to the center, why don\u2019t we draw loads of lines using the Bresenham's line algorithm from core to circumference and use each previous pixel as the direction of gravity?\n\n\n![image16.png](\/\/\/raw\/14b\/1\/z\/4054.png)\n\n**We end up with this:**\n\n![image20.gif](\/\/\/raw\/14b\/1\/z\/4055.gif)\n\n**A lot better! It\u2019s worth getting quick-and-dirty when not too mathematically inclined.**\n\nNow we have gravity, we can move on to simulating something interesting!\n\n**[Read part 3: Simulation time](https:\/\/ldjam.com\/events\/ludum-dare\/38\/little-fortune-planet\/little-fortune-planet-breakdown-part-3-simulation-time) or [play it here](https:\/\/ldjam.com\/events\/ludum-dare\/38\/little-fortune-planet)!**","title":"Little Fortune Planet Breakdown - Part 2: Gravity of the simulation, man!","wayback_source":[]}