Sunday, September 9, 2012

Kid Physics in the Code Editor

‹prev | My Chain | next›

Up tonight, I would like to make the physics in my Three.js / Physijs jumping game a bit more "kid like". Specifically, I would like the player to fall faster. I would like changes in direction to be more immediate.

I am going to eat my own dog food here. I plan to use the excellent code editor from Mr Doob for Gaming JavaScript. To do it right, I need to use it myself. My starting point, in the code editor, is current version of the game.

After hiding the code (and clicking on the document for proper event captures), the arrow keys move the player—the rectangle—around:


Falling faster is a simple matter of increasing gravity:
  scene = new Physijs.Scene({ fixedTimeStep: 2 / 60 });
  scene.setGravity(new THREE.Vector3( 0, -50, 0 ));
I also increase the Physijs timestep for an added boost.

To move the players differently, I would like to experiment in the Chrome JavaScript console. Mostly, I just want the code completion to remind me what methods Physijs exposes, but I also appreciate the immediate feedback that the console can provide—even more so than the code editor.

The problem is that the player, along with all other top level game variables, is not available in the JavaScript console:


Fortunately, it is available in a frame:


With that sorted out, I end up with this implementation for the game movement:
function left()  { move(-50, 0); }
function right() { move(50, 0); }
function up()    { move(player.getLinearVelocity().x, 50); }

function move(x, y) {
  player.setLinearVelocity(
    new THREE.Vector3(x, y, 0)
  );
}
Which seems to work well enough. And, since I am using the code editor, I can share it for others to try out.


Day #504

1 comment:

  1. Heya Sebastian from Germany here.

    Greate Concept of letting our younger ones have a crack at 3d Gaming. I guess you are expecting JS-Knowledge in adavance from your readers of the book?

    Keep up the work with box2d and threejs. There isnt alot out there on the topic so your blog is filling in nicely.

    Thought of an example like monkey ball for collisions? A ball which collides into walls, rebounds, then takes up speed again. With an over-the-shoulder camera following. Could be a little too complex but it woul
    be ultimate fun...

    Cheers!

    ReplyDelete