Skip to content

Commit

Permalink
Added player/ball - wall collision
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenCow committed Apr 20, 2013
1 parent ce6e41a commit 3a889be
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 34 deletions.
28 changes: 28 additions & 0 deletions web/linesegment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(function() {

var m = function(Vector) {
function LineSegment(startx, starty, endx, endy) {
this.start = new Vector(startx, starty);
this.end = new Vector(endx, endy);
this.normal = new Vector(0,0);
this.recalculate();
}
var p = LineSegment.prototype;
p.recalculate = function() {
var n = this.normal;
n.setV(this.end);
n.substractV(this.start);
this.length = n.length();
n.normalize();
n.normalLeft();
};
return LineSegment;
};

try {
module.exports = m(require('./vector'));
} catch(e) {
define(['./vector'],m);
}

})();
12 changes: 9 additions & 3 deletions web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,22 +317,28 @@ define(['platform','game','vector','staticcollidable','linesegment','editor','re
return Math.round(f*100)/100;
}

for(var i=1-1;i>=0;i--) {
var drawHistory = Math.min(100,timeframes.length);
for(var i=drawHistory-1;i>=0;i--) {
var timeframe = timeframes[i];
g.context.globalAlpha = 1-(i/timeframes.length);
g.context.globalAlpha = 1-(i/drawHistory);
timeframe.gamestate.players.forEach(function(player) {
var x = player.x;
var y = 600-player.y;
g.fillStyle('blue');
g.fillCircle(x,y,20);
g.fillStyle('white');
g.fillText('Player:'+round(player.x)+','+round(player.y),x,y);
//g.fillText('Player:'+round(player.x)+','+round(player.y),x,y);
});

g.fillStyle('white');
g.fillCircle(timeframe.gamestate.ball.x,600-timeframe.gamestate.ball.y,10);
}

game.getLevel().lines.forEach(function(line) {
g.strokeStyle('white');
g.strokeLine(line.start.x,line.start.y,line.end.x,line.end.y);
});


// Draw HUD
g.fillStyle('white');
Expand Down
14 changes: 2 additions & 12 deletions web/vector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
(function() {

var m = function() {
define(function() {
function Vector(x,y) {
this.x = x;
this.y = y;
Expand Down Expand Up @@ -126,12 +124,4 @@ var m = function() {
Vector.yaxis = new Vector(0,1);

return Vector;
};

try {
module.exports = m();
} catch(e) {
define(m);
}

})();
});
145 changes: 126 additions & 19 deletions web/wsball-game.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
(function() {
var Vector;
try {
module.exports = newGame;
Vector = require('./vector.js');
} catch(e) {
define(['vector.js'],function(vector) {
Vector = vector;
return newGame;
});
}

function newGame() {

define(['./vector','./linesegment'],function(Vector,LineSegment) {
return function newGame() {
function assert(b) { if(!b) {
debugger;
throw new Error('Assertion failed');
Expand Down Expand Up @@ -48,6 +36,41 @@ function newGame() {
}
}];


function createBox(points) {
var lineSegments = [];
var prevPoint = points[points.length-1];
points.forEach(function(point) {
lineSegments.push(new LineSegment(prevPoint.x, prevPoint.y, point.x, point.y));
prevPoint = point;
});
return lineSegments;
}
var level = {
lines: createBox([
new Vector(50,50),

new Vector(50,240),
new Vector(10,240),
new Vector(10,360),
new Vector(50,360),

new Vector(50,550),
new Vector(750,550),

new Vector(750,360),
new Vector(790,360),
new Vector(790,240),
new Vector(750,240),

new Vector(750,50)
]),
teams: [
{ goals: [new LineSegment(40,240, 40,360)] },
{ goals: [new LineSegment(760,360, 760,240)] }
]
};

function getLastTimeFrame() {
return timeframes[0];
}
Expand Down Expand Up @@ -150,6 +173,7 @@ function newGame() {
});

var t = new Vector();
var t2 = new Vector();
var radius = 20.0;
var bounciness = 0;
var mass = 1;
Expand All @@ -158,9 +182,9 @@ function newGame() {
ng.players.forEach(function(pa) {
ng.players.forEach(function(pb) {
if (pa === pb) { return; }
handleCollision(pa,10,20,pb,10,20);
handleCircleCollision(pa,10,20,pb,10,20);
});
if (handleCollision(ng.ball,5,10,pa,10,20) && pa.keys['x']) {
if (handleCircleCollision(ng.ball,5,10,pa,10,20) && pa.keys['x']) {
t.set(ng.ball.x,ng.ball.y);
t.substract(pa.x,pa.y);
t.normalizeOrZero();
Expand All @@ -172,7 +196,7 @@ function newGame() {
delete pa.keys['x'];
}
});
function handleCollision(pa,massa,radiusa,pb,massb,radiusb) {
function handleCircleCollision(pa,massa,radiusa,pb,massb,radiusb) {
t.set(pa.x,pa.y);
t.substract(pb.x,pb.y);
var l = t.length();
Expand All @@ -193,6 +217,88 @@ function newGame() {
return false;
}

ng.players.forEach(function(p) {
handleLineCollision(p,20,0, level.lines);
});
handleLineCollision(ng.ball,10,0.5, level.lines);

function getCollisions(o,radius, lineSegments, collisions) {
for(var i=0;i<lineSegments.length;i++) {
var lineSegment = lineSegments[i];
if (lineSegment.normal.dot(o.vx,o.vy) > 0) {
continue;
}
t.setV(lineSegment.normal);
t.normalRight();
var l = lineSegment.start.distanceToV(lineSegment.end);
t2.set(o.x,o.y);
t2.substractV(lineSegment.start);
var offY = lineSegment.normal.dotV(t2)-radius;
var offX = t.dotV(t2);
if (offY < -radius*2) {
continue;
} else if (offY < 0) {
var d;
if (offX > 0 && offX < l) {
offY*=-1;
collisions.push({
lineSegment: lineSegment,
offset:offY
});
} else if (offX < 0 && offX > -radius) {
d = lineSegment.start.distanceTo(o.x,o.y);
if (d < radius) {
t.set(o.x,o.y);
t.substractV(lineSegment.start);
t.normalize();
collisions.push({
lineSegment: lineSegment,
offset:radius-d
});
}
} else if (offX > l && offX < l+radius) {
d = lineSegment.end.distanceTo(o.x,o.y);
if (d < radius) {
t.set(o.x,o.y);
t.substractV(lineSegment.end);
t.normalize();
collisions.push({
lineSegment: lineSegment,
offset:radius-d
});
}
}
} else {
continue;
}
}
}

function handleLineCollision(o,radius,bounciness, collisionlines) {
for(var iteration=0;iteration<5;iteration++) {
var collisions = [];

getCollisions(o,radius,collisionlines,collisions);
if (collisions.length > 0) {
collisions.sort(function(a,b) {
return b.offset-a.offset;
});
var c = collisions[0];
//offset-=1;
t.set(o.x,o.y);
t.add(c.lineSegment.normal.x*c.offset,c.lineSegment.normal.y*c.offset);
o.x = t.x; o.y = t.y;
var vc = c.lineSegment.normal.dot(o.vx,o.vy);

t.set(o.vx,o.vy);
t.substract((1+bounciness)*c.lineSegment.normal.x*vc, (1+bounciness)*c.lineSegment.normal.y*vc);
o.vx = t.x; o.vy = t.y;
} else {
break;
}
}
}

return ng;
}

Expand Down Expand Up @@ -251,10 +357,11 @@ function newGame() {
updateGame: updateGame,
insertEvent: insertEvent,
resetToTimeFrames: resetToTimeFrames,
getLevel: function() { return level; },
isFramePrehistoric: function(frame) { return frame < timeframes[timeframes.length-1].gamestate.frame; },
getCurrentGameState: function() { return timeframes[0].gamestate; },
getCurrentFrame: function() { return timeframes[0].gamestate.frame; }
};
}

})();
}
});

0 comments on commit 3a889be

Please sign in to comment.