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:
<head>
  <script src="http://gamingJS.com/Three.js">
  </script>
  <script src="http://gamingJS.com/Detector.js">
  </script>
  <script src="http://gamingJS.com/physi.js">
  </script>
  <title>Jumping</title>
</head>
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 = 'http://gamingJS.com/ammo.js';
Physijs.scripts.worker = 'http://gamingJS.com/physijs_worker.js';
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 http://gamingJS.com so that both Code Editor and physics worker will be served from the same server.

So I clone the Code Editor:
➜  repos  git clone https://github.com/mrdoob/code-editor.git
➜  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 http://gamingjs.com/code-editor/.

More importantly, I am able to use the physijs web worker already on gamingJS.com 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