repaired debug draw, added more classes from game draft

This commit is contained in:
logsol 2012-07-01 15:16:33 +02:00
parent 1d24d98297
commit 5a6581393e
6 changed files with 354 additions and 71 deletions

105
lib/Chuck/Physics/Doll.js Normal file
View file

@ -0,0 +1,105 @@
define(['Doll'], function(){
Chuck.Physics.Doll = function(engine){
this._engine = engine;
this._body;
this._legs;
this._contactPoint;
this.init(this._engine.getWorld());
}
Chuck.Physics.Doll.prototype.init = function (world) {
var bodyDef = new Chuck.b2BodyDef();
bodyDef.position.x = 220 / Chuck.Settings.RATIO;
bodyDef.position.y = 0 / Chuck.Settings.RATIO;
bodyDef.fixedRotation = true;
bodyDef.linearDamping = Chuck.Settings.PLAYER_LINEAR_DAMPING;
bodyDef.type = Chuck.b2Body.b2_dynamicBody;
this._body = world.CreateBody(bodyDef);
var fixtureDef = new Chuck.b2FixtureDef();
fixtureDef.density = Chuck.Settings.PLAYER_DENSITY;
fixtureDef.friction = 0;
fixtureDef.restitution = Chuck.Settings.PLAYER_RESTITUTION;
var headShape = new Chuck.b2CircleShape();
headShape.SetRadius(5 / Chuck.Settings.RATIO);
headShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -37 / Chuck.Settings.RATIO));
fixtureDef.shape = headShape;
fixtureDef.isSensor = false;
fixtureDef.userData = 'myHead';
this._body.CreateFixture(fixtureDef);
var bodyShape = new Chuck.b2PolygonShape();
bodyShape.SetAsOrientedBox(5 / Chuck.Settings.RATIO, 16 / Chuck.Settings.RATIO, new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -21 / Chuck.Settings.RATIO));
fixtureDef.shape = bodyShape;
fixtureDef.isSensor = false;
fixtureDef.userData = 'myBody';
this._body.CreateFixture(fixtureDef);
var legsShape = new Chuck.b2CircleShape();
legsShape.SetRadius(5 / Chuck.Settings.RATIO);
legsShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -5 / Chuck.Settings.RATIO));
fixtureDef.shape = legsShape;
fixtureDef.friction = Chuck.Settings.PLAYER_FRICTION;
fixtureDef.isSensor = false;
fixtureDef.userData = 'myLegs';
this._legs = this._body.CreateFixture(fixtureDef);
var feetShape = new Chuck.b2CircleShape();
feetShape.SetRadius(4 / Chuck.Settings.RATIO);
feetShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, 0 / Chuck.Settings.RATIO));
fixtureDef.shape = feetShape;
fixtureDef.isSensor = true;
fixtureDef.userData = 'myFeet';
this._body.CreateFixture(fixtureDef);
this._body.SetActive(false);
}
Chuck.Physics.Doll.prototype.spawn = function (x, y) {
this._body.SetPosition(new Chuck.b2Vec2(x / Chuck.Settings.RATIO, y / Chuck.Settings.RATIO));
this._body.SetActive(true);
}
Chuck.Physics.Doll.prototype.getBody = function () {
return this._body;
}
Chuck.Physics.Doll.prototype._setFriction = function (friction) {
if(!friction) friction = -1;
if (this._legs.GetFriction() != friction)
{
this._legs.SetFriction(friction);
}
}
Chuck.Physics.Doll.prototype.move = function (direction, speed) {
this._setFriction(Chuck.Settings.PLAYER_MOTION_FRICTION);
this._body.SetAwake(true);
var vector = new Chuck.b2Vec2(speed * direction, this._body.GetLinearVelocity().y);
this._body.SetLinearVelocity(vector);
}
Chuck.Physics.Doll.prototype.stop = function () {
this._setFriction(Chuck.Settings.PLAYER_FRICTION);
}
Chuck.Physics.Doll.prototype.jump = function () {
this._body.SetAwake(true);
var vector = new Chuck.b2Vec2(0, -Chuck.Settings.JUMP_SPEED);
this._body.ApplyImpulse(vector, this._body.GetPosition());
// maybe change to a constant force instead of applying of force?
// to prevent higher jumping running uphill, etc.
}
Chuck.Physics.Doll.prototype.jumping = function () {
var vector = new Chuck.b2Vec2(0, -0.1);
this._body.ApplyImpulse(vector, this._body.GetPosition());
}
}

View file

@ -6,7 +6,7 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){
}
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();
@ -25,32 +25,30 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){
}
Engine.prototype.setupDebugDraw = function() {
var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE;
console.log(debugSprite);
//var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE;
var debugSprite = document.getElementById("canvas").getContext("2d");
// set debug draw
var dbgDraw = new Box2D.Dynamics.b2DebugDraw();
var debugDraw = new Box2D.Dynamics.b2DebugDraw();
dbgDraw.SetSprite(debugSprite);
dbgDraw.SetDrawScale(Settings.RATIO);
dbgDraw.SetAlpha(0.5);
dbgDraw.SetFillAlpha(0.1);
dbgDraw.SetLineThickness(0);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(Settings.RATIO);
debugDraw.SetDrawScale(30.0);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetLineThickness(1.0);
dbgDraw.SetFlags(null
| dbgDraw.e_shapeBit
//| b2DebugDraw.e_jointBit
//| b2DebugDraw.e_coreShapeBit
//| b2DebugDraw.e_aabbBit
//| b2DebugDraw.e_centerOfMassBit
//| b2DebugDraw.e_obbBit
//| b2DebugDraw.e_pairBit
);
this._world.SetDebugDraw(dbgDraw);
debugDraw.SetFlags(null
| Box2D.Dynamics.b2DebugDraw.e_shapeBit
| Box2D.Dynamics.b2DebugDraw.e_jointBit
//| Box2D.Dynamics.b2DebugDraw.e_coreShapeBit
//| Box2D.Dynamics.b2DebugDraw.e_aabbBit
//| Box2D.Dynamics.b2DebugDraw.e_centerOfMassBit
//| Box2D.Dynamics.b2DebugDraw.e_obbBit
//| Box2D.Dynamics.b2DebugDraw.e_pairBit
);
this._world.SetDebugDraw(debugDraw);
this._world.SetWarmStarting(true);
console.log('Debug Draw was set up');
}
Engine.prototype.createBody = function(bodyDef) {
@ -58,7 +56,7 @@ define(["Chuck/Settings", "Box2D/Box2D"], function(Settings, Box2D){
}
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.DrawDebugData();
}