javascript - collisions detection between my player and a block from a 2d-array -


i'm trying make little game fun of have collision problems.

i have player drawn on canvas , blocks (16 x 16px) drawn on canvas.

but have problem detecting horizontal collisions.

...

my problem comes down this:

my player uses x y coordinates stored as:

var p_x; var p_y; 

these values players bottom left coordinates in pixels.

but blocks in 2d array called:

var g_levelarray; 

and each block 16 x 16 px instance if do:

g_levelarray[3][2] = 1; 

means block drawn @ canvas left: 48px , canvas bottom 32px

...

but have code check if block exists (according player) x , y playercoordinates

function blockexists(x, y) {     var xpos = parseint(x / g_blocksize);     var ypos = parseint(y / g_blocksize);       $("#checkedblock").html("checked block: " + xpos + " " + ypos);       if (g_levelarray[xpos][ypos] != undefined) {         return true;     }     else {         return false;     } } 

but check has errors due fact rounds down number when hit block half down top (as shown on image below) allows player go inside block.

the error have http://userhome.org/test/error.png

i have tried math.round instead of parseint makes problem @ players middle.

so how can write code in right way player doesnt go block?

thx in advance

instead of using parseint , round try using range.

take lower bound(math.floor) , upper bound math.ceil , check if block exists in range checking these values in array.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -