Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created January 18, 2013 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdickinson/da55ec060160d0bf82b5 to your computer and use it in GitHub Desktop.
Save chrisdickinson/da55ec060160d0bf82b5 to your computer and use it in GitHub Desktop.
diff --git a/lib/game.js b/lib/game.js
index fde1174..41cb563 100644
--- a/lib/game.js
+++ b/lib/game.js
@@ -517,12 +517,67 @@ Game.prototype.calculateFreedom = function(cs, pos) {
return freedom
}
+var collisions = require('collide-3d')
+ , aabb = require('aabb-3d')
+
Game.prototype.updatePlayerPhysics = function(controls) {
var self = this
-
+
+
var pos = controls.yawObject.position.clone()
+ , currentID = this.voxels.chunkAtPosition(this.controls.yawObject.position).join('|')
+ , chunk = this.voxels.chunks[currentID]
+
+ if(!chunk) return
+
+ var bbox = aabb(
+ [ pos.x - (chunk.position[0] * chunk.dims[0] * this.cubeSize) - (this.cubeSize / 8)
+ , pos.y - (chunk.position[1] * chunk.dims[1] * this.cubeSize) - this.cubeSize
+ , pos.z - (chunk.position[2] * chunk.dims[2] * this.cubeSize) - (this.cubeSize / 8)]
+ , [this.cubeSize / 4, this.cubeSize * 1.5, this.cubeSize / 4]
+ )
+ , collide = chunk.collide || (chunk.collide = collisions(chunk.voxels, 25, chunk.dims))
+ , base = [this.controls.yawObject.position.x, this.controls.yawObject.position.y, this.controls.yawObject.position.z]
+ , user_vec = [this.controls.velocity.x, this.controls.velocity.y, this.controls.velocity.z]
+ , world_vec
+ this.controls.yawObject.translateX(user_vec[0])
+ this.controls.yawObject.translateY(user_vec[1])
+ this.controls.yawObject.translateZ(user_vec[2])
+
+ world_vec = [this.controls.yawObject.position.x - base[0]
+ , this.controls.yawObject.position.y - base[1]
+ , this.controls.yawObject.position.z - base[2]]
+
+ this.controls.yawObject.translateX(-user_vec[0])
+ this.controls.yawObject.translateY(-user_vec[1])
+ this.controls.yawObject.translateZ(-user_vec[2])
+
+ function sign(x) {
+ return x / Math.abs(x)
+ }
+
+ self.controls.freedom['y-'] = true
+ collide(bbox, world_vec, function(axis, tile, coords, dir, edge_vector) {
+ if(tile) {
+ Math.abs(edge_vector) > 10 && console.log('HIT', ['x', 'y', 'z'][axis], edge_vector, world_vec[axis], JSON.stringify(coords))
+ world_vec[axis] = edge_vector
+ if(axis === 1 && dir === -1) {
+ self.controls.freedom['y-'] = false
+ }
+ return true
+ }
+ })
+
+ this.controls.velocity.x = 0
+ this.controls.velocity.y = 0
+ this.controls.velocity.z = 0
+ this.controls.yawObject.position.x = world_vec[0] + base[0]
+ this.controls.yawObject.position.y = world_vec[1] + base[1]
+ this.controls.yawObject.position.z = world_vec[2] + base[2]
+
+ return
pos.y -= this.cubeSize
-
+
var cs = this.getCollisions(pos, {
width: this.cubeSize / 2,
depth: this.cubeSize / 2,
@@ -533,24 +588,27 @@ Game.prototype.updatePlayerPhysics = function(controls) {
var degrees = 0
Object.keys(freedom).forEach(function (key) { degrees += freedom[key] })
controls.freedom = degrees === 0 ? controls.freedom : freedom
-
+
+ return
var ry = this.controls.yawObject.rotation.y
var v = controls.velocity
var mag = 1
-
+ , vec = [0, 0, 0]
+
if (cs.left.length && !cs.right.length) {
- controls.yawObject.position.x += mag * Math.cos(ry - Math.PI / 2)
+ vec[0] = mag * Math.cos(ry - Math.PI / 2)
}
if (cs.right.length && !cs.left.length) {
- controls.yawObject.position.x += mag * Math.cos(ry + Math.PI / 2)
+ vec[0] = mag * Math.cos(ry + Math.PI / 2)
}
if (cs.forward.length && !cs.back.length) {
- controls.yawObject.position.z += mag * Math.sin(ry)
+ vec[1] = mag * Math.sin(ry)
}
if (cs.back.length && !cs.forward.length) {
- controls.yawObject.position.z += mag * Math.sin(ry - Math.PI)
+ vec[1] = mag * Math.sin(ry - Math.PI)
}
+
}
Game.prototype.bindWASD = function (controls) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment