Re: Collision Detection
- Posted by prickle Aug 09, 2009
- 1069 views
A simple method might be bounding boxes. Depending on the number of guys you have on the screen, it may be acceptably fast.
If we have the following functions:
--sign of r function sgn(atom r) if r < 0 then return -1 else return 1 end if end function --absolute value of r global function abs(atom r) return sgn(r) * r end function
(BTW, have these been included in more recent versions of Euphoria? If not, may I suggest them?)
We also need the size of your guys:
atom sx, sy, sx2, sy2 sx = length(Guy[1][1]) sy = length(Guy[1]) sx2 = length(Guy2[1][1]) sy2 = length(Guy2[1])
(it still works this way, right?)
We then precalculate the difference between the midpoints of the guys when they are just touching.
atom dx, dy dx = floor((sx + sx2) / 2) dy = floor((sy + sy2) / 2)
Then, where you have:
elsif X and Y = X2 and Y2 then setText(Stat,"Images Hit") end if
We can say:
elsif abs(X - X2) < dx and abs(Y - Y2) < dy then setText(Stat,"Images Hit") end if
Is it obvious what we are doing here? We are finding the difference between the positions of the guys and seeing if it is small enough for them to be touching. Of course this gives one a rectangular bounding box around a guy which may not be ideal for irregular shapes, but it is probably the cheapest, simplest method of collision detection I know.
Hope this helps.
Cheers,
Nick