Skip to content

Commit

Permalink
Old code I forgot to commit. Might still be unstable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias VP committed Aug 19, 2014
1 parent 64c32d0 commit a211902
Show file tree
Hide file tree
Showing 27 changed files with 2,070 additions and 1,279 deletions.
37 changes: 17 additions & 20 deletions CollisionDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
The logic has been ported from the Quake 1 engine
**/

window.cs = window.cs || { };

cs.CollisionDetection = (function() {

define(["lib/gl-matrix", "GameInfo", "MapParser"], function(glMatrix, GameInfo, MapParser) {
var samePosition = function(vStart, vEnd) {
return vStart[0] === vEnd[0] &&
vStart[1] === vEnd[1] &&
vStart[2] === vEnd[2];
}

var trace = function(vStart, vEnd, shouldSlide) {
var mapData = cs.map.mapData;
var mapData = GameInfo.map.mapData;
//No need to do anything if no movement is required
if(samePosition(vStart, vEnd)) {
return vEnd;
Expand All @@ -38,13 +35,13 @@ cs.CollisionDetection = (function() {
//formula:
//vNewPosition = vStart + (vEnd - vStart)*ratio
var vDelta = [];
vec3.sub(vDelta, vEnd, vStart);
glMatrix.vec3.sub(vDelta, vEnd, vStart);
var vNewPosition = [];
vec3.scaleAndAdd(vNewPosition, vStart, vDelta, traceObj.ratio);
glMatrix.vec3.scaleAndAdd(vNewPosition, vStart, vDelta, traceObj.ratio);

//Calculate how much we still need to move
var vMove = [];
vec3.sub(vMove, vEnd, vNewPosition);
glMatrix.vec3.sub(vMove, vEnd, vNewPosition);

if(!shouldSlide) {
return vNewPosition;
Expand All @@ -53,7 +50,7 @@ cs.CollisionDetection = (function() {
//We now calculate how much the player should "slide"
//i.e when hitting a wall we don't completely stop, but rather slide
//along the wall
var distance = vec3.dot(vMove, traceObj.plane.vNormal);
var distance = glMatrix.vec3.dot(vMove, traceObj.plane.vNormal);
var vEndPosition = [
vEnd[0] - traceObj.plane.vNormal[0] * distance,
vEnd[1] - traceObj.plane.vNormal[1] * distance,
Expand All @@ -65,9 +62,9 @@ cs.CollisionDetection = (function() {
};

var recursiveHullCheck = function(iNode, p1f, p2f, p1, p2, traceObj) {
var mapData = cs.map.mapData;
var mapData = GameInfo.map.mapData;
if(iNode < 0) {
if(iNode != cs.MapParser.constants.CONTENTS_SOLID) {
if(iNode != MapParser.constants.CONTENTS_SOLID) {
traceObj.allSolid = false;
}
return true;
Expand All @@ -91,8 +88,8 @@ cs.CollisionDetection = (function() {
}
else {
//Not perpendicular to any axis
t1 = vec3.dot(vNormal, p1) - plane.distance;
t2 = vec3.dot(vNormal, p2) - plane.distance;
t1 = glMatrix.vec3.dot(vNormal, p1) - plane.distance;
t2 = glMatrix.vec3.dot(vNormal, p2) - plane.distance;
}

if(t1 >= 0 && t2 >= 0) {
Expand Down Expand Up @@ -155,7 +152,7 @@ cs.CollisionDetection = (function() {
}
else {
var negvNormal = [];
vec3.negate(negvNormal, vNormal);
glMatrix.vec3.negate(negvNormal, vNormal);
traceObj.plane.vNormal = negvNormal;
traceObj.plane.distance = -plane.distance;
}
Expand All @@ -172,8 +169,8 @@ cs.CollisionDetection = (function() {
//We could go further, calculate new ratio of how far we went
mid = p1f + (p2f - p1f)*frac;
var vDelta = [];
vec3.sub(vDelta, p2, p1);
vec3.scaleAndAdd(vMid, p1, vDelta, frac);
glMatrix.vec3.sub(vDelta, p2, p1);
glMatrix.vec3.scaleAndAdd(vMid, p1, vDelta, frac);
}

//Store how far we were able to go
Expand All @@ -183,7 +180,7 @@ cs.CollisionDetection = (function() {

//Check whether the point p is a solid or not
var hullPointContentIsSolid = function(iNode, p) {
var mapData = cs.map.mapData;
var mapData = GameInfo.map.mapData;

while(iNode >= 0) {
var node = mapData.clipNodes[iNode];
Expand All @@ -194,7 +191,7 @@ cs.CollisionDetection = (function() {
mapData.planes.normals[3*node.iPlane + 2]
];

var d = vec3.dot(vNormal, p) - plane.distance;
var d = glMatrix.vec3.dot(vNormal, p) - plane.distance;

if(d < 0) {
iNode = node.iChildren[1];
Expand All @@ -204,7 +201,7 @@ cs.CollisionDetection = (function() {
}
}

return iNode == cs.MapParser.constants.CONTENTS_SOLID;
return iNode == MapParser.constants.CONTENTS_SOLID;
};

return {
Expand All @@ -220,4 +217,4 @@ cs.CollisionDetection = (function() {
return Math.abs(t[2] - z) < 0.000001;
}
};
})();
});
9 changes: 9 additions & 0 deletions GameInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["lib/gl-matrix"], function(glMatrix) {
return {
gl: null,
player: null,
map: null,
mvMatrix: glMatrix.mat4.create(),
pMatrix: glMatrix.mat4.create()
}
});
25 changes: 12 additions & 13 deletions Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
class that performs both actions.
**/

window.cs = window.cs || { };

cs.Map = function(gl, data) {
var mapData = cs.MapParser.parse(data);
var mapRender = new cs.MapRender(gl, mapData);

this.mapData = mapData;
/**
Render the map as seen from the "pos" position
**/
this.render = function(pos) {
return mapRender.render(pos);
define(["MapParser", "MapRender"], function(MapParser, MapRender) {
return function(gl, data) {
var mapData = MapParser.parse(data);
var mapRender = new MapRender(gl, mapData);
this.mapData = mapData;
/**
Render the map as seen from the "pos" position
**/
this.render = function(pos) {
return mapRender.render(pos);
};
};
}
});
8 changes: 3 additions & 5 deletions MapParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
A few changes has been made to the JSON layout for performance reasons
**/

window.cs = window.cs || { };

/**
Provides easy-to-use functions for reading binary data
**/

cs.MapParser = (function () {
define(["util/DataReader"], function(DataReader) {
var constants = {
BSP_VERSION: 30,

Expand Down Expand Up @@ -683,5 +681,5 @@ cs.MapParser = (function () {
visibility: visibility
};
}
}
}());
};
});
Loading

0 comments on commit a211902

Please sign in to comment.