mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
refining js version
This commit is contained in:
parent
3c8fa410ba
commit
8d0ff1a929
15 changed files with 562 additions and 355 deletions
104
lib/Chuck/Physics/Doll.js
Executable file
104
lib/Chuck/Physics/Doll.js
Executable file
|
|
@ -0,0 +1,104 @@
|
|||
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());
|
||||
}
|
||||
|
||||
63
lib/Chuck/Physics/Engine.js
Executable file
63
lib/Chuck/Physics/Engine.js
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
Chuck.Physics.Engine = function () {
|
||||
this._world;
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.init = function() {
|
||||
this._world = this._initBox2d();
|
||||
this.setupDebugDraw();
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype._initBox2d = function() {
|
||||
return new Chuck.b2World(new Chuck.b2Vec2(0,Chuck.Settings.BOX2D_GRAVITY), Chuck.Settings.BOX2D_ALLOW_SLEEP);
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.getWorld = function() {
|
||||
return this._world;
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.setCollisionDetector = function() {
|
||||
var cd = new CollisionDetector();
|
||||
this._world.SetContactListener(cd);
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.setupDebugDraw = function() {
|
||||
//var debugSprite = new Sprite();
|
||||
//View.getInstance().add(debugSprite);
|
||||
|
||||
var debugSprite = document.getElementById("canvas").getContext("2d");
|
||||
|
||||
// set debug draw
|
||||
var dbgDraw = new Chuck.b2DebugDraw();
|
||||
|
||||
dbgDraw.SetSprite(debugSprite);
|
||||
dbgDraw.SetDrawScale(Chuck.Settings.RATIO);
|
||||
dbgDraw.SetAlpha(0.5);
|
||||
dbgDraw.SetFillAlpha(0.1);
|
||||
dbgDraw.SetLineThickness(0);
|
||||
|
||||
dbgDraw.SetFlags(null
|
||||
| Chuck.b2DebugDraw.e_shapeBit
|
||||
//| b2DebugDraw.e_jointBit
|
||||
//| b2DebugDraw.e_coreShapeBit
|
||||
//| b2DebugDraw.e_aabbBit
|
||||
//| b2DebugDraw.e_centerOfMassBit
|
||||
//| b2DebugDraw.e_obbBit
|
||||
//| b2DebugDraw.e_pairBit
|
||||
);
|
||||
|
||||
this._world.SetDebugDraw(dbgDraw);
|
||||
|
||||
this._world.SetWarmStarting(true);
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.createBody = function(bodyDef) {
|
||||
return this._world.CreateBody(bodyDef);
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.update = function() {
|
||||
this._world.Step(Chuck.Settings.BOX2D_TIME_STEP, Chuck.Settings.BOX2D_VELOCITY_ITERATIONS, Chuck.Settings.BOX2D_POSITION_ITERATIONS);
|
||||
this._world.ClearForces();
|
||||
this._world.DrawDebugData();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue