Sunday, August 5, 2012

Collisions at a Distance

‹prev | My Chain | next›

After an aborted attempt to get the Box2D.js plugin working with Gladius, I hope to get it working tonight. I actually was able to get the plugin to not crash, but could not get any further.

I start by adding a Box2D body to serve as an obstacle for my avatar:
        var bodyDefinition = new box2d.BodyDefinition();
        var fixtureDefinition = new box2d.FixtureDefinition({
          shape:new box2d.BoxShape(0.25,0.25)
        });
        var obstacleBody = new box2d.Body({
          bodyDefinition: bodyDefinition,
          fixtureDefinition: fixtureDefinition
        });
The Box2D body has on-contact callbacks. I set it up so that, in addition to console.log() output, I change the color of the obstacle to grey when contact starts and back to yellow when contact ends:
        obstacleBody.onContactBegin = function(event){
          console.log("Obstable contact begin");
          this.owner.findComponent( "Model").setMaterialDefinition(resources.grey_material);
        };
        obstacleBody.onContactEnd = function(event){
          console.log("Obstable contact end");
          this.owner.findComponent( "Model").setMaterialDefinition(resources.yellow_material);
        };
Lastly, I add the Box2D body to a Gladius sphere entity and add both to space:
        var obstacle = new engine.Entity( "obstacle",
          [
            new engine.core.Transform([0.0, -0.45, 10]),
            obstacleBody,
            new cubicvr.Model( resources.sphere_mesh, resources.yellow_material )
          ]
        );
        space.add( obstacle );
Then I add another Box2D body to my avatar:
        var box2dBody = new box2d.Body({
          bodyDefinition: bodyDefinition,
          fixtureDefinition: fixtureDefinition
        });

        space.add(new engine.Entity("avatar",
          [
            new engine.core.Transform([0, 0, 0]),
            box2dBody,
            new cubicvr.Model(resources.cone_mesh, resources.red_material),
            new input.Controller( resources.avatar_controls ),
            new engine.logic.Actor( avatarLogic )
          ]
        ));
And when I am far away along the x-axis, it seems to work:


But when I am far away along the z-axis, it does not work:


My avatar is clearly far away (a distance of 10), and yet a collision is registered.

Even reduced to simple case with all input logic removed, all motion of the arms and legs removed, I still get a collision at a z-distance:


I can't figure this one out. No matter how far away the objects get along the z-axis, they always collide. It seems as if the collision detection on that axis is infinite. I am at a loss to explain this, unless it is similar to Three.js's collision detection with rays. I'll probably give this one more day before giving up completely, but I am frustrated.

Day #469

No comments:

Post a Comment