Permalink
Browse files

effing collision detection. better aabs.

  • Loading branch information...
1 parent 4c8bdde commit db0605f82503ceb2ccef68339c04e7b39617d447 @jlongster committed Nov 29, 2012
View
@@ -9,7 +9,11 @@ shade: *.js
shade/object.js \
shade/program.js \
shade/resources.js \
+ shade/collision.js \
+ shade/aabb.js \
+ shade/quadtree.js \
shade/scene.js \
+ shade/scenenode.js \
shade/mesh.js \
shade/cube.js \
shade/camera.js \
View
@@ -4,8 +4,9 @@ var remix = require('webremix');
var settings = require('./settings');
var BinaryServer = require('binaryjs').BinaryServer;
var p = require('./static/js/packets');
-var World = require('./world');
+var Scene = require('./scene');
var Entity = require('./static/js/entity');
+var level = require('./static/js/level');
var app = express();
var server = require('http').createServer(app);
@@ -17,7 +18,7 @@ app.configure(function() {
// Sockets
var CLIENTS = [];
-var world = new World();
+var scene = new Scene(255 * 4, 255 * 4);
function broadcast(user, packet) {
for(var i=0, l=CLIENTS.length; i<l; i++) {
@@ -55,7 +56,7 @@ function handlePacket(user, data) {
switch(p.getPacketDesc(packet.type)) {
case p.inputPacket:
packet = p.parsePacket(data, p.inputPacket);
- user.entity.handleServerInput(packet);
+ scene.update(user.entity, packet);
break;
case p.clickPacket:
var entStates = {};
@@ -64,7 +65,7 @@ function handlePacket(user, data) {
interp: packet.entInterps[i] };
}
- var ent = world.getHit(user,
+ var ent = scene.getHit(user,
entStates,
[packet.x, packet.y, packet.z],
[packet.x2, packet.y2, packet.z2]);
@@ -215,7 +216,7 @@ bserver.on('connection', function(client) {
user.name = 'anon' + user.id;
user.entity = new Entity();
user.entity.id = user.name;
- world.addEntity(user.entity);
+ scene.addObject(user.entity);
console.log(user.name + ' connected [' + CLIENTS.length + ']');
@@ -300,7 +301,13 @@ bserver.on('connection', function(client) {
client.on('close', function() {
// Remove itself from the clients array
CLIENTS.splice(CLIENTS.indexOf(user), 1);
- world.removeEntity(user.entity);
+
+ // And from the scene
+ var ent = scene.getObject(user.name);
+ if(ent) {
+ ent._parent.removeObject(ent);
+ }
+
console.log(user.name + ' disconnected [' + CLIENTS.length + ']');
// Broadcast to everyone that he/she left
@@ -322,8 +329,10 @@ bserver.on('connection', function(client) {
});
});
+level.createLevel(scene);
+
// Fire up the server
console.log('Started server on ' + settings.port + '...');
server.listen(settings.port);
-world.start(lookupUser, broadcast);
+scene.start(lookupUser, broadcast);
View
@@ -1,25 +1,16 @@
var shade = require('./static/js/node-shade');
var p = require('./static/js/packets');
-var collision = require('./static/js/collision');
+var collision = require('./static/js/node-shade').Collision;
+var Scene = require('./static/js/node-shade').Scene;
-var World = shade.Obj.extend({
- init: function() {
+module.exports = Scene.extend({
+ init: function(sceneWidth, sceneDepth) {
+ this.parent(sceneWidth, sceneDepth);
this.entities = [];
this.packetBuffer = [];
},
- addEntity: function(ent) {
- this.entities.push(ent);
- },
-
- removeEntity: function(ent) {
- var idx = this.entities.indexOf(ent);
- if(idx !== -1) {
- this.entities.splice(idx, 1);
- }
- },
-
getHit: function(user, entStates, start, end) {
var ents = this.entities;
for(var i=0, l=ents.length; i<l; i++) {
@@ -39,6 +30,13 @@ var World = shade.Obj.extend({
}
},
+ update: function(entity, packet) {
+ entity.snapshot();
+ entity.handleServerInput(packet);
+ this.checkObjCollisions(entity);
+ entity.sendDiff(packet.sequenceId);
+ },
+
start: function(lookupUser, broadcast) {
var _this = this;
setInterval(function() {
@@ -81,6 +79,3 @@ var World = shade.Obj.extend({
}, 100);
}
});
-
-module.exports = World;
-
View
@@ -33,12 +33,14 @@ <h1 id="loading">Loading...</h1>
<script src="js/shade/program.js"></script>
<script src="js/shade/resources.js"></script>
<script src="js/shade/scene.js"></script>
+ <script src="js/shade/scenenode.js"></script>
<script src="js/shade/mesh.js"></script>
<script src="js/shade/cube.js"></script>
<script src="js/shade/line.js"></script>
<script src="js/shade/camera.js"></script>
<script src="js/shade/renderer.js"></script>
<script src="js/shade/mesh-loader.js"></script>
+ <script src="js/shade/collision.js"></script>
<script src="js/shade/quadtree.js"></script>
<script src="js/shade/aabb.js"></script>
@@ -52,7 +54,7 @@ <h1 id="loading">Loading...</h1>
<script src="js/input.js"></script>
<script src="js/simplex-noise.js"></script>
<script src="js/terrain.js"></script>
- <script src="js/collision.js"></script>
+ <script src="js/level.js"></script>
<script src="js/entity.js"></script>
<script src="js/player.js"></script>
<script src="js/stats.min.js"></script>
View
@@ -1,11 +1,12 @@
-(function(SceneNode, Cube, CubeMesh, Terrain, packets, vec3) {
+(function(SceneNode, Cube, CubeMesh, Terrain, Collision, packets, vec3) {
var Entity = SceneNode.extend({
init: function(opts) {
opts = opts || {};
this.parent(opts.pos, opts.rot, opts.scale);
this.speed = opts.speed || 100;
+ this.collisionType = Collision.ACTIVE;
this.sequenceId = 0;
this.pos[1] = Terrain.getHeight(this.pos[0], this.pos[2], true) + 20.0;
@@ -16,10 +17,12 @@
}
if(!opts.scale) {
- this.setScale(10, 10, 10);
+ this.setScale(25, 25, 25);
}
- this.setAABB();
+ var halfScale = vec3.create();
+ vec3.scale(this.scale, .5, halfScale);
+ this.setAABB(vec3.createFrom(0, 0, 0), halfScale);
this.goodPos = vec3.create(this.pos);
this.goodRot = vec3.create(this.rot);
@@ -44,12 +47,7 @@
handleServerInput: function(state) {
// Run the entity's movement on the server-side.
-
var dt = state.dt;
- var diffPos = vec3.create();
- vec3.set(this.pos, diffPos);
- var diffRot = vec3.create();
- vec3.set(this.rot, diffRot);
this.rotateX(state.mouseY * -Math.PI / 6.0 * dt);
this.rotateY(state.mouseX * -Math.PI / 6.0 * dt);
@@ -79,12 +77,24 @@
if(state.down) {
this.moveBack(this.speed * dt);
}
-
+
this.pos[1] = Terrain.getHeight(this.pos[0], this.pos[2], true) + 20.0;
+ },
- vec3.subtract(this.pos, diffPos, diffPos);
- vec3.subtract(this.rot, diffRot, diffRot);
- this.sendInput(diffPos, diffRot, state.sequenceId);
+ snapshot: function() {
+ vec3.set(this.pos, this.historyPos);
+ vec3.set(this.rot, this.historyRot);
+ },
+
+ revert: function(alsoRot) {
+ vec3.set(this.historyPos, this.pos);
+ vec3.set(this.historyRot, this.rot);
+ },
+
+ sendDiff: function(sequenceId) {
+ vec3.subtract(this.pos, this.historyPos, this.historyPos);
+ vec3.subtract(this.rot, this.historyRot, this.historyRot);
+ this.sendInput(this.historyPos, this.historyRot, sequenceId);
},
sendInput: function(diffPos, diffRot, id) {
@@ -142,7 +152,7 @@
// have one more in the history, apply the
// interpolation that the client performs
// (important for fast movement)
-
+
var targetPos = vec3.create();
var targetRot = vec3.create();
vec3.add(this.pos, [state.x, state.y, state.z], targetPos);
@@ -180,7 +190,7 @@
this.setRot(0, 0, 0);
break;
}
-
+
this.interp = 1.0;
this.packetBuffer = [];
this.startPos = this.targetPos = null;
@@ -251,6 +261,7 @@
null,
null,
require('./terrain'),
+ require('./node-shade').Collision,
require('./packets'),
require('./shade/gl-matrix').vec3] :
- [sh.SceneNode, sh.Cube, sh.CubeMesh, Terrain, null, vec3]));
+ [sh.SceneNode, sh.Cube, sh.CubeMesh, Terrain, sh.Collision, null, vec3]));
View
@@ -0,0 +1,25 @@
+(function(Cube) {
+
+ function createLevel(scene) {
+ for(var i=0; i<5; i++) {
+ var cube = new Cube([-20, 0, i*70],
+ [0, 0, 0],
+ [100, 100, 20]);
+ cube.id = 'cube' + i;
+ console.log(cube.AABB.extent);
+ scene.addObject(cube);
+ }
+ }
+
+ if(typeof module !== 'undefined') {
+ module.exports = {
+ createLevel: createLevel
+ };
+ }
+ else {
+ window.createLevel = createLevel;
+ }
+
+}).apply(this, typeof module !== 'undefined' ?
+ [require('./node-shade').Cube] :
+ [sh.Cube]);
View
@@ -15,6 +15,7 @@ var stats;
var w = window.innerWidth;
var h = window.innerHeight;
var renderer;
+var scene;
var gl;
var server;
var player;
@@ -74,25 +75,22 @@ function notify(msg) {
}
function init() {
- renderer = new sh.Renderer(w, h, 255 * 4, 255 * 4);
+ renderer = new sh.Renderer(w, h);
+ scene = new sh.Scene(255 * 4, 255 * 4);
server = new ServerConnection();
+ createLevel(scene);
+
player = new Player();
- renderer.setCamera(new sh.Camera(player));
+ scene.addObject(player);
+ scene.setCamera(new sh.Camera(player));
renderer.perspective(45, w / h, 1.0, 5000.0);
var terrain = new Terrain(null, null, null,
- renderer.sceneWidth,
- renderer.sceneDepth);
+ scene.sceneWidth,
+ scene.sceneDepth);
terrain.create();
- renderer.addObject(terrain);
-
- for(var i=0; i<50; i++) {
- var cube = new sh.Cube([50, 0, i*25],
- [0, 0, 0],
- [100, 100, 10]);
- renderer.addObject(cube);
- }
+ scene.addObject(terrain);
document.getElementById('loading').style.display = 'none';
@@ -109,13 +107,12 @@ function heartbeat() {
var now = Date.now();
var dt = Math.min((now - last) / 1000., .1);
- renderer.update(dt);
- //checkCollisions();
+ scene.update(dt);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
- renderer.render();
+ renderer.render(scene);
last = now;
requestAnimFrame(heartbeat);
Oops, something went wrong.

0 comments on commit db0605f

Please sign in to comment.