From: dsc Date: Thu, 4 Nov 2010 23:17:39 +0000 (-0700) Subject: Adds vector and math utils for bullet collisions X-Git-Url: http://git.lttlst.com:3516/?a=commitdiff_plain;h=9f9239aa8cd7b45148941e39af8669ac8330834c;p=tanks.git Adds vector and math utils for bullet collisions --- diff --git a/src/portal/layer.js b/src/portal/layer.js index 62e2da1..f88500a 100644 --- a/src/portal/layer.js +++ b/src/portal/layer.js @@ -131,11 +131,11 @@ Layer = new Y.Class('Layer', { var nb = this.negBleed.x , v = this.canvasWidth = w + nb + this.posBleed.x; - this.canvas[0].width = v; this.canvas.css({ 'width' : v+'px', 'margin-left' : (-nb)+'px' }); + this.canvas[0].width = v; return this; }, @@ -149,11 +149,11 @@ Layer = new Y.Class('Layer', { var nb = this.negBleed.y , v = this.canvasHeight = h + nb + this.posBleed.y; - this.canvas[0].height = v; this.canvas.css({ 'height' : v+'px', 'margin-top' : (-nb)+'px' }); + this.canvas[0].height = v; return this; }, @@ -438,7 +438,7 @@ function makeDelegate(name, dirties, prop){ $(function(){ $(' + + + +
+ +
+\n"; +} + +foreach ($scripts as $s) js($s); +?> +
+ + \ No newline at end of file diff --git a/test/math/math.test.js b/test/math/math.test.js new file mode 100644 index 0000000..ee96a8b --- /dev/null +++ b/test/math/math.test.js @@ -0,0 +1,79 @@ +PPU = 25; +PX = 2.5/PPU; + +$(function(){ + +plot = $('#plot'); + +w = plot.width(); w2 = w/2; +h = plot.height(); h2 = h/2; +W = w/PPU; W2 = W/2; +H = h/PPU; H2 = H/2; + +grid = new Grid( W, H, PPU ).appendTo(plot); +grid.lineWidth = 1.0; +grid.strokeStyle = '#E0E0E0'; //'#EEEEEE'; // +grid.draw(); + +P = new Layer() + .width(w).height(h) + .appendTo(grid); + +ctx = P.ctx; +ctx.translate(w2, h2); +ctx.scale(PPU, PPU); + +// Draw axes +drawLine(-W2,0, W2,0, '#CCCCCC'); +drawLine(0,-H2, 0,H2, '#CCCCCC'); + +testLine(1,2, 4,7, 'rgba(231,48,117, 0.5)', 'rgba(69,150,255, 1)'); +// testLine(-4,-2, 7,13, 'rgba(131,187,50, 0.75)', 'rgba(69,150,255, 1)'); + +}); + +function testLine(x1,y1, x2,y2, color, pcolor){ + var t,p, line = new math.Line(x1,y1, x2,y2, 10); + drawLine(-W/2, line.calcY(-W2), line.calcX(H2), H2, color); + drawPoint(line.x1, line.y1); + drawPoint(line.x2, line.y2); + + drawPoint(0, 0, pcolor); + + t = 0; + p = line.pcalc(t); + drawPoint(p.x, p.y, pcolor); + + t = 1; + p = line.pcalc(t); + drawPoint(p.x, p.y, pcolor); + + t = -1; + p = line.pcalc(t); + drawPoint(p.x, p.y, pcolor); + + return line; +} + + +function drawLine(x1,y1, x2,y2, color, width){ + ctx.beginPath(); + ctx.lineWidth = width || PX; + ctx.strokeStyle = color || '#000000'; + ctx.moveTo(x1, -y1); + ctx.lineTo(x2, -y2); + ctx.stroke(); + ctx.closePath(); +} + +function drawPoint(x,y, color, r){ + r = r || 3.75; + var c = new Circle(r) + .position(w2-r + x*PPU, h2-r - y*PPU) + .attr({ 'fillStyle': color || 'rgba(0,0,0,0.5)' }) + .appendTo(P) + .draw(); + c.layer.attr('title', '('+x+','+y+')'); + return c; +} +