Skip to content

Commit

Permalink
added distance check and collision tests
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Sep 26, 2012
1 parent ca0702e commit 744341d
Show file tree
Hide file tree
Showing 21 changed files with 3,764 additions and 131 deletions.
52 changes: 45 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ httpServer.on('request', httpRequestHandler);
var clientsData = {};
var treatsData = [];

var intersect = require(__dirname + '/intersect.js');

var _ = require('underscore');

// game = {

// };
var game = {};

// helper functions
var getRandomNumberBetween = function(lo, hi) {
return (lo + Math.random() * (hi - lo));
Expand All @@ -59,12 +68,9 @@ var getGameSite = function() {
},
'linda': {
top: 59.44345,
bottom: 59.44265, // 80
left: 24.73060,
right: 24.73220, // 160
lat_center: 59.443283,
lon_center: 24.731743,
diameter: 80
bottom: 59.44265,
left: 24.7306,
right: 24.7322
},
'rävala': {
top: 59.43485,
Expand Down Expand Up @@ -219,14 +225,46 @@ var handlers = {
*/

clientsData[client.id].locations = data.locations;

publishToAll({
command: 'client.locations.updated',
data: {
id: client.id,
locations: data.locations
}
}, client);

if (clientsData[client.id].locations.length > 1) {
var myLastSegment = _.last(clientsData[client.id].locations);
var mySecondLastSegment = clientsData[client.id].locations[clientsData[client.id].locations.length - 2];

var myLastLine = new intersect.Line({ x: myLastsegment[0], y: myLastsegment[1] }, { x: mySecondLastSegment[0], y: mySecondLastSegment[1] });

_.each(clientsData[client.id], function(clientData) {
if (!clientData.locations) return;

if (clientData.locations.length > 1) {

var lastSegment = _.last(clientsData[client.id].locations);
var secondLastSegment = clientsData[client.id].locations[clientsData[client.id].locations.length - 2];

var lastLine = new intersect.Line({ x: lastsegment[0], y: lastsegment[1] }, { x: secondLastSegment[0], y: secondLastSegment[1] });

if (intersect.checkIntersection(lastLine, myLastLine)) {
// BOOM! We have a collision!!!!1
publishToAll({
command: 'clients.collided',
data: {
collidingClient: client.id,
collidedIntoClient: clientData.id
}
}, client);
}

}
});
}

},
'update.level': function(client, data) {
console.log('update.level');
Expand Down
38 changes: 38 additions & 0 deletions intersect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var Point = function(x, y) {
return {
x: x,
y: y
}
};

var Line = function(p1, p2) {
return {
p1: new Point(p1.x, p1.y),
p2: new Point(p2.x, p2.y)
}
};

var checkIntersection = function(line1, line2) {
var returnVal = {};

var d = (line2.p2.y - line2.p1.y) * (line1.p2.x - line1.p1.x) - (line2.p2.x - line2.p1.x) * (line1.p2.y - line1.p1.y);
var n_a = (line2.p2.x - line2.p1.x) * (line1.p1.y - line2.p1.y) - (line2.p2.y - line2.p1.y) * (line1.p1.x - line2.p1.x);
var n_b = (line1.p2.x - line1.p1.x) * (line1.p1.y - line2.p1.y) - (line1.p2.y - line1.p1.y) * (line1.p1.x - line2.p1.x);

if (d == 0) return false;

var ua = (n_a << 14)/d;
var ub = (n_b << 14)/d;

if (ua >=0 && ua <= (1 << 14) && ub >= 0 && ub <= (1 << 14)) {
returnVal.x = line1.p1.x + ((ua * (line1.p2.x - line1.p1.x)) >> 14);
returnVal.y = line1.p1.y + ((ua * (line1.p2.y - line1.p1.y)) >> 14);
return returnVal;
}

return false;
}

exports.Point = Point;
exports.Line = Line;
exports.checkIntersection = checkIntersection;
2 changes: 2 additions & 0 deletions node_modules/intersection/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions node_modules/intersection/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions node_modules/intersection/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/intersection/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 744341d

Please sign in to comment.