RubbeBall
by Tatsuya Suzuki
HTML
<script src="https://googledrive.com/host/0B-jdnZOJbr7XUkgybU1zYUs0dG8/Box2dWeb-2.1.a.3.js"></script>
<script src="https://googledrive.com/host/0B-jdnZOJbr7XUkgybU1zYUs0dG8/b2LoadAllClasses.js"></script>
<script src="https://googledrive.com/host/0B-jdnZOJbr7XUkgybU1zYUs0dG8/debugDraw.js"></script>
<script src="https://googledrive.com/host/0B-jdnZOJbr7XUkgybU1zYUs0dG8/worldWalls.js"></script>
<canvas id='canvas' width='450px' height='450px' style='background-color: beige;'></canvas>
JavaScript
var world = new b2World(new b2Vec2(0, 100), true);
var fixDef = new b2FixtureDef;
var bodyDef = new b2BodyDef;
var canvas = document.getElementById('canvas');
function RubberBallShape(radius) {
b2CircleShape.call(this, radius);
this.RADIUS = radius;
this.DIFF_THRESHOLD_TO_ROUND = this.RADIUS * 0.02;
this.IMPULSE_THRESHOLD_TO_STOP_UPDATING = 20;
}
RubberBallShape.prototype = Object.create(b2CircleShape.prototype);
RubberBallShape.prototype.Copy = function () {
var s = new RubberBallShape(this.RADIUS);
s.Set(this);
return s;
}
RubberBallShape.ballFactory = function (position) {
var fixDef = new b2FixtureDef;
var bodyDef = new b2BodyDef;
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position = position;
fixDef.shape = new RubberBallShape(5.0);
fixDef.restitution = 0.9;
var ball = world.CreateBody(bodyDef);
ball.CreateFixture(fixDef);
return ball;
}
RubberBallShape.prototype.shrink = function (impulse) {
this.imp = impulse.normalImpulses[0];
this.transformingSpeed = 0.1;
if (console) console.debug(this.imp);
this.SetRadius(this.RADIUS - this.imp / 100);
this.expanding = true;
}
RubberBallShape.prototype.diffExpanding = function () {
return this.RADIUS - this.GetRadius()
}
RubberBallShape.prototype.diffShrinking = function () {
return this.GetRadius() - this.shrinkedRadius()
}
RubberBallShape.prototype.shrinkedRadius = function () {
return this.RADIUS - this.imp / 100
}
RubberBallShape.prototype.diminishImpulse = function () {
this.imp -= 3
}
RubberBallShape.prototype.updateRadius = function () {
if (this.imp == undefined || this.imp <= this.IMPULSE_THRESHOLD_TO_STOP_UPDATING) return;
if (this.expanding) {
if (this.diffExpanding() < this.DIFF_THRESHOLD_TO_ROUND) {
var radius = this.RADIUS;
this.transformingSpeed += 0.2;
this.expanding = false;
} else {
var radius = this.GetRadius() +...