From 52d18ddb211a7f4ee814ef23ff09656134810519 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Wed, 23 Jul 2014 01:38:17 -0400 Subject: making things intersect more greedily --- test/01-test-vec2.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/01-test-vec2.js (limited to 'test/01-test-vec2.js') diff --git a/test/01-test-vec2.js b/test/01-test-vec2.js new file mode 100644 index 0000000..054d37b --- /dev/null +++ b/test/01-test-vec2.js @@ -0,0 +1,48 @@ +var assert = require("assert") +var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") + +describe('vec2', function(){ + describe('#intersects()', function(){ + var vec = new vec2(0, 10) + + it('intersects itself', function(){ + assert.equal(true, vec.intersects( new vec2(0, 10) )); + }) + it('intersects w/ same startpoint (shorter)', function(){ + assert.equal(true, vec.intersects( new vec2(0, 5) )); + }) + it('intersects w/ same startpoint (longer)', function(){ + assert.equal(true, vec.intersects( new vec2(0, 15) )); + }) + it('intersects w/ same endpoint (shorter)', function(){ + assert.equal(true, vec.intersects( new vec2(5, 10) )); + }) + it('intersects w/ same endpoint (longer)', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 10) )); + }) + it('intersects when contained', function(){ + assert.equal(true, vec.intersects( new vec2(-5, 15) )); + }) + it('does not intersect when before', function(){ + assert.equal(false, vec.intersects( new vec2(-10, -5) )); + }) + it('does not intersect when after', function(){ + assert.equal(false, vec.intersects( new vec2(15, 20) )); + }) + it('contains itself', function(){ + assert.equal(true, vec.contains( 0 )); + assert.equal(true, vec.contains( 5 )); + assert.equal(true, vec.contains( 10 )); + }) + it('does not contain before or after', function(){ + assert.equal(false, vec.contains( -5 )); + assert.equal(false, vec.contains( 15 )); + }) + 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) )); + }) + }) +}) -- cgit v1.2.3-70-g09d2 From c3d855b3f0b6af000c0d359da6a2b774bcd0a5d5 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Wed, 23 Jul 2014 15:15:21 -0400 Subject: handling coplanar walls correctly --- .../assets/javascripts/rectangles/models/rect.js | 43 ++++++++++---- .../assets/javascripts/rectangles/models/vec2.js | 3 + test/01-test-vec2.js | 7 +++ test/02-test-rect.js | 65 +++++++++++++++++----- 4 files changed, 93 insertions(+), 25 deletions(-) (limited to 'test/01-test-vec2.js') diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index f724ecc..c14d62c 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -143,23 +143,46 @@ var x_intervals = this.x.split( r.x, LEFT, RIGHT ) var y_intervals = this.y.split( r.y, FRONT, BACK ) - x_intervals.forEach(function(x){ - y_intervals.forEach(function(y){ + x_intervals.forEach(function(x, i){ + y_intervals.forEach(function(y, i){ var rn = new Rect(x[0], y[0]) rn.id = rz.id rn.sides = ((x[1] | y[1]) & sides) - if (r.x.contains(rn.x.a)) { - rn.sides &= ~ LEFT + if (r.x.contains(rn.x.a) && r.x.contains(rn.x.b)) { + if (rz.y.a == rn.y.a && r.y.containsCenter(rn.y.a)) { // top edges + rn.sides &= ~ FRONT + } + if (rz.y.b == rn.y.b && r.y.containsCenter(rn.y.b)) { // bottom edges + rn.sides &= ~ BACK + } } - if (r.x.contains(rn.x.b)) { - rn.sides &= ~ RIGHT + + if (r.y.contains(rn.y.a) && r.y.contains(rn.y.b)) { + if (rz.x.a == rn.x.a && r.x.containsCenter(rn.x.a)) { // left edges + rn.sides &= ~ LEFT + } + + if (rz.x.b == rn.x.b && r.x.containsCenter(rn.x.b) ) { // right edges + rn.sides &= ~ RIGHT + } } -// if (r.y.contains(rn.y.a)) { -// rn.sides &= ~ FRONT + +// if (rz.x.contains(r.x.a) || rz.x.contains(r.x.b)) { +// if (r.x.contains(rn.x.b)) { +// rn.sides &= ~ LEFT +// } +// if (r.x.contains(rn.x.b)) { +// rn.sides &= ~ RIGHT +// } // } -// if (r.y.contains(rn.y.a)) { -// rn.sides &= ~ BACK +// if (rz.y.contains(r.y.a) || rz.y.contains(r.y.b)) { +// if (r.y.contains(rn.y.a)) { +// rn.sides &= ~ FRONT +// } +// if (r.y.contains(rn.y.b)) { +// rn.sides &= ~ BACK +// } // } // if (r.intersects(rn)) { diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 447c7a3..5c2b519 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -70,6 +70,9 @@ vec2.prototype.contains = function(n){ return this.a <= n && n <= this.b } + vec2.prototype.containsCenter = function(n){ + return this.a < n && n < this.b + } vec2.prototype.containsDisc = function(n,r){ return this.a <= n-r && n+r <= this.b } diff --git a/test/01-test-vec2.js b/test/01-test-vec2.js index 054d37b..b38f052 100644 --- a/test/01-test-vec2.js +++ b/test/01-test-vec2.js @@ -38,6 +38,13 @@ describe('vec2', function(){ assert.equal(false, vec.contains( -5 )); assert.equal(false, vec.contains( 15 )); }) + it('containsCenter itself', function(){ + assert.equal(true, vec.containsCenter( 5 )); + }) + it('does not containsCenter its endpoints', 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) )); }) diff --git a/test/02-test-rect.js b/test/02-test-rect.js index 0ae59f7..55f1ec9 100644 --- a/test/02-test-rect.js +++ b/test/02-test-rect.js @@ -60,17 +60,15 @@ describe('rect', function(){ return prev | curr.sides }, 0) } + /* + console.log(s0.map(function(r){ return r.toString() })) + console.log(s1.map(function(r){ return r.toString() })) + */ describe('#split(rect, east)', function(){ var s0 = rect.split(east) var s1 = east.split(rect) - console.log("\n") - console.log(rect+"") - console.log(east+"") - console.log(s0.map(function(r){ return r.toString() })) - console.log(s1.map(function(r){ return r.toString() })) - it('splits on all 4 sides', function(){ assert.equal(ALL, sides(s0) | sides(s1)) }) @@ -99,6 +97,7 @@ describe('rect', function(){ describe('#split(rect, east_edge)', function(){ var s0 = rect.split(east_edge) var s1 = east_edge.split(rect) + it('has no degenerate vectors', function(){ s0.forEach(function(r){ assert.notEqual(0, r.x.magnitude()) @@ -113,17 +112,17 @@ describe('rect', function(){ assert.equal(ALL, sides(s0) | sides(s1)) }) it('rect is front/back/left', function(){ - assert.equal(FRONT | BACK | LEFT, sides(s0)) + assert.equal(ALL, sides(s0)) }) it('east is front/back/right', function(){ assert.equal(FRONT | BACK | RIGHT, sides(s1)) }) }) - return describe('#split(rect, south)', function(){ var s0 = rect.split(south) var s1 = south.split(rect) + it('splits on all 4 sides', function(){ assert.equal(ALL, sides(s0) | sides(s1)) }) @@ -135,18 +134,54 @@ describe('rect', function(){ }) }) + describe('#split(rect, south_in)', function(){ + var s0 = rect.split(south_in) + var s1 = south_in.split(rect) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect is has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('south_in only has left/right', function(){ + assert.equal(LEFT | RIGHT, sides(s1)) + }) + }) + + describe('#split(rect, south_edge)', function(){ + var s0 = rect.split(south_edge) + var s1 = south_edge.split(rect) + + it('has no degenerate vectors', function(){ + s0.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + s1.forEach(function(r){ + assert.notEqual(0, r.x.magnitude()) + assert.notEqual(0, r.y.magnitude()) + }) + }) + it('splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0) | sides(s1)) + }) + it('rect has all sides', function(){ + assert.equal(ALL, sides(s0)) + }) + it('south is back/left/right', function(){ + assert.equal(BACK | LEFT | RIGHT, sides(s1)) + }) + }) + describe('#split(rect, corner)', function(){ var s0 = rect.split(corner) var s1 = corner.split(rect) - it('splits on all 4 sides', function(){ - // assert.equal(ALL, sides(s0) | sides(s1)) - }) - it('rect is front/left/right', function(){ - // assert.equal(FRONT | LEFT | RIGHT, sides(s0)) + it('rect splits on all 4 sides', function(){ + assert.equal(ALL, sides(s0)) }) - it('corner is back/left/right', function(){ -// assert.equal(BACK| LEFT | RIGHT, sides(s1)) + it('corner splits on all 4 sides', function(){ + assert.equal(ALL, sides(s1)) }) }) -- cgit v1.2.3-70-g09d2 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 --- .../javascripts/rectangles/engine/rooms/mover.js | 11 +--- .../assets/javascripts/rectangles/models/room.js | 10 +-- .../assets/javascripts/rectangles/models/vec2.js | 2 + test/01-test-vec2.js | 57 +++++++++++++++-- test/05-test-mover.js | 74 ++++++++++++++++++++++ 5 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 test/05-test-mover.js (limited to 'test/01-test-vec2.js') 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