Thursday, September 6, 2012

Copying Mr Doob's Code Editor for Fun and Physics

‹prev | My Chain | next›

Up today, I would like to see if I can get Physijs simulations working in Mr Doob's code editor. Right up until the beta release date of Gaming JavaScript, I had meant to include a Physijs-based game. Unfortunately, I did not realize that web workers need to be served from the same machine as the original web page. Since I have kids developing locally, this presented a seemingly unassailable obstacle.

Until Mr Doob's Code Editor. At least, that is the hope.

The Physijs physics engine is loaded like any other script:
  <script src="">
  <script src="">
  <script src="">
But inside a Physijs script, one needs to specify the location of the underlying ammo.js physics engine as well as a web worker for doing physics calculations:
// Physics
Physijs.scripts.ammo = '';
Physijs.scripts.worker = '';
Since I am loading the web page from a http://localhost server, that web worker URL will not work. And indeed, if I load the page, I get the dreaded "SECURITY_ERR: DOM Exception 18":

In fact, since my web worker resides on a different server than Mr Doob's Code Editor, I still get this error if I try to paste my code into the editor.

I see no other option but to copy Mr Doob's Code Editor into so that both Code Editor and physics worker will be served from the same server.

So I clone the Code Editor:
➜  repos  git clone
➜  repos  cd code-editor
And export it into my Gaming JavaScript repository:
➜  code-editor git:(master) mkdir ../gamingjs/code-editor
➜  code-editor git:(master) git archive HEAD | tar -C ../gamingjs/code-editor -xf -
In my Gaming JavaScript repository, which is just a GitHub pages repository, I add the code editor and push it up to GitHub:
➜  gamingjs git:(gh-pages) ✗ git add code-editor/
➜  gamingjs git:(gh-pages) ✗ git ci -m "Add Mr Doob's Code Editor"
➜  gamingjs git:(gh-pages) gp origin gh-pages
With that, I have my very own copy of Mr Doob's excellent Code Editor at

More importantly, I am able to use the physijs web worker already on to make physics happen:

Best of all, since this is Mr Doob's Code Editor, I can save my code so that others can check it out.

Please excuse me now while I go rewrite Gaming JavaScript completely to use this. This is just fantastic.

Day #502

No comments:

Post a Comment