Javascript: Collision detection

Posted by jack moore on Stack Overflow See other posts from Stack Overflow or by jack moore
Published on 2010-03-13T22:57:39Z Indexed on 2010/03/13 23:05 UTC
Read the original article Hit count: 471

Filed under:
|

Hello,

could someone please help me to understand how collision detection works in JS? I can't use jQuery or gameQuery - already using prototype - so, I'm looking for something very simple. Not asking for complete solution, just point me to the right direction.

Let's say there's:

<div id="ball"></div>
and 
<div id="someobject0"></div>

Now the ball is moving (any direction). "Someobject"(0-X) is already pre-defined and there's 20-60 of them randomly positioned like this:

#someobject {position: absolute; top: RNDpx; left: RNDpx;} 

I can create an array with "someobject(X)" positions and test collision while the "ball" is moving... Something like:

for(var c=0; c<objposArray.length; c++){
........ and code to check ball's current position vs all objects one by one....
}

But I guess this would be a "noob" solution and it looks pretty slow. Is there anything better?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about prototype