summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles')
-rw-r--r--public/assets/javascripts/rectangles/engine/rooms/mover.js95
-rw-r--r--public/assets/javascripts/rectangles/models/rect.js6
-rw-r--r--public/assets/javascripts/rectangles/models/surface.js9
-rw-r--r--public/assets/javascripts/rectangles/models/vec2.js5
4 files changed, 90 insertions, 25 deletions
diff --git a/public/assets/javascripts/rectangles/engine/rooms/mover.js b/public/assets/javascripts/rectangles/engine/rooms/mover.js
index 121ecec..8ce00fe 100644
--- a/public/assets/javascripts/rectangles/engine/rooms/mover.js
+++ b/public/assets/javascripts/rectangles/engine/rooms/mover.js
@@ -36,6 +36,18 @@ Rooms.mover = new function(){
// check if we've breached one of the walls.. clamp position if so
var collision = base.room.collidesDisc(cam, pos, radius)
+ if (collision && ! base.noclip) {
+ 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
+ }
+
+ // in this case, we appear to have left the room..
+ // $(".face.active").removeClass("active")
+ Walls.clearBodyColor()
+ base.room = null
+ }
+
/*
var dz = new vec2( cam.z, pos.z )
dz.normalize()
@@ -43,47 +55,82 @@ Rooms.mover = new function(){
var dx = new vec2( cam.x, pos.x )
dx.normalize()
+ // first check if the cam-pos movement intersects the wall.
+ // next check if it intersects any of the surface frames
+ // if we can pass through the wall at this point, we do not need to clamp.
+ // otherwise, get the distance along the cam-pos vector where we would hit the wall and clamp to it.
Walls.list.forEach(function(wall){
+ var t = -1
switch (wall.side) {
case FRONT:
+ if (cam.z >= wall.edge + radius && wall.edge + radius >= pos.z && wall.vec.intersects(dx) ) {
+ t = check_intersection( front_back_intersection, cam, pos, dx, wall, radius )
+ }
break
case BACK:
+ // console.log(cam.z|0, wall.edge-radius, pos.z|0, wall.vec.intersects(dx))
+ if (cam.z <= wall.edge - radius && wall.edge - radius <= pos.z && wall.vec.intersects(dx) ) {
+ t = check_intersection( front_back_intersection, cam, pos, dx, wall, -radius )
+ console.log(t)
+ }
break
case LEFT:
- if (cam.x >= wall.edge + radius && wall.edge + radius >= pos.x && (wall.vec.intersects(dz) ) {
- // intersects the wall.. next check if it intersects any of the surface frames
- wall.surface.faces.some(function(face, i){
- // if we can pass through the wall at this point, we do not need to clamp
- if (face.y.a === 0 && face.x.intersects(dz)) {
- dz.a = pos.z = face.x.clamp(pos.z)
- dz.b = cam.z
- dz.normalize()
- return true
- }
- })
+ if (cam.x >= wall.edge + radius && wall.edge + radius >= pos.x && wall.vec.intersects(dz) ) {
+ t = check_intersection( left_right_intersection, cam, pos, dz, wall, radius )
}
break
case RIGHT:
- if (cam.x <= wall.edge - radius && wall.edge - radius <= pos.x && (wall.vec.contains(cam.z) || wall.vec.contains(pos.z)) ) {
- // intersects
+ if (cam.x <= wall.edge - radius && wall.edge - radius <= pos.x && wall.vec.intersects(dz) ) {
+ t = check_intersection( left_right_intersection, cam, pos, dz, wall, -radius )
}
break
}
+ if (0 <= t && t <= 1) {
+ pos.x = cam.x + (pos.x - cam.x) * t
+ pos.z = cam.z + (pos.z - cam.z) * t
+
+ dz = new vec2( cam.z, pos.z )
+ dz.normalize()
+
+ dx = new vec2( cam.x, pos.x )
+ dx.normalize()
+ }
})
-*/
- if (collision && ! base.noclip) {
- 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
- }
+ function check_intersection(intersection_function, cam, pos, cam_pos_vector, wall, radius) {
+ var t = -1
+ wall.surface.faces.some(function(face, i){
+ if (face.y.a == 0 && face.x.intersects(cam_pos_vector)) {
+ t = intersection_function( cam, pos, wall, face, radius )
+ console.log(">>", t)
+ if (0 <= t && t <= 1) {
+ return true
+ }
+ else {
+ t = -1
+ }
+ }
+ return false
+ })
+ return t
+ }
+ function left_right_intersection (cam, pos, wall, face, radius) {
+ var perp_n = (face.x.a - face.x.b) * (wall.edge - cam.z + radius)
+ var perp_d = (face.x.a - face.x.b) * (pos.z - cam.z)
+ if (perp_d == 0) return Infinity
+ return perp_n / perp_d
+ }
+ function front_back_intersection (cam, pos, wall, face, radius) {
+ // va.vx*vb.vy - va.vy*vb.vx
+ var perp_n = (face.x.b - face.x.a) * (wall.edge - cam.x + radius)
+ var perp_d = (face.x.b - face.x.a) * (pos.x - cam.x)
- // in this case, we appear to have left the room..
- // $(".face.active").removeClass("active")
- Walls.clearBodyColor()
- base.room = null
- }
+ console.log((pos.x - cam.x), wall.edge - cam.x, radius)
+ if (perp_d == 0) return Infinity
+ return perp_n / perp_d
+ }
+*/
// collision test failed, so update position
cam.x = pos.x
cam.z = pos.z
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js
index 00f2c55..c667cf5 100644
--- a/public/assets/javascripts/rectangles/models/rect.js
+++ b/public/assets/javascripts/rectangles/models/rect.js
@@ -169,6 +169,12 @@
var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides
return s
}
+ Rect.prototype.exactString = function(){
+ var sides = sidesToString(this.sides)
+ var s = "[" + this.x.exactString() + " " + this.y.exactString() + "] " + sides
+ return s
+ }
+
Rect.prototype.serialize = function(){
return { x: this.x.serialize(), y: this.y.serialize() }
}
diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js
index 3a6c449..fc4aae4 100644
--- a/public/assets/javascripts/rectangles/models/surface.js
+++ b/public/assets/javascripts/rectangles/models/surface.js
@@ -154,6 +154,15 @@
}
return -1
}
+ Surface.prototype.face_for_x = function(x, min_i){
+ min_i = min_i || 0
+ for (var i = min_i; i < this.faces.length; i++) {
+ if (this.faces[i].x.contains(x)) {
+ return this.faces[i]
+ }
+ }
+ return null
+ }
Surface.prototype.bounds_at_index_with_dimensions = function(index, dimensions){
var faces = this.faces
diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js
index 49613c3..f28df54 100644
--- a/public/assets/javascripts/rectangles/models/vec2.js
+++ b/public/assets/javascripts/rectangles/models/vec2.js
@@ -200,7 +200,10 @@
}
vec2.prototype.toString = function(){
- return "[" + ~~this.a + " " + ~~this.b + "]"
+ return "[" + round(this.a) + " " + round(this.b) + "]"
+ }
+ vec2.prototype.exactString = function(){
+ return "[" + this.a + " " + this.b + "]"
}
vec2.prototype.serialize = function(){
return [ ~~this.a, ~~this.b ]