From c2c500e6b8534b838e9d58d364d11c73a33b683d Mon Sep 17 00:00:00 2001 From: Simon Giesel Date: Mon, 19 Jun 2017 18:21:58 +0200 Subject: [PATCH] More collision debugging... --- index.html | 64 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 2fad740..3d0dc62 100644 --- a/index.html +++ b/index.html @@ -38,11 +38,11 @@ var _COLOR_GRASS = '#388E3C', ctx = $('canvas')[0].getContext('2d') staticctx = $('canvas')[1].getContext('2d') init() - drawBorder(23, 10, 500, 5) - drawBorder(23, 10, 5, 500) + drawBorder(23+5, 10, 500-10, 5) + drawBorder(23, 10+5, 5, 500-10) drawBorder(23+200, 10+70, 50, 50) - drawBorder(23, 500+10-5, 500, 5) - drawBorder(500+23-5, 10, 5, 500) + drawBorder(23+5, 500+10-5, 500-10, 5) + drawBorder(500+23-5, 10+5, 5, 500-10) drawHole(432, 366) setInterval(animLoop, 10) @@ -101,10 +101,20 @@ var _COLOR_GRASS = '#388E3C', borders.push(new Border(x, y, width, height)) } + function debugBorder(border) { + drawDebugBorder(border) + setTimeout(function(){drawDebugBorderUndo(border)}, 2*1000) + } + function drawDebugBorder(border) { staticctx.fillStyle = 'red' staticctx.fillRect(border.x, border.y, border.width, border.height) } + + function drawDebugBorderUndo(border) { + staticctx.fillStyle = _COLOR_BORDER + staticctx.fillRect(border.x, border.y, border.width, border.height) + } function drawHole(x, y) { staticctx.beginPath() @@ -141,6 +151,7 @@ var _COLOR_GRASS = '#388E3C', } function collide() { + var coll = [false, false, false, false] //left, top, right, bottom borders.forEach(function(el){ // if(bX >= el.x && bX <= el.x+el.width && bY >= el.y && bY <= el.y+el.height){ // drawDebugBorder(el) @@ -162,22 +173,47 @@ var _COLOR_GRASS = '#388E3C', var height = (el.height+bWidth)/2 var crossWidth = width*dy; var crossHeight = height*dx; - var collision = 'none'; if(Math.abs(dx)<=width && Math.abs(dy)<=height){ + debugBorder(el) if(crossWidth -1) + console.log(coll); + // if(coll[0] || coll[2]) + // bVec = [-bVec[0], bVec[1]] + // if(coll[1] || coll[3]) + // bVec = [bVec[0], -bVec[1]] + if(coll[0]){ + console.log('left < 0'); + bVec = [Math.abs(bVec[0]), bVec[1]] + } + if(coll[1]){ + console.log('top < 0'); + bVec = [bVec[0], Math.abs(bVec[1])] + } + if(coll[2]){ + console.log('right > 0'); + bVec = [-Math.abs(bVec[0]), bVec[1]] + } + if(coll[3]){ + console.log('bottom > 0'); + bVec = [bVec[0], -Math.abs(bVec[1])] + } + + + if($.inArray(true, coll) > -1) + console.log(bVec); } /** CLASSES **/