Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created January 18, 2013 22:50
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/3ec0445a4c8f3aed8084 to your computer and use it in GitHub Desktop.
Save chrisdickinson/3ec0445a4c8f3aed8084 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) {
diff --git a/index.js b/index.js
index 6c18e06..d0bf5be 100644
--- a/index.js
+++ b/index.js
@@ -25,9 +25,9 @@ function PlayerPhysics(camera, opts) {
this.enabled = false
this.speed = {
- jump: opts.jump || 6,
- move: opts.move || 0.12,
- fall: opts.fall || 0.3
+ jump: 120,
+ move: 1.7,
+ fall: 1.298
}
this.pitchObject = opts.pitchObject || new THREE.Object3D()
@@ -57,7 +57,8 @@ function PlayerPhysics(camera, opts) {
this.on('command', function(command, setting) {
if (command === 'jump') {
- if ( self.canJump === true || self.velocity.y === 0) self.velocity.y += self.speed.jump
+
+ if ( self.canJump === true) self.velocity.y += self.speed.jump
self.canJump = false
return
}
@@ -97,6 +98,8 @@ PlayerPhysics.prototype.tick = function (delta, cb) {
this.velocity.z += (-this.velocity.z) * 0.08 * delta
if (this.gravityEnabled) this.velocity.y -= this.speed.fall * delta
+ document.querySelector('.button.tally').innerText = '['+[this.speed.fall * delta, this.velocity.y]
+ .map(function(x) { return x.toFixed(2) })+']'
if (this.moveForward) this.velocity.z -= this.speed.move * delta
if (this.moveBackward) this.velocity.z += this.speed.move * delta
@@ -126,7 +129,7 @@ PlayerPhysics.prototype.tick = function (delta, cb) {
}
if (!this.freedom['y-']) this.canJump = true
-
+
this.yawObject.translateX( this.velocity.x )
this.yawObject.translateY( this.velocity.y )
this.yawObject.translateZ( this.velocity.z )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment