Skip to content

Commit

Permalink
Merge pull request #120 from islemaster/flip-friction
Browse files Browse the repository at this point in the history
Flip friction value
  • Loading branch information
islemaster authored May 4, 2017
2 parents e9e7fca + e123fb5 commit 5eb1cee
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/p5.play.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,14 +827,15 @@ function Sprite(pInst, _x, _y, _w, _h) {

/**
* Friction factor, reduces the sprite's velocity.
* The friction should be close to 1 (eg. 0.99)
* 1: no friction
* The friction should be close to 0 (eg. 0.01)
* 0: no friction
* 1: full friction
*
* @property friction
* @type {Number}
* @default 1
* @default 0
*/
this.friction = 1;
this.friction = 0;

/**
* The sprite's current collider.
Expand Down Expand Up @@ -1251,8 +1252,8 @@ function Sprite(pInst, _x, _y, _w, _h) {
else
this.previousPosition = createVector(this.position.x, this.position.y);

this.velocity.x *= this.friction;
this.velocity.y *= this.friction;
this.velocity.x *= 1 - this.friction;
this.velocity.y *= 1 - this.friction;

if(this.maxSpeed !== -1)
this.limitSpeed(this.maxSpeed);
Expand Down
99 changes: 99 additions & 0 deletions test/unit/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,103 @@ describe('Sprite', function() {
}).to.throw(TypeError, 'Usage: setCollider("rectangle") or setCollider("rectangle", offsetX, offsetY, width, height)');
});
});

describe('friction', function() {
var sprite;

beforeEach(function() {
sprite = pInst.createSprite();
});

it('has no effect on update() when set to 0', function() {
sprite.velocity.x = 1;
sprite.velocity.y = 1;
sprite.friction = 0;
sprite.update();
expect(sprite.velocity.x).to.equal(1);
expect(sprite.velocity.y).to.equal(1);
});

it('reduces velocity to zero on update() when set to 1', function() {
sprite.velocity.x = 1;
sprite.velocity.y = 1;
sprite.friction = 1;
sprite.update();
expect(sprite.velocity.x).to.equal(0);
expect(sprite.velocity.y).to.equal(0);
});

describe('axis-aligned', function() {
beforeEach(function() {
sprite.velocity.x = 16;
});

it('cuts velocity in half each update when set to 0.5', function() {
sprite.friction = 0.5;
expect(sprite.velocity.x).to.equal(16);
sprite.update();
expect(sprite.velocity.x).to.equal(8);
sprite.update();
expect(sprite.velocity.x).to.equal(4);
sprite.update();
expect(sprite.velocity.x).to.equal(2);
});

it('cuts velocity to one-quarter each update when set to 0.75', function() {
sprite.friction = 0.75;
expect(sprite.velocity.x).to.equal(16);
sprite.update();
expect(sprite.velocity.x).to.equal(4);
sprite.update();
expect(sprite.velocity.x).to.equal(1);
sprite.update();
expect(sprite.velocity.x).to.equal(0.25);
});
});

describe('not axis-aligned', function() {
beforeEach(function() {
sprite.velocity.x = 3 * 16;
sprite.velocity.y = 4 * 16;
});

it('cuts velocity in half each update when set to 0.5', function() {
sprite.friction = 0.5;
expect(sprite.velocity.x).to.equal(3 * 16);
expect(sprite.velocity.y).to.equal(4 * 16);
expect(sprite.velocity.mag()).to.equal(5 * 16);
sprite.update();
expect(sprite.velocity.x).to.equal(3 * 8);
expect(sprite.velocity.y).to.equal(4 * 8);
expect(sprite.velocity.mag()).to.equal(5 * 8);
sprite.update();
expect(sprite.velocity.x).to.equal(3 * 4);
expect(sprite.velocity.y).to.equal(4 * 4);
expect(sprite.velocity.mag()).to.equal(5 * 4);
sprite.update();
expect(sprite.velocity.x).to.equal(3 * 2);
expect(sprite.velocity.y).to.equal(4 * 2);
expect(sprite.velocity.mag()).to.equal(5 * 2);
});

it('cuts velocity to one-quarter each update when set to 0.75', function() {
sprite.friction = 0.75;
expect(sprite.velocity.x).to.equal(3 * 16);
expect(sprite.velocity.y).to.equal(4 * 16);
expect(sprite.velocity.mag()).to.equal(5 * 16);
sprite.update();
expect(sprite.velocity.x).to.equal(3 * 4);
expect(sprite.velocity.y).to.equal(4 * 4);
expect(sprite.velocity.mag()).to.equal(5 * 4);
sprite.update();
expect(sprite.velocity.x).to.equal(3 * 1);
expect(sprite.velocity.y).to.equal(4 * 1);
expect(sprite.velocity.mag()).to.equal(5 * 1);
sprite.update();
expect(sprite.velocity.x).to.equal(3 * 0.25);
expect(sprite.velocity.y).to.equal(4 * 0.25);
expect(sprite.velocity.mag()).to.equal(5 * 0.25);
});
});
});
});

0 comments on commit 5eb1cee

Please sign in to comment.