From d18cb4c622fbb0172b9618d8594953a32ccb88b2 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 29 Jul 2014 17:46:40 -0400 Subject: fix wall clamping bug --- test/01-test-vec2.js | 57 +++++++++++++++++++++++++++++++++++---- test/05-test-mover.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 test/05-test-mover.js (limited to 'test') diff --git a/test/01-test-vec2.js b/test/01-test-vec2.js index b38f052..6104f92 100644 --- a/test/01-test-vec2.js +++ b/test/01-test-vec2.js @@ -2,8 +2,8 @@ var assert = require("assert") var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") describe('vec2', function(){ + var vec = new vec2(0, 10) describe('#intersects()', function(){ - var vec = new vec2(0, 10) it('intersects itself', function(){ assert.equal(true, vec.intersects( new vec2(0, 10) )); @@ -29,6 +29,15 @@ describe('vec2', function(){ it('does not intersect when after', function(){ assert.equal(false, vec.intersects( new vec2(15, 20) )); }) + it('intersects when only startpoint matches', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 0) )); + }) + it('intersects when only endpoint matches', function(){ + assert.equal(true, vec.intersects( new vec2(10, 15) )); + }) + }) + + describe('#contains()', function(){ it('contains itself', function(){ assert.equal(true, vec.contains( 0 )); assert.equal(true, vec.contains( 5 )); @@ -38,6 +47,9 @@ describe('vec2', function(){ assert.equal(false, vec.contains( -5 )); assert.equal(false, vec.contains( 15 )); }) + }) + + describe('#containsCenter()', function(){ it('containsCenter itself', function(){ assert.equal(true, vec.containsCenter( 5 )); }) @@ -45,11 +57,46 @@ describe('vec2', function(){ assert.equal(false, vec.containsCenter( 0 )); assert.equal(false, vec.containsCenter( 10 )); }) - it('intersects when only startpoint matches', function(){ - assert.equal(true, vec.intersects( new vec2(-5, 0) )); + }) + + describe('#containsDisc()', function(){ + it('containsDisc within a radius', function(){ + assert.equal(true, vec.containsDisc( 2, 2 )); + assert.equal(true, vec.containsDisc( 5, 2 )); + assert.equal(true, vec.containsDisc( 7, 2 )); }) - it('intersects when only endpoint matches', function(){ - assert.equal(true, vec.intersects( new vec2(10, 15) )); + it('does not containsDisc its endpoints', function(){ + assert.equal(false, vec.containsDisc( 0, 2 )); + assert.equal(false, vec.containsDisc( 1, 2 )); + assert.equal(false, vec.containsDisc( 9, 2 )); + assert.equal(false, vec.containsDisc( 10, 2 )); + }) + it('does not containsDisc outside points', function(){ + assert.equal(false, vec.containsDisc( -5, 2 )); + assert.equal(false, vec.containsDisc( -2, 2 )); + assert.equal(false, vec.containsDisc( 12, 2 )); + assert.equal(false, vec.containsDisc( 15, 2 )); + }) + }) + + describe('#clampDisc()', function(){ + it('clampDisc clamps on the left', function(){ + assert.equal(2, vec.clampDisc( -1, 2 )); + assert.equal(2, vec.clampDisc( 0, 2 )); + assert.equal(2, vec.clampDisc( 1, 2 )); + assert.equal(2, vec.clampDisc( 2, 2 )); + }) + it('clampDisc clamps on the right', function(){ + assert.equal(8, vec.clampDisc( 8, 2 )); + assert.equal(8, vec.clampDisc( 9, 2 )); + assert.equal(8, vec.clampDisc( 10, 2 )); + assert.equal(8, vec.clampDisc( 11, 2 )); + }) + it('clampDisc doesnt clamp in the middle', function(){ + assert.equal(3, vec.clampDisc( 3, 2 )); + assert.equal(4, vec.clampDisc( 4, 2 )); + assert.equal(5, vec.clampDisc( 5, 2 )); }) }) }) + diff --git a/test/05-test-mover.js b/test/05-test-mover.js new file mode 100644 index 0000000..42c8653 --- /dev/null +++ b/test/05-test-mover.js @@ -0,0 +1,74 @@ +var assert = require("assert") +var vec = require("../public/assets/javascripts/rectangles/models/vec2.js") +var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") +var Room = require("../public/assets/javascripts/rectangles/models/room.js") +var Rooms = require("../public/assets/javascripts/rectangles/engine/rooms/_rooms.js") +var Clipper = require("../public/assets/javascripts/rectangles/engine/rooms/clipper.js") +var Builder = require("../public/assets/javascripts/rectangles/engine/rooms/builder.js") +var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 +var ALL = FRONT | BACK | LEFT | RIGHT +var NONE = 0x0 +function sidesToString(sides){ + var s = "" + if (sides & FRONT) s += "front " + if (sides & BACK) s += "back " + if (sides & LEFT) s += "left " + if (sides & RIGHT) s += "right " + if (sides & TOP) s += "top " + if (sides & BOTTOM) s += "bottom " + return s +} + +var radius = 1 + +var rect = new Rect( new vec(1,5), new vec(1,5) ) +var east = new Rect( new vec(2,6), new vec(1,5) ) +var corner = new Rect( new vec(3,7), new vec(3,7) ) +var peninsula = new Rect( new vec(4,6), new vec(6,8) ) + +var rect_room = new Room({ id: "rect", rect: rect, height: 2 }) +var east_room = new Room({ id: "east", rect: east, height: 2 }) +var corner_room = new Room({ id: "corner", rect: corner, height: 2 }) +var peninsula_room = new Room({ id: "peninsula", rect: peninsula, height: 2 }) + +function reset(){ + Rooms.forEach(function(room){ + room.reset() + }) + Rooms.list = {} + Rooms.regions = [] +} +function rebuild(){ + Rooms.clipper.reset_rects() + var regions = Rooms.clipper.clip_rects() + var culled = Rooms.clipper.cull_rects_iterative() + return culled +} + +describe('mover', function(){ + reset() + Rooms.add( rect_room ) + rebuild() + + describe('room#collidesDisc()', function(){ + var room = rect_room + it("should not collide in the center", function(){ + assert.equal(NONE, room.collidesDisc(3, 3, radius)) + assert.equal(NONE, room.collidesDisc(2, 2, radius)) + assert.equal(NONE, room.collidesDisc(4, 4, radius)) + }) + it("should collide on the sides", function(){ + assert.equal(FRONT, room.collidesDisc(3, 1, radius)) + assert.equal(BACK, room.collidesDisc(3, 5, radius)) + assert.equal(LEFT, room.collidesDisc(1, 3, radius)) + assert.equal(RIGHT, room.collidesDisc(5, 3, radius)) + }) + it("should collide in the corners", function(){ + assert.equal(LEFT | FRONT, room.collidesDisc(1, 1, radius)) + assert.equal(RIGHT | FRONT, room.collidesDisc(5, 1, radius)) + assert.equal(LEFT | BACK, room.collidesDisc(1, 5, radius)) + assert.equal(RIGHT | BACK, room.collidesDisc(5, 5, radius)) + }) + }) +}) + -- cgit v1.2.3-70-g09d2