Saturday, August 4, 2012

Getting (Slow) Started with Gladius-Box2D

‹prev | My Chain | next›

Up tonight, I hope to get started with collision detection in Gladius. Gladius includes an example of collision detection that uses Box2D.js for collision detection, so that seem a good place to start.

I add the gladius-box2d extension into my application with the usual require.js import:
  require(
    [ "gladius-core", "gladius-cubicvr", "gladius-input", "gladius-box2d" ],
    function( Gladius, cubicvrExtension, inputExtension, box2dExtension ) {
      var engine = new Gladius();
      // ...
      // collision detection
      engine.registerExtension(box2dExtension);
      // ...
    });
When I load my Gladius avatar simulation, however, I am greeted with:
Uncaught Error: already completed      gladius-box2d.js:701
I really have not missed those errors in Gladius. Already completed means little more than something went wrong somewhere and good luck tracking it down.

I am a little out of date with Gladius, but upgrading such a dynamic / unstable project brings risks. I will leave that as a last option.

Instead, I recall that Gladius will do that sometimes until the extension is used. So I plow ahead blindly adding box2d calls. First to the game() definition:
      function game(engine, resources) {
        var cubicvr = engine.findExtension("gladius-cubicvr");
        var box2d = engine.findExtension( "gladius-box2d" );
        var input = engine.findExtension( "gladius-input" );
        var math = engine.math;
        var space = new engine.SimulationSpace();
        // ...

        var bodyDefinition = new box2d.BodyDefinition();
        var fixtureDefinition = new box2d.FixtureDefinition({
          shape:new box2d.BoxShape(0.25,0.25)
        });
        var box2dBody = new box2d.Body({
          bodyDefinition: bodyDefinition, 
          fixtureDefinition: fixtureDefinition
        });
      }
This has no effect.

So I add the box2dBody definition to the body of my avatar:
      function game(engine, resources) {
        var cubicvr = engine.findExtension("gladius-cubicvr");
        var box2d = engine.findExtension( "gladius-box2d" );
        var input = engine.findExtension( "gladius-input" );
        var math = engine.math;
        var space = new engine.SimulationSpace();
        // ...

        var bodyDefinition = new box2d.BodyDefinition();
        var fixtureDefinition = new box2d.FixtureDefinition({
          shape:new box2d.BoxShape(0.25,0.25)
        });
        var box2dBody = new box2d.Body({
          bodyDefinition: bodyDefinition, 
          fixtureDefinition: fixtureDefinition
        });
        space.add(new engine.Entity("body",
          [
            new engine.core.Transform([0, 0, 0], [0, 0, Math.PI]),
            box2dBody,
            new cubicvr.Model(resources.cone_mesh, resources.red_material)
          ],
          ["avatar"]
        ));
        // ...
      }
Amazingly, that gets rid of the error.

I spend the rest of my time tonight attaching input controls to my avatar to move it and establishing an obstacle. Hopefully tomorrow I can interact with an actual collision.


Day #468

No comments:

Post a Comment