From 9f9239aa8cd7b45148941e39af8669ac8330834c Mon Sep 17 00:00:00 2001 From: dsc Date: Thu, 4 Nov 2010 16:17:39 -0700 Subject: [PATCH] Adds vector and math utils for bullet collisions --- src/portal/layer.js | 6 +- src/portal/math/line.js | 39 +++++++++++++++++++ src/portal/math/math.js | 9 ++++ src/portal/math/vec.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++ src/portal/shape.js | 57 ++++++++++++++++------------ src/portal/util/loc.js | 23 +++++++++-- src/tanks/ui.js | 1 + src/tanks/util/grid.js | 13 ++---- test/math/index.php | 62 +++++++++++++++++++++++++++++++ test/math/math.test.js | 79 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 344 insertions(+), 39 deletions(-) create mode 100644 src/portal/math/line.js create mode 100644 src/portal/math/math.js create mode 100644 src/portal/math/vec.js create mode 100644 test/math/index.php create mode 100644 test/math/math.test.js 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; +} + -- 1.7.0.4