From: Joe Wreschnig Date: Thu, 18 Sep 2014 15:04:00 +0000 (+0200) Subject: Derive rotor angles from bodies. X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;h=ca536dd9d6bf0f56875175560c586c79a10eeade;p=featherfall2.git Derive rotor angles from bodies. --- diff --git a/src/main.js b/src/main.js index ecff045..1846c3e 100644 --- a/src/main.js +++ b/src/main.js @@ -65,6 +65,7 @@ var BodyC = yT(yuu.C, { }, position: { alias: 'body.position' }, + angle: { alias: 'body.angle' }, linearVelocity: { alias: 'body.linearVelocity' }, ApplyForce: { proxy: 'body.ApplyForce' }, @@ -89,8 +90,6 @@ var PlayerController = yT(yuu.C, { this.drightLeft = this.drightRight = 0; this.up = 0; this.free = 0; - this.leftPivot = 0; - this.rightPivot = 0; this.commands = { dleftLeft: yuu.propcmd(this, 'dleftLeft'), dleftRight: yuu.propcmd(this, 'dleftRight'), @@ -102,43 +101,43 @@ var PlayerController = yT(yuu.C, { }, _updatePivots: function () { - var PIVOT_SPEED = 0.05; - var leftSpeed = (this.dleftRight - this.dleftLeft) * PIVOT_SPEED; - var rightSpeed = (this.drightLeft - this.drightRight) * PIVOT_SPEED; - this.leftPivot = yf.clamp(this.leftPivot + leftSpeed, 0, 1); - this.rightPivot = yf.clamp(this.rightPivot + rightSpeed, 0, 1); + var PIVOT_SPEED = 2; + this.leftJoint.motorSpeed = + (this.dleftRight - this.dleftLeft) * PIVOT_SPEED; + this.rightJoint.motorSpeed = + (this.drightRight - this.drightLeft) * PIVOT_SPEED; }, _updateTransforms: function () { - var gain = 1.0; - var leftTarget = this.leftPivot * Math.PI / 2; - var rightTarget = -this.rightPivot * Math.PI / 2; - var leftError = this.leftJoint.jointAngle - leftTarget; - var rightError = this.rightJoint.jointAngle - rightTarget; - this.leftJoint.motorSpeed = -gain * leftError; - this.rightJoint.motorSpeed = -gain * rightError; }, tick: function () { this._updatePivots(); - var THRUST = 3.5; - var DRAG_FREE = 0.01; + var THRUST = 5; +/* var DRAG_FREE = 0.01; var DRAG_OPEN = 0.5; var DRAG_LOCK = 1; - var CORRECTION = 1; + var CORRECTION = 1;*/ - var leftAngle = (1 - this.leftPivot) * Math.PI / 2; - var rightAngle = (1 - this.rightPivot) * Math.PI / 2; + var leftAngle = this.leftJoint.GetJointAngle(); + var rightAngle = this.rightJoint.GetJointAngle(); + var thrust = +!this.free * +this.up * THRUST; + var leftThrust = new b2Vec2( + Math.sin(leftAngle) * thrust, Math.cos(leftAngle) * thrust); + var rightThrust = new b2Vec2( + Math.sin(rightAngle) * thrust, Math.cos(rightAngle) * thrust); + this.body.body.ApplyForceToCenter(leftThrust); + this.body.body.ApplyForceToCenter(rightThrust); var cleft = Math.cos(leftAngle); var cright = Math.cos(rightAngle); var sleft = Math.sin(leftAngle); var sright = Math.sin(rightAngle); - var thrust = +!this.free * +this.up * THRUST; var ax = thrust * (cleft - cright); var ay = thrust * (sright + sleft); + /* var v = this.body.linearVelocity; var drag = this.up ? DRAG_OPEN : this.free ? DRAG_FREE : DRAG_LOCK; ax += drag * Math.max(cleft, cright) * v.x * v.x * -Math.sign(v.x); @@ -146,6 +145,7 @@ var PlayerController = yT(yuu.C, { if (!this.up || this.free) ax += CORRECTION * (cleft - cright) * v.y * v.y * Math.sign(v.y); + */ this.body.ApplyForce(new b2Vec2(ax, ay), this.body.position); @@ -155,23 +155,26 @@ var PlayerController = yT(yuu.C, { TAPS: ['tick'], }); -function bodyFromAABB (world, position, aabb, density) { - var bd = new Box2D.b2BodyDef(); +function bodyFromAABB (world, position, aabb, density, center) { + var dfn = new Box2D.b2BodyDef(); var shape = new Box2D.b2PolygonShape(); - shape.SetAsBox(aabb.hw, aabb.hh); - if (density) - bd.type = Box2D.DYNAMIC_BODY; - bd.position = new b2Vec2(position[0], position[1]); - var body = world.CreateBody(bd); - body.CreateFixture(shape, density || 0); + if (center) + shape.SetAsBox(aabb.hw, aabb.hh, new b2Vec2(center), 0); + else + shape.SetAsBox(aabb.hw, aabb.hh); + if (density !== undefined) + dfn.type = Box2D.DYNAMIC_BODY; + dfn.position = new b2Vec2(position[0], position[1]); + var body = world.CreateBody(dfn); + body.CreateFixture(shape, density || 0.0001); return body; } function bodyFromLine (world, p0, p1) { - var bd = new Box2D.b2BodyDef(); + var dfn = new Box2D.b2BodyDef(); var shape = new Box2D.b2EdgeShape(); shape.Set(new b2Vec2(p0), new b2Vec2(p1)); - var body = world.CreateBody(bd); + var body = world.CreateBody(dfn); body.CreateFixture(shape, 0); return body; } @@ -179,9 +182,10 @@ function bodyFromLine (world, p0, p1) { function pinJoint (world, bodyA, bodyB, anchor) { var dfn = new Box2D.b2RevoluteJointDef(); dfn.Initialize(bodyA, bodyB, new b2Vec2(anchor)); - dfn.maxMotorTorque = 10.0; - dfn.motorSpeed = 0.0; + dfn.maxMotorTorque = 100.0; + dfn.motorSpeed = 0.1; dfn.enableMotor = true; + dfn.enableLimit = true; return Box2D.castObject(world.CreateJoint(dfn), Box2D.b2RevoluteJoint); } @@ -203,20 +207,25 @@ var GameScene = yT(yuu.Scene, { .setSize([0.89, 1.0]) ); - var leftWing = new yuu.E(left = new BodyC( - bodyFromAABB(world, [-0.50, 5.15], new yuu.AABB(0.45, 0.22), 1.0)), + var leftWing = new yuu.E( + left = new BodyC(bodyFromAABB( + world, [-0.275, 5.15], new yuu.AABB(0.45, 0.22), 0, + [-0.45/2, 0.0])), new yuu.QuadC('@left') + .setAnchorAtPosition("right") .setZ(-1) .setSize([0.45, 0.22])); - var leftJoint = pinJoint(world, left.body, body.body, [-0.1, 5.15]); + var leftJoint = pinJoint(world, left.body, body.body, [0.1, 5.15]); var rightWing = new yuu.E(right = new BodyC( - bodyFromAABB(world, [0.50, 5.15], new yuu.AABB(0.45, 0.22), 1.0)), + bodyFromAABB(world, [0.50, 5.15], new yuu.AABB(0.45, 0.22), 0)), new yuu.QuadC('@right') .setZ(-1) .setSize([0.45, 0.22])); var rightJoint = pinJoint(world, right.body, body.body, [0.1, 5.15]); this.player.addChildren(leftWing, rightWing); this.entity0.addChild(this.player); + leftJoint.SetLimits(0, Math.PI / 2); + rightJoint.SetLimits(-Math.PI / 2, 0); var ground = new yuu.E( new BodyC(bodyFromLine(world, [-100, 0], [100, 0])), @@ -228,7 +237,7 @@ var GameScene = yT(yuu.Scene, { this.player.attach( this.controller = new PlayerController(body, left, right, - leftJoint, rightJoint)); + leftJoint, rightJoint)); Object.assign(this.commands, this.controller.commands); this.entity0.attach(new yuu.Ticker(function () {