summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/mx/extensions
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/mx/extensions')
-rw-r--r--public/assets/javascripts/mx/extensions/mx.movements.js406
-rw-r--r--public/assets/javascripts/mx/extensions/mx.movementsMobile.js203
-rw-r--r--public/assets/javascripts/mx/extensions/mx.orbitCamera.js97
3 files changed, 502 insertions, 204 deletions
diff --git a/public/assets/javascripts/mx/extensions/mx.movements.js b/public/assets/javascripts/mx/extensions/mx.movements.js
index 669a7f4..3e34c1b 100644
--- a/public/assets/javascripts/mx/extensions/mx.movements.js
+++ b/public/assets/javascripts/mx/extensions/mx.movements.js
@@ -16,193 +16,281 @@ MX.Movements = function (cam) {
creeping = false,
locked = false,
gravity = false,
- rotationX_min = PI/-2,
- rotationX_max = PI/2
+ rotationX_min = PI/-4,
+ rotationX_max = PI/6
var v = 12,
- vr = Math.PI * 0.012,
+ vr = Math.PI * 0.018,
jumpV = 23,
vx = vy = vz = 0,
creepFactor = 0.3
+ var mouseX, mouseY, dx, dy, rotX, rotY, dragging = false
+
+ var trackpad
+
var DEFAULT_SCALE = 1.0, scale = DEFAULT_SCALE
var pos = { x: 0, y: 0, z: 0, rotationX: 0, rotationY: 0 }
- return {
+ $(document).one("keydown", function(){
+ $("#keyhint").fadeOut(250);
+ $('.reader #minimap').addClass('active');
+ })
- init: function () {
+ function clampRotation( vr ) {
+ if (window.Rooms && Rooms.mover.noclip) {
+ return clamp(vr, PI/-2, PI/2 )
+ }
+ else {
+ return clamp(vr, PI/-4, PI/6 )
+ }
+ }
- document.addEventListener('keydown', function (e) {
- // console.log(e.keyCode)
- if (locked || e.altKey || e.metaKey || e.ctrlKey) {
- return
- }
- switch ( e.keyCode ) {
-
- case 16: // shift
- creeping = true
- break
+ var exports = {
+
+ init: function () {
+
+ trackpad = new wheel ({
+ el: scene.el,
+ update: exports.mousewheel,
+ })
+
+ document.addEventListener('keydown', exports.keydown)
+ document.addEventListener('keyup', exports.keyup)
+ document.addEventListener('mousedown', exports.mousedown)
+ document.addEventListener('mousemove', exports.mousemove)
+ document.addEventListener('mouseup', exports.mouseup)
+ window.addEventListener('blur', exports.reset)
+ window.addEventListener('focus', exports.reset)
+ },
+
+ keydown: function (e) {
+ // console.log(e.keyCode)
+ if (locked || e.altKey || e.metaKey || e.ctrlKey) {
+ return
+ }
+ switch ( e.keyCode ) {
+
+ case 16: // shift
+ creeping = true
+ break
- case 38: // up
- case 87: // w
- moveForward = true
- break
+ case 38: // up
+ case 87: // w
+ moveForward = true
+ break
- case 37: // left
- case 65: // a
- moveLeft = true
- break
+ case 65: // a
+ moveLeft = true
+ break
- case 40: // down
- case 83: // s
- moveBackward = true
- break
+ case 40: // down
+ case 83: // s
+ moveBackward = true
+ break
- case 39: // right
- case 68: // d
- moveRight = true
- break
+ case 68: // d
+ moveRight = true
+ break
- case 81: // q
- turnLeft = true
- break
-
- case 69: // e
- turnRight = true
- break
-
- case 82: // r
- turnUp = true
- break
+ case 37: // left
+ case 81: // q
+ turnLeft = true
+ break
+
+ case 39: // right
+ case 69: // e
+ turnRight = true
+ break
+
+ case 82: // r
+ turnUp = true
+ break
- case 70: // f
- turnDown = true
- break
+ case 70: // f
+ turnDown = true
+ break
- case 32: // space
- if (gravity) {
- jumping = true
- vy = abs(vy) + jumpV * scale
- if (e.shiftKey) {
- vy *= -1
- }
+ case 32: // space
+ if (gravity) {
+ jumping = true
+ vy = abs(vy) + jumpV * scale
+ if (e.shiftKey) {
+ vy *= -1
+ }
+ }
+ else {
+ if (e.shiftKey) {
+ moveDown = true
}
else {
- if (e.shiftKey) {
- moveDown = true
- }
- else {
- moveUp = true
- }
+ moveUp = true
}
- break
+ }
+ break
- case 27: // esc
- map.toggle()
- break
- }
- })
+ case 27: // esc
+ if (Scenery.nextMedia) {
+ Scenery.nextMedia = null
+ app.tube('cancel-scenery')
+ }
+ else if (Scenery.nextWallpaper) {
+ Scenery.nextWallpaper = null
+ app.tube('cancel-wallpaper')
+ }
+ else if (app.controller.mediaViewer && app.controller.mediaViewer.$el.hasClass("active")) {
+ app.controller.mediaViewer.hide()
+ $(".inuse").removeClass("inuse")
+ }
+ else if (app.controller.colorControl && app.controller.colorControl.$el.hasClass('active')) {
+ app.controller.colorControl.hide()
+ $(".inuse").removeClass("inuse")
+ }
+ else if (app.controller.wallpaperPicker && app.controller.wallpaperPicker.$el.hasClass('active')) {
+ app.controller.wallpaperPicker.hide()
+ $(".inuse").removeClass("inuse")
+ }
+ else if (app.controller.presets && app.controller.presets.$el.hasClass('active')) {
+ app.controller.presets.hide()
+ $(".inuse").removeClass("inuse")
+ }
+ else if (Rooms.shapesMode) {
+ // don't show map in editor for now..
+ }
+ else {
+ app.controller.toolbar.toggleMap()
+ }
+ break
+
+ case 8: // backspace
+ e.preventDefault()
+ if (app.controller.sculptureEditor && app.controller.sculptureEditor.sculpture) {
+ app.controller.sculptureEditor.sculpture.remove()
+ }
+ else if (app.controller.mediaEditor && app.controller.mediaEditor.scenery) {
+ app.controller.mediaEditor.scenery.remove()
+ }
+ else if (app.controller.textEditor && app.controller.textEditor.scenery) {
+ app.controller.textEditor.scenery.remove()
+ }
+ }
+ },
- document.addEventListener('keyup', function (e) {
- if (locked) return;
- switch ( e.keyCode ) {
-
- case 16: // shift
- creeping = false
- break
+ keyup: function (e) {
+ if (locked) return;
+ switch ( e.keyCode ) {
+
+ case 16: // shift
+ creeping = false
+ break
- case 38: // up
- case 87: // w
- moveForward = false
- break
+ case 38: // up
+ case 87: // w
+ moveForward = false
+ break
- case 37: // left
- case 65: // a
- moveLeft = false
- break
+ case 65: // a
+ moveLeft = false
+ break
- case 40: // down
- case 83: // s
- moveBackward = false
- break
+ case 40: // down
+ case 83: // s
+ moveBackward = false
+ break
- case 39: // right
- case 68: // d
- moveRight = false
- break
+ case 68: // d
+ moveRight = false
+ break
- case 81: // q
- turnLeft = false
- break
-
- case 69: // e
- turnRight = false
- break
+ case 37: // left
+ case 81: // q
+ turnLeft = false
+ break
+
+ case 39: // right
+ case 69: // e
+ turnRight = false
+ break
- case 82: // r
- turnUp = false
- break
+ case 82: // r
+ turnUp = false
+ break
- case 70: // f
- turnDown = false
- break
+ case 70: // f
+ turnDown = false
+ break
- case 32: // space
- moveUp = moveDown = false
- break
+ case 32: // space
+ moveUp = moveDown = false
+ break
/*
- case 48: // 0
- cam.rotationX = 0
- cam.rotationY = 0
- cam.x = 0
- cam.y = viewHeight
- cam.z = 0
- break
+ case 48: // 0
+ movements.center()
+ break
*/
- }
- })
-
- var mouseX, mouseY, dx, dy, rotX, rotY, dragging = false
- document.addEventListener('mousedown', function (e) {
- if (locked) return;
- mouseX = e.pageX
- mouseY = e.pageY
- rotX = cam.rotationX
- rotY = cam.rotationY
- dragging = true
- })
-
- document.addEventListener('mousemove', function (e) {
- if (locked || ! dragging) return
- var dx = (e.pageX - mouseX) / window.innerWidth * Math.PI/3
- var dy = (e.pageY - mouseY) / window.innerHeight * Math.PI/3
- cam.rotationY = rotY + dx
- cam.rotationX = clamp( rotX - dy, rotationX_min, rotationX_max )
- })
-
- document.addEventListener('mouseup', function (e) {
- dragging = false
- })
-
- window.addEventListener('blur', reset)
- window.addEventListener('focus', reset)
- function reset(){
- moveForward = moveLeft = moveBackward = moveRight = moveUp = moveDown = turnLeft = turnRight = jumping = dragging = creeping = false
}
+ },
+
+ center: function(){
+ cam.rotationX = 0
+ cam.rotationY = 0
+ cam.x = 0
+ cam.y = viewHeight
+ cam.z = 0
+ },
+ mousedown: function (e) {
+ if (locked) return;
+ mouseX = e.pageX
+ mouseY = e.pageY
+ rotX = cam.rotationX
+ rotY = cam.rotationY
+ dragging = true
+ },
+
+ mousemove: function (e) {
+ if (locked || ! dragging) return
+ var dx = (e.pageX - mouseX) / window.innerWidth * Math.PI/3
+ var dy = (e.pageY - mouseY) / window.innerHeight * Math.PI/3
+ cam.rotationY = rotY + dx
+ cam.rotationX = clampRotation( rotX - dy )
+ },
+
+ mouseup: function (e) {
+ dragging = false
+ },
+
+ reset: function(){
+ moveForward = moveLeft = moveBackward = moveRight = moveUp = moveDown = turnLeft = turnRight = jumping = dragging = creeping = false
+ },
+
+ mousewheel: function (e, deltaY, deltaX) {
+ if (e.shiftKey) {
+ cam.rotationY -= deltaY / 150
+ }
+ else {
+ pos.x += deltaY * Math.cos(cam.rotationY + Math.PI / 2) * 10
+ pos.z += deltaY * Math.sin(cam.rotationY + Math.PI / 2) * 10
+ app.tube("move", pos)
+ }
},
- update: function () {
+ update: function (dt) {
- if (locked) return;
+ if (locked) { return }
var ry = cam.rotationY
var s = creeping ? scale * creepFactor : scale
- var vrrrr = creeping ? vr * creepFactor * 5 : vr
+ var vrrrr = creeping ? vr * creepFactor * 5 : vr * 0.5
var moving = moveForward || moveBackward || moveRight || moveLeft || moveUp || moveDown || turnLeft || turnRight || turnUp || turnDown
vx = vz = 0
+
+ var vv = v
+// vv *= dt / 100 * 8
+// s *= dt / 100 * 8
+// console.log(dt / 100 * 8)
pos.x = cam.x
pos.z = cam.z
@@ -210,33 +298,33 @@ MX.Movements = function (cam) {
if (moving) {
if (moveForward) {
- vx += v * Math.cos(ry + Math.PI / 2) * s
- vz += v * Math.sin(ry + Math.PI / 2) * s
+ vx += vv * Math.cos(ry + Math.PI / 2) * s
+ vz += vv * Math.sin(ry + Math.PI / 2) * s
}
if (moveBackward) {
- vx -= v * Math.cos(ry + Math.PI / 2) * s
- vz -= v * Math.sin(ry + Math.PI / 2) * s
+ vx -= vv * Math.cos(ry + Math.PI / 2) * s
+ vz -= vv * Math.sin(ry + Math.PI / 2) * s
}
if (moveLeft) {
- vx -= v * Math.cos(ry) * s
- vz -= v * Math.sin(ry) * s
+ vx -= vv * Math.cos(ry) * s
+ vz -= vv * Math.sin(ry) * s
}
if (moveRight) {
- vx += v * Math.cos(ry) * s
- vz += v * Math.sin(ry) * s
+ vx += vv * Math.cos(ry) * s
+ vz += vv * Math.sin(ry) * s
}
if (moveUp) {
- pos.y += v * scale
+ pos.y += vv * scale
}
if (moveDown) {
- pos.y -= v * scale
+ pos.y -= vv * scale
}
if (turnUp) {
- cam.rotationX = clamp( cam.rotationX - vrrrr*s, rotationX_min, rotationX_max)
+ cam.rotationX = clampRotation( cam.rotationX - vrrrr*s )
}
if (turnDown) {
- cam.rotationX = clamp( cam.rotationX + vrrrr*s, rotationX_min, rotationX_max)
+ cam.rotationX = clampRotation( cam.rotationX + vrrrr*s )
}
if (turnLeft) {
cam.rotationY += vrrrr*s
@@ -261,15 +349,13 @@ MX.Movements = function (cam) {
pos.y = viewHeight
vy = 0
jumping = false
- vz = vz || 1
}
- var ceiling = (Rooms.mover.room ? Rooms.mover.room.height : 5000)
+ var ceiling = ((window.Rooms && Rooms.mover.room) ? Rooms.mover.room.height : 5000)
- if (pos.y >= ceiling) {
+ if (pos.y >= ceiling-5) {
vy = 0
- pos.y = ceiling
- vz = vz || 1
+ pos.y = ceiling-5
}
}
@@ -286,4 +372,6 @@ MX.Movements = function (cam) {
velocity: function(n){ v = clamp(n, 1, 50) },
jumpVelocity: function(n){ jumpV = clamp(n, 1, 50) },
}
+
+ return exports
}
diff --git a/public/assets/javascripts/mx/extensions/mx.movementsMobile.js b/public/assets/javascripts/mx/extensions/mx.movementsMobile.js
index 994c8d7..95b61d1 100644
--- a/public/assets/javascripts/mx/extensions/mx.movementsMobile.js
+++ b/public/assets/javascripts/mx/extensions/mx.movementsMobile.js
@@ -14,56 +14,169 @@ MX.MobileMovements = function (cam) {
var pos = { x: 0, y: viewHeight, z: 0, rotationX: 0, rotationY: 0 }
var pointX, pointY, deltaX, deltaY, distX = 0, distY = 0, absDistX = 0, absDistY = 0, startTime
-
- return {
+
+ var rotationX = 0, rotationY = 0, destRotationX = 0, destRotationY = 0
+ var rotationSum = 0
+ var rotationMedian = 0
+ var orientationMax = 0
+ var samples = 0
+ var sampleThreshold = 120
+ var lastAlpha
+
+ var is_portrait
+
+ var exports = {
init: function () {
- document.addEventListener("touchstart", function(e){
- if (e.touches.length == 1) {
- touching = true
-
- startTime = Date.now()
-
- var point = event.touches[0]
- pointX = point.pageX
- pointY = point.pageY
- distX = distY = 0
- pos.x = cam.x
- pos.z = cam.z
- pos.rotationY = cam.rotationY
- }
- })
- document.addEventListener("touchmove", function(e){
- e.preventDefault()
- if (e.touches.length == 1) {
+ exports.orientationchange()
+
+ document.addEventListener("touchstart", exports.touchstart)
+ document.addEventListener("touchmove", exports.touchmove)
+ document.addEventListener("touchend", exports.touchend)
+ window.addEventListener('orientationchange', exports.orientationchange)
+ window.addEventListener("devicemotion", exports.devicemotion)
+ window.addEventListener("deviceorientation", exports.deviceorientation)
+ },
+ touchstart: function(e){
+ if (e.touches.length == 1) {
+ touching = true
+
+ startTime = Date.now()
+
+ var point = event.touches[0]
+ pointX = point.pageX
+ pointY = point.pageY
+ distX = distY = 0
+ pos.x = cam.x
+ pos.z = cam.z
+ pos.rotationY = cam.rotationY
+ }
+ },
+ touchmove: function(e){
+ e.preventDefault()
+ if (e.touches.length == 1) {
+
+ var timestamp = Date.now()
+ var point = event.touches[0]
+ deltaX = point.pageX - pointX
+ deltaY = point.pageY - pointY
+
+ pointX = point.pageX
+ pointY = point.pageY
+
+ distX += deltaX
+ distY += deltaY
+ absDistX = abs(distX)
+ absDistY = abs(distY)
+ }
+ },
+ touchend: function(e){
+ e.preventDefault()
+ if (e.touches.length == 0) {
+ touching = directionLocked = false
+ var timestamp = Date.now()
+ var duration = startTime - timestamp
+ if (duration < 300) {
+ }
+ }
+ },
+ orientationchange: function(e){
+ is_portrait = window.innerWidth < window.innerHeight
+ if (is_portrait) {
+ lastAlpha = 0
+ }
+ },
+ devicemotion: function(e) {
+ if (! is_portrait) return;
+ var rotationBeta = e.rotationRate.alpha; // weird!
+ rotationSum += rotationBeta;
+ samples += 1;
+ },
+ deviceorientation: function (e) {
+ if (! lastAlpha) { lastAlpha = e.alpha }
+ is_portrait ? exports.portraitorientation(e) : exports.landscapeorientation(e)
+ },
+ portraitorientation: function(e) {
+ // compass gives most accurate orientation in portrait mode
+ var alpha, dx = 0, dy = 0
+
+ if (e.webkitCompassHeading) {
+ alpha = 180 - e.webkitCompassHeading;
+ }
+ else {
+ alpha = 180 - e.alpha;
+ }
+
+ // android rotates in reverse
+ if (is_android) {
+ alpha = 360 - alpha
+ }
+
+ // use rotationRate to gauge if we've tilted the screen past vertical
+ // for looking at ceiling
+ if (e.beta > orientationMax) {
+ orientationMax = e.beta
+ rotationMedian = rotationSum
+ }
+
+ // this number was only going to 83 max.. not 90.. weird
+ var beta = e.beta + 7;
+
+ // if we've got enough motion data, we should be able to determine
+ // if we've tilted backwards. otherwise, lock to the horizon.
+ if (! is_android && samples > sampleThreshold) {
+ dx = rotationSum > rotationMedian ? e.beta - 90 : 90 - e.beta
+ }
+ else {
+ dx = 0
+ }
+
+ // avoid jumping around in a circle
+ if (Math.abs(alpha - lastAlpha) < 100 || Math.abs(alpha - lastAlpha) > 300) {
+ dy = alpha - lastAlpha
+ lastAlpha = alpha
+ }
+
+ // avoid jumping around in a circle #2
+ if (dy > 300) {
+ dy -= 360
+ } else if (dy < -300) {
+ dy += 360
+ }
+
+ destRotationX = MX.toRad(dx)
+ destRotationY += MX.toRad(dy)
+ },
+
+ landscapeorientation: function (e) {
+ var dx, dy
+
+ dx = e.gamma > 0 ? 90 - e.gamma : 90 + e.gamma
+ dy = e.alpha - lastAlpha
+ lastAlpha = e.alpha
- var timestamp = Date.now()
- var point = event.touches[0]
- deltaX = point.pageX - pointX
- deltaY = point.pageY - pointY
-
- pointX = point.pageX
- pointY = point.pageY
+ // avoid the sudden jump from 0 to -360
+ if (dy > 300) {
+ dy -= 360
+ }
+ else if (dy < -300) {
+ dy += 360
+ }
- distX += deltaX
- distY += deltaY
- absDistX = abs(distX)
- absDistY = abs(distY)
- }
- })
- document.addEventListener("touchend", function(e){
- e.preventDefault()
- if (e.touches.length == 0) {
- touching = directionLocked = false
- var timestamp = Date.now()
- var duration = startTime - timestamp
- if (duration < 300) {
- }
- }
- })
+ destRotationX = dx > 45 ? 0 : MX.toRad(dx)
+ destRotationY += MX.toRad(dy)
},
update: function () {
+ var drx, dry
+
+ dry = (destRotationY - rotationY) / 6
+ drx = (destRotationX - rotationX) / 6
+ rotationY += dry
+ rotationX += drx
+ cam.rotationY = pos.rotationY += dry
+ cam.rotationX = pos.rotationX += drx
+
if (distX || distY) {
var oldDistY = absDistY, oldDistX = absDistX
absDistY = avg(absDistY, 0, 5)
@@ -77,8 +190,7 @@ MX.MobileMovements = function (cam) {
pos.x -= dy * Math.cos(pos.rotationY + Math.PI / 2)
pos.z -= dy * Math.sin(pos.rotationY + Math.PI / 2)
- pos.rotationY += dx / (window.innerWidth) * Math.PI / 2
- cam.rotationY = pos.rotationY
+ cam.rotationY = pos.rotationY += dx / (window.innerWidth) * Math.PI / 2
app.tube("move", pos)
}
@@ -93,6 +205,7 @@ MX.MobileMovements = function (cam) {
jumpVelocity: function(n){ jumpV = clamp(n, 1, 50) },
}
+ return exports
}
diff --git a/public/assets/javascripts/mx/extensions/mx.orbitCamera.js b/public/assets/javascripts/mx/extensions/mx.orbitCamera.js
new file mode 100644
index 0000000..a936cef
--- /dev/null
+++ b/public/assets/javascripts/mx/extensions/mx.orbitCamera.js
@@ -0,0 +1,97 @@
+MX.OrbitCamera = function(opt){
+ var exports = {}, bound = false
+ exports.opt = opt = defaults(opt, {
+ el: window, // object to bind events on
+ camera: scene.camera, // camera object we'll be moving
+ radius: 100,
+ radiusRange: [ 10, 1000 ],
+ rotationX: PI/2,
+ rotationY: 0,
+ center: { x: 0, y: 0, z: 0 },
+ sensitivity: 10, // moving 1 pixel is like moving N radians
+ wheelSensitivity: 10,
+ ease: 10,
+ })
+ var rx, ry, radius, px, py, epsilon = 1e-10, dragging = false
+ exports.init = function(){
+ ry = opt.rotationY
+ rx = opt.rotationX
+ radius = opt.radius
+ exports.wheel = new wheel({
+ el: opt.el,
+ update: function(e, delta){
+ opt.radius = clamp( opt.radius + delta * opt.wheelSensitivity, opt.radiusRange[0], opt.radiusRange[1] )
+ },
+ })
+ exports.bind()
+ }
+ exports.toggle = function(state){
+ if (state) exports.bind()
+ else exports.unbind()
+ }
+ exports.bind = function(){
+ if (bound) return;
+ bound = true
+ opt.el.addEventListener("mousedown", down)
+ window.addEventListener("mousemove", move)
+ window.addEventListener("mouseup", up)
+ exports.wheel.unlock()
+ }
+ exports.unbind = function(){
+ if (! bound) return;
+ bound = false
+ opt.el.removeEventListener("mousedown", down)
+ window.removeEventListener("mousemove", move)
+ window.removeEventListener("mouseup", up)
+ exports.wheel.lock()
+ }
+ function down (e) {
+ px = e.pageX
+ py = e.pageY
+ dragging = true
+ }
+ function move (e) {
+ if (! dragging) return
+ exports.delta(px- e.pageX, py - e.pageY)
+ px = e.pageX
+ py = e.pageY
+ }
+ function up (e) {
+ dragging = false
+ }
+ exports.delta = function(x,y){
+ opt.rotationY += x/window.innerWidth * opt.sensitivity
+ opt.rotationX = clamp( opt.rotationX + y/window.innerHeight * opt.sensitivity, 0, PI)
+ }
+ exports.move = function(y, x){
+ opt.rotationY = y
+ if (typeof x == "number") { opt.rotationX = x }
+ }
+ exports.update = function(){
+ if (! bound) return
+ if (abs(ry - opt.rotationY) > epsilon) {
+ ry = avg(ry, opt.rotationY, opt.ease)
+ }
+ else {
+ ry = opt.rotationY
+ }
+ if (abs(rx - opt.rotationX) > epsilon) {
+ rx = avg(rx, opt.rotationX, opt.ease)
+ }
+ else {
+ rx = opt.rotationX
+ }
+ if (abs(radius - opt.radius) > epsilon) {
+ radius = avg(radius, opt.radius, opt.ease)
+ }
+ else {
+ radius = opt.radius
+ }
+ opt.camera.x = opt.center.x + radius * sin(rx) * cos(ry)
+ opt.camera.y = opt.center.y + radius * cos(rx)
+ opt.camera.z = opt.center.z + radius * sin(rx) * sin(ry)
+ opt.camera.rotationX = PI/2 - rx
+ opt.camera.rotationY = ry + PI/2
+ }
+ return exports
+}