diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-07-29 17:46:40 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-07-29 17:46:40 -0400 |
| commit | d18cb4c622fbb0172b9618d8594953a32ccb88b2 (patch) | |
| tree | 4ea6ef72c4682aff081d4ce2ead763e437efd2bd | |
| parent | 4388c9ce2ae680862adb8abaf9d5b34088591a81 (diff) | |
fix wall clamping bug
| -rw-r--r-- | public/assets/javascripts/rectangles/engine/rooms/mover.js | 11 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/models/room.js | 10 | ||||
| -rw-r--r-- | public/assets/javascripts/rectangles/models/vec2.js | 2 | ||||
| -rw-r--r-- | test/01-test-vec2.js | 57 | ||||
| -rw-r--r-- | test/05-test-mover.js | 74 |
5 files changed, 136 insertions, 18 deletions
diff --git a/public/assets/javascripts/rectangles/engine/rooms/mover.js b/public/assets/javascripts/rectangles/engine/rooms/mover.js index e67d9bc..7195fcc 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/mover.js +++ b/public/assets/javascripts/rectangles/engine/rooms/mover.js @@ -43,15 +43,8 @@ Rooms.mover = new function(){ var collision = base.room.collidesDisc(pos.x, pos.z, radius) if (collision) { - if (! (collision & LEFT_RIGHT)) { - cam.x = base.room.rect.x.clampDisc(pos.x, radius) - } - else { - // cam.x = base.room.rect.x.clampDisc(pos.x, radius) - } - if (! (collision & FRONT_BACK)) { - cam.z = base.room.rect.y.clampDisc(pos.z, radius) - } + cam.x = (collision & LEFT_RIGHT) ? base.room.rect.x.clampDisc(pos.x, radius) : pos.x + cam.z = (collision & FRONT_BACK) ? base.room.rect.y.clampDisc(pos.z, radius) : pos.z return } diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index e5f42fe..32549e9 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -14,6 +14,8 @@ sort = require('../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 TOP = CEILING, BOTTOM = FLOOR + FRONT_BACK = FRONT | BACK + LEFT_RIGHT = LEFT | RIGHT function sidesToString(sides){ var s = "" if (sides & FRONT) s += "front " @@ -238,12 +240,12 @@ if (contains_x) { collision |= wall_collision & FRONT_BACK } - else if (contains_y) { + if (contains_y) { collision |= wall_collision & LEFT_RIGHT } - else if (bitcount(wall_collision) > 1) { - collision |= wall_collision - } +// if (bitcount(wall_collision) > 1) { +// collision |= wall_collision +// } }) return collision } diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index ee02088..2bf286b 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -1,4 +1,6 @@ (function(){ + function clamp(n,a,b){ return n<a?a:n<b?n:b } + var vec2 = function (a,b){ this.a = a this.b = b 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)) + }) + }) +}) + |
