mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
added collision detection
This commit is contained in:
parent
ff6456fd47
commit
a6058ce3d3
3 changed files with 55 additions and 16 deletions
38
lib/Chuck/Collision/Detector.js
Normal file
38
lib/Chuck/Collision/Detector.js
Normal 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;
|
||||
});
|
||||
|
|
@ -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 () {
|
||||
this._world;
|
||||
this.world;
|
||||
this.init();
|
||||
}
|
||||
|
||||
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) {
|
||||
this.setupDebugDraw();
|
||||
|
|
@ -14,14 +14,13 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){
|
|||
}
|
||||
|
||||
Engine.prototype.getWorld = function() {
|
||||
return this._world;
|
||||
return this.world;
|
||||
}
|
||||
|
||||
Engine.prototype.setCollisionDetector = function(me) {
|
||||
/*
|
||||
var cd = new Chuck.Collision.Detector(me);
|
||||
var listener = cd.getListener();
|
||||
this._world.SetContactListener(listener);*/
|
||||
|
||||
var detector = new CollisionDetector(me);
|
||||
this.world.SetContactListener(detector.getListener());
|
||||
}
|
||||
|
||||
Engine.prototype.setupDebugDraw = function() {
|
||||
|
|
@ -46,18 +45,18 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){
|
|||
//| Box2D.Dynamics.b2DebugDraw.e_pairBit
|
||||
);
|
||||
|
||||
this._world.SetDebugDraw(debugDraw);
|
||||
this._world.SetWarmStarting(true);
|
||||
this.world.SetDebugDraw(debugDraw);
|
||||
this.world.SetWarmStarting(true);
|
||||
}
|
||||
|
||||
Engine.prototype.createBody = function(bodyDef) {
|
||||
return this._world.CreateBody(bodyDef);
|
||||
return this.world.CreateBody(bodyDef);
|
||||
}
|
||||
|
||||
Engine.prototype.update = function() {
|
||||
this._world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
|
||||
this._world.ClearForces();
|
||||
this._world.DrawDebugData();
|
||||
this.world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
|
||||
this.world.ClearForces();
|
||||
this.world.DrawDebugData();
|
||||
}
|
||||
|
||||
return Engine;
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box
|
|||
Processor.prototype.init = function() {
|
||||
|
||||
this._physicsEngine = new PhysicsEngine();
|
||||
this._makeBox();
|
||||
//this._makeBox();
|
||||
|
||||
if(Settings.IS_BROWSER_ENVIRONMENT) {
|
||||
|
||||
|
|
@ -39,9 +39,11 @@ define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box
|
|||
//this._camera = Camera.getInstance()
|
||||
//this._camera.follow(this._me);
|
||||
//this._repository = Repository.getInstance();
|
||||
//this._physicsEngine.setCollisionDetector(this._me);
|
||||
this._physicsEngine.setCollisionDetector(this._me);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//new Chuck.Loader.Level(this._physicsEngine);u
|
||||
//new Items();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue