JSON files: my super dumb and technical week.

Whew. Sometimes game development requires spending a long time working on things that aren’t visible. I spend the better part of this week putting together the code to convert a box2d world and the objects that make up a creature (joints, muscles, hierarchy) into a json text string. Basically, something that can turn this:
creature
into this:

[[{"ID":1,"bodies":[2,6,14,22,30],"gravity":0,"joints":[38,41,44,47,50]},{"ID":2,"angle":0,"fxes":[4],"pos":3,"type":2},{"ID":3,"x":0,"y":0},{"ID":4,"dens":1,"fric":0.4,"rest":0.3,"shape":5},{"ID":5,"radius":1.5,"type":0},{"ID":6,"angle":2.148698261301898,"fxes":[8],"pos":7,"type":2},{"ID":7,"x":-1.5022363965040386,"y":2.3034291413061876},{"ID":8,"dens":1,"fric":0.4,"rest":0.3,"shape":9},{"ID":9,"type":1,"vertices":[10,11,12,13]},{"ID":10,"x":-1.25,"y":-0.5},{"ID":11,"x":1.25,"y":-0.5},{"ID":12,"x":1.25,"y":0.5},{"ID":13,"x":-1.25,"y":0.5},{"ID":14,"angle":5.819537691528989,"fxes":[16],"pos":15,"type":2},{"ID":15,"x":2.459674775249768,"y":-1.2298373876248851},{"ID":16,"dens":1,"fric":0.4,"rest":0.3,"shape":17},{"ID":17,"type":1,"vertices":[18,19,20,21]},{"ID":18,"x":-1.25,"y":-0.5},{"ID":19,"x":1.25,"y":-0.5},{"ID":20,"x":1.25,"y":0.5},{"ID":21,"x":-1.25,"y":0.5},{"ID":22,"angle":6.124751611729083,"fxes":[24],"pos":23,"type":2},{"ID":23,"x":4.92836381080626,"y":-1.624266653429484},{"ID":24,"dens":1,"fric":0.4,"rest":0.3,"shape":25},{"ID":25,"type":1,"vertices":[26,27,28,29]},{"ID":26,"x":-1.25,"y":-0.5},{"ID":27,"x":1.25,"y":-0.5},{"ID":28,"x":1.25,"y":0.5},{"ID":29,"x":-1.25,"y":0.5},{"ID":30,"angle":1.7284620311810566,"fxes":[32],"pos":31,"type":2},{"ID":31,"x":-1.8947696372115996,"y":4.772420363433075},{"ID":32,"dens":1,"fric":0.4,"rest":0.3,"shape":33},{"ID":33,"type":1,"vertices":[34,35,36,37]},{"ID":34,"x":-1.25,"y":-0.5},{"ID":35,"x":1.25,"y":-0.5},{"ID":36,"x":1.25,"y":0.5},{"ID":37,"x":-1.25,"y":0.5},{"ID":38,"anchor1":39,"anchor2":40,"body1":2,"body2":6,"collide":false,"limit":true,"lower":-0.7853981625,"type":1,"upper":0.7853981625},{"ID":39,"x":-0.7511181982520193,"y":1.1517145706530938},{"ID":40,"x":-0.7511181982520193,"y":1.1517145706530938},{"ID":41,"anchor1":42,"anchor2":43,"body1":2,"body2":14,"collide":false,"limit":true,"lower":-0.7853981625,"type":1,"upper":0.7853981625},{"ID":42,"x":1.229837387624884,"y":-0.6149186938124426},{"ID":43,"x":1.2298373876248838,"y":-0.6149186938124426},{"ID":44,"anchor1":45,"anchor2":46,"body1":14,"body2":22,"collide":false,"limit":true,"lower":-0.7853981625,"type":1,"upper":0.7853981625},{"ID":45,"x":3.6940192930280142,"y":-1.4270520205271846},{"ID":46,"x":3.6940192930280142,"y":-1.4270520205271846},{"ID":47,"anchor1":48,"anchor2":49,"body1":6,"body2":30,"collide":false,"limit":true,"lower":-0.7853981625,"type":1,"upper":0.7853981625},{"ID":48,"x":-1.6985030168578192,"y":3.5379247523696313},{"ID":49,"x":-1.6985030168578192,"y":3.5379247523696318},{"ID":50,"anchor1":51,"anchor2":52,"body1":30,"body2":22,"collide":false,"damping":1,"freq":1.5,"length":6,"type":3},{"ID":51,"x":-1.6666666666666687,"y":5.833333333333334},{"ID":52,"x":5.5333333333333306,"y":-1.5666666666666655}],[{"body":2,"branch":0,"parent":-1,"radius":1.5,"shape":0},{"body":6,"branch":1,"het":0.5,"parent":0,"shape":1,"wid":1.25},{"body":14,"branch":2,"het":0.5,"parent":0,"shape":1,"wid":1.25},{"body":22,"branch":3,"het":0.5,"parent":0,"shape":1,"wid":1.25},{"body":30,"branch":4,"het":0.5,"parent":0,"shape":1,"wid":1.25}],[{"damping0":1,"damping1":1,"hz0":1,"hz1":1,"joint":50,"len0":6,"len1":0}]]

And then back again. This is important because one of the major things I want people to be able to do during the kickstarter demo is save and share their created creatures via a link. At first, I thought I might be able to just push all that data through the URL bar (like ChipTone does), but I think I might have to instead learn how to set up a database that’ll store all that stuff. Unfortunately, most of what I know about databases comes from seeing Hollywood actors hit keyboards while pretending to hack them. Just another thing that I’ll have to learn, I guess.

Regarding the box2d->json converter, I was surprised that one didn’t already exist for Monkey X, the language I’m doing all this in. I’d like to release the module I made to do so once it’s a little more complete (e.g. right now it only supports the joint types I happen to be using). PROS: I’ve never released code for general use before, so that’s exciting.  CONS: people will know both my super messy code and my shame. We’ll see how it goes.

Leave a Reply

Your email address will not be published. Required fields are marked *

By Wick Perry | © 2017 Wickworks
Proud to be a member of PIGsquad and Playful Oasis.