added collision detection

This commit is contained in:
logsol 2012-07-11 02:45:54 +02:00
parent ff6456fd47
commit a6058ce3d3
3 changed files with 55 additions and 16 deletions

View file

@ -0,0 +1,38 @@
define(["Box2D/Box2D"], function(Box2D) {
function Detector(me) {
this.me = me;
this.listener = new Box2D.Dynamics.b2ContactListener();
this.listener.chuckDetector = this;
this.listener.BeginContact = this.BeginContact;
this.listener.PostSolve = this.PostSolve;
this.listener.EndContact = this.EndContact;
}
Detector.prototype.getListener = function() {
return this.listener;
}
Detector.prototype.handleStand = function(point, isColliding) {
if (point.GetFixtureA().GetUserData() == 'myFeet' || point.GetFixtureB().GetUserData() == 'myFeet') {
this.me.onFootSensorDetection(isColliding);
}
}
/** Extension **/
Detector.prototype.BeginContact = function(point) {
this.chuckDetector.handleStand(point, true);
}
Detector.prototype.PostSolve = function(point, impulse) {
this.chuckDetector.handleStand(point, true);
}
Detector.prototype.EndContact = function(point) {
this.chuckDetector.handleStand(point, false);
}
return Detector;
});

View file

@ -1,12 +1,12 @@
define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){ define(["Chuck/Settings", "Box2D/Box2D", "Chuck/Collision/Detector"], function(Settings, Box2D, CollisionDetector){
function Engine () { function Engine () {
this._world; this.world;
this.init(); this.init();
} }
Engine.prototype.init = function() { Engine.prototype.init = function() {
this._world = new Box2D.Dynamics.b2World(new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY), Settings.BOX2D_ALLOW_SLEEP); this.world = new Box2D.Dynamics.b2World(new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY), Settings.BOX2D_ALLOW_SLEEP);
if(Settings.IS_BROWSER_ENVIRONMENT) { if(Settings.IS_BROWSER_ENVIRONMENT) {
this.setupDebugDraw(); this.setupDebugDraw();
@ -14,14 +14,13 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){
} }
Engine.prototype.getWorld = function() { Engine.prototype.getWorld = function() {
return this._world; return this.world;
} }
Engine.prototype.setCollisionDetector = function(me) { Engine.prototype.setCollisionDetector = function(me) {
/*
var cd = new Chuck.Collision.Detector(me); var detector = new CollisionDetector(me);
var listener = cd.getListener(); this.world.SetContactListener(detector.getListener());
this._world.SetContactListener(listener);*/
} }
Engine.prototype.setupDebugDraw = function() { Engine.prototype.setupDebugDraw = function() {
@ -46,18 +45,18 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){
//| Box2D.Dynamics.b2DebugDraw.e_pairBit //| Box2D.Dynamics.b2DebugDraw.e_pairBit
); );
this._world.SetDebugDraw(debugDraw); this.world.SetDebugDraw(debugDraw);
this._world.SetWarmStarting(true); this.world.SetWarmStarting(true);
} }
Engine.prototype.createBody = function(bodyDef) { Engine.prototype.createBody = function(bodyDef) {
return this._world.CreateBody(bodyDef); return this.world.CreateBody(bodyDef);
} }
Engine.prototype.update = function() { Engine.prototype.update = function() {
this._world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS); this.world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
this._world.ClearForces(); this.world.ClearForces();
this._world.DrawDebugData(); this.world.DrawDebugData();
} }
return Engine; return Engine;

View file

@ -28,7 +28,7 @@ define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box
Processor.prototype.init = function() { Processor.prototype.init = function() {
this._physicsEngine = new PhysicsEngine(); this._physicsEngine = new PhysicsEngine();
this._makeBox(); //this._makeBox();
if(Settings.IS_BROWSER_ENVIRONMENT) { if(Settings.IS_BROWSER_ENVIRONMENT) {
@ -39,9 +39,11 @@ define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box
//this._camera = Camera.getInstance() //this._camera = Camera.getInstance()
//this._camera.follow(this._me); //this._camera.follow(this._me);
//this._repository = Repository.getInstance(); //this._repository = Repository.getInstance();
//this._physicsEngine.setCollisionDetector(this._me); this._physicsEngine.setCollisionDetector(this._me);
} }
//new Chuck.Loader.Level(this._physicsEngine);u //new Chuck.Loader.Level(this._physicsEngine);u
//new Items(); //new Items();