summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-04-24 16:36:49 -0400
committerJules Laplace <jules@okfoc.us>2014-04-24 16:36:49 -0400
commit0e4bc72fe453797743d63f57db47ef3e0468fc65 (patch)
tree31c8983117d7043ff2af5bc315691900ae80a592
parent796e1194d13bfa16f41676d81f7d9cdb4b9fc96f (diff)
collide based on a radius
-rw-r--r--assets/javascripts/mx/extensions/mx.movements.js2
-rw-r--r--assets/javascripts/rectangles/_env.js20
-rw-r--r--assets/javascripts/rectangles/engine/mover.js17
-rw-r--r--assets/javascripts/rectangles/models/rect.js18
-rw-r--r--assets/javascripts/rectangles/models/vec2.js9
5 files changed, 45 insertions, 21 deletions
diff --git a/assets/javascripts/mx/extensions/mx.movements.js b/assets/javascripts/mx/extensions/mx.movements.js
index 35a23d0..df3d79c 100644
--- a/assets/javascripts/mx/extensions/mx.movements.js
+++ b/assets/javascripts/mx/extensions/mx.movements.js
@@ -19,7 +19,7 @@ MX.Movements = function (cam, viewHeight) {
rotationX_min = PI/-2,
rotationX_max = PI/2
- var v = 15,
+ var v = 10,
vr = Math.PI * 0.015,
jumpV = 23,
vx = vy = vz = 0,
diff --git a/assets/javascripts/rectangles/_env.js b/assets/javascripts/rectangles/_env.js
index 5b4643a..d41d490 100644
--- a/assets/javascripts/rectangles/_env.js
+++ b/assets/javascripts/rectangles/_env.js
@@ -1,14 +1,18 @@
var environment = new function(){}
environment.init = function(){
- window.scene && scene.camera.move({
- "x": 1000,
- "y": 1500,
- "z": 250,
- "rotationX": 0, // PI/2,
- "rotationY": PI/2, // PI
- })
-
+ if (window.scene) {
+ scene.camera.move({
+ "x": 1000,
+ "y": 1500,
+ "z": 250,
+ "rotationX": 0, // PI/2,
+ "rotationY": PI/2, // PI
+ })
+
+ scene.camera.radius = 25
+ }
+
map.center.a = scene.camera.x
map.center.b = scene.camera.z
// map.center.a = 0
diff --git a/assets/javascripts/rectangles/engine/mover.js b/assets/javascripts/rectangles/engine/mover.js
index 311ed83..66c1813 100644
--- a/assets/javascripts/rectangles/engine/mover.js
+++ b/assets/javascripts/rectangles/engine/mover.js
@@ -13,26 +13,29 @@ var mover = new function(){
}
base.update = function(pos){
+ var radius = scene.camera.radius
+
cam.y = pos.y
// if we were in a room already..
if (base.room) {
// check if we're still in the room
- if (base.room.rect.contains(pos.x, pos.z)) {
+ if (base.room.rect.containsDisc(pos.x, pos.z, radius)) {
cam.x = pos.x
cam.z = pos.z
return
}
-
+
// check if we've breached one of the walls.. clamp position if so
- var collision = base.room.collides(pos.x, pos.z)
+ var collision = base.room.collides(pos.x, pos.z, radius)
+
if (collision) {
- if (! (collision & LEFT || collision & RIGHT)) {
- cam.x = pos.x
+ if (! (collision & LEFT_RIGHT)) {
+ cam.x = base.room.rect.x.clampDisc(pos.x, radius)
}
- if (! (collision & FRONT || collision & BACK)) {
- cam.z = pos.z
+ if (! (collision & FRONT_BACK)) {
+ cam.z = base.room.rect.y.clampDisc(pos.z, radius)
}
return
}
diff --git a/assets/javascripts/rectangles/models/rect.js b/assets/javascripts/rectangles/models/rect.js
index 86cdae3..51162c0 100644
--- a/assets/javascripts/rectangles/models/rect.js
+++ b/assets/javascripts/rectangles/models/rect.js
@@ -1,6 +1,15 @@
var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8,
FRONT_BACK = FRONT | BACK, LEFT_RIGHT = LEFT | RIGHT
+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 "
+ return s
+}
+
window.rect = (function(){
var rect = function (x0,y0,x1,y1){
if (x0 instanceof vec2) {
@@ -49,17 +58,16 @@ window.rect = (function(){
rect.prototype.contains = function(x,y){
return this.x.contains(x) && this.y.contains(y)
}
+ rect.prototype.containsDisc = function(x,y,r){
+ return this.x.contains(x,r) && this.y.contains(y,r)
+ }
rect.prototype.intersects = function(r){
return this.x.intersects(r.x) && this.y.intersects(r.y)
}
rect.prototype.width = function(){ return this.x.length() }
rect.prototype.height = function(){ return this.y.length() }
rect.prototype.toString = function(){
- var sides = ""
- if (this.sides & FRONT) sides += "front "
- if (this.sides & BACK) sides += "back "
- if (this.sides & LEFT) sides += "left "
- if (this.sides & RIGHT) sides += "right "
+ var sides = sidesToString(this.sides)
var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides
return s
}
diff --git a/assets/javascripts/rectangles/models/vec2.js b/assets/javascripts/rectangles/models/vec2.js
index 3105fec..7307fca 100644
--- a/assets/javascripts/rectangles/models/vec2.js
+++ b/assets/javascripts/rectangles/models/vec2.js
@@ -54,6 +54,15 @@ vec2.normalize = function(){
vec2.prototype.contains = function(n){
return this.a <= n && n <= this.b
}
+vec2.prototype.containsDisc = function(n,r){
+ return this.a <= n-r && n+r <= this.b
+}
+vec2.prototype.clamp = function(n){
+ return clamp(n, this.a, this.b)
+}
+vec2.prototype.clampDisc = function(n,r){
+ return clamp(n, this.a+r, this.b-r)
+}
vec2.prototype.intersects = function(v){
if (this.a < v.a) {
return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b)