See working code here. For your future reference:
- This algorithm is called DDA (digital differential analyser). The canonical implementation requires cell co-ordinates to be a floating point value with each increment of 1 denoting the width of a full cell. Thus in
x
, the start of the first cell's edge is 0, the middle of the second cell is 1.5, 2/3s through the third cell is 2.67, etc. The first thing you needed to do was to divide yourmouseVector
values byCELLSIZE
and work only in this form, never inCELLSIZE
d form. - use canonical / industry standard naming e.g.
raycastCollision
->rayMarch
. - simplify your naming:
drawCellOutline
->outlineCell
(matches yourfillCell
) - do not repeat yourself in function call parameters which you can include within the function itself e.g.
fillCell(x * CELLSIZE, y * CELLSIZE)
->fillCell(x, y)
; since they are grid-aligned they must all be ofCELLSIZE
, not so? - if you are learning from example code as in this case, do not change variable names within the function(s). Rather copy out your values from your custom vector
mouseVector
intox0, y0, x1, y1
as per the original as this helps you avoid at least one class of mistakes / errors. - do not expect to use the advanced / optimised version of an algorithm until you have studied and understood the basic version.