Computer Science Canada Mass 2D hit detection |
| Author: | keyboardwalker [ Sun Jan 27, 2013 6:53 pm ] | ||
| Post subject: | Mass 2D hit detection | ||
Okay so I am trying to find the best way to do 2D hit detection very quickly. I currently have two methods one is with the the array of dots and checks to see if the movement location is okay by checking to see if any of the dots are in the array are in that X and Y location The other with colour. The issue with using the array locations is that you are doing a check for every single dot to see if any of the other dots are in that location. So far the best method I have found for hit detection with dots is actually colour which can be unreliable in some cases but it works. So what I was wondering is there a faster way or a just as fast way but more reliable? This is what I have done for colour hit detection:
|
|||
| Author: | Insectoid [ Sun Jan 27, 2013 8:44 pm ] |
| Post subject: | RE:Mass 2D hit detection |
Using an array is your best bet. You don't have to check everything though. If your character is all the way on the left, he can't possibly hit anything on the right, so why bother checking against objects on the right? |
|
| Author: | keyboardwalker [ Sun Jan 27, 2013 9:47 pm ] |
| Post subject: | Re: RE:Mass 2D hit detection |
Insectoid @ Sun Jan 27, 2013 8:44 pm wrote: Using an array is your best bet. You don't have to check everything though. If your character is all the way on the left, he can't possibly hit anything on the right, so why bother checking against objects on the right?
Here is my hit detection with arrays but it can't compare to hit detection with colour. How can I optimize it so that it is faster? I don't know how to apply what you just said. Try running the two and see the difference I really want the fastest way. const NUMDOT := 200 var DotX : array 1 .. NUMDOT of nat var DotY : array 1 .. NUMDOT of nat var MouseX, MouseY, MouseButton : int var Count : nat var Hit : boolean var Temp : int for I : 1 .. NUMDOT randint (Temp, 1, 400) DotY (I) := Temp randint (Temp, 1, 400) DotX (I) := Temp end for setscreen ("offscreenonly") loop mousewhere (MouseX, MouseY, MouseButton) if MouseButton = 1 then for i : 1 .. NUMDOT if DotX (i) > MouseX then Count := 1 Hit := false loop if DotX (i) - 1 = DotX (Count) and DotY (i) = DotY (Count) then Hit := true end if Count := Count + 1 exit when Count = NUMDOT or Hit end loop if not Hit then DotX (i) := DotX (i) - 1 end if elsif DotX (i) < MouseX then Count := 1 Hit := false loop if DotX (i) + 1 = DotX (Count) and DotY (i) = DotY (Count) then Hit := true end if Count := Count + 1 exit when Count = NUMDOT or Hit end loop if not Hit then DotX (i) := DotX (i) + 1 end if end if if DotY (i) > MouseY then Count := 1 Hit := false loop if DotY (i) - 1 = DotY (Count) and DotX (i) = DotX (Count) then Hit := true end if Count := Count + 1 exit when Count = NUMDOT or Hit end loop if not Hit then DotY (i) := DotY (i) - 1 end if elsif DotY (i) < MouseY then Count := 1 Hit := false loop if DotY (i) + 1 = DotY (Count) and DotX (i) = DotX (Count) then Hit := true end if Count := Count + 1 exit when Count = NUMDOT or Hit end loop if not Hit then DotY (i) := DotY (i) + 1 end if end if end for end if cls for I : 1 .. NUMDOT drawdot (DotX (I), DotY (I), black) end for View.Update exit when hasch end loop |
|
| Author: | Insectoid [ Mon Jan 28, 2013 1:27 pm ] |
| Post subject: | RE:Mass 2D hit detection |
You could try breaking the screen up into a grid of smaller squares. Figure out what squares your objects are in, then figure out which square(s) your character is in, and only check for collisions with objects in the same square(s) as your character. |
|