Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collision detection

Status
Not open for further replies.

stormbind

Technical User
Joined
Mar 6, 2003
Messages
1,165
Location
GB
How best to perform collision detection in javascipt?

This is something that I have often wondered, but now I need it for a sites christmas decorations. What I need is a means of detecting when the sprite is over a static image.

The pixel positions could be measured and compared but that might be slow and unsuitable for web browsing :(

I'm thinking maybe I need a 2-bit collision map, for simpler detection without re-calculating the position of every element on the page.

Perhaps this is already done by the browser? Netscape 4.x document.layers is an example of a browser's reserved array of specific objects.

Is there anything similar in MSIE? Hopefully a built in array with all the IMG on the page?

Please share your knowledge or experiences of collisions :)

Thank you kindly! :)

----------
I'm willing to trade custom scripts for... [see profile]
 
OK. I'll talk to myself then...

It's easy to collect the IMG tags in an array, and extracting their position and sizes... that leaves me with something like this:

img[0], img[1], img[2] (objects)

x[c] 100, 200, 120 (left)
y[c] 100, 200, 120 (top)
w[c] 100, 100, 100 (width)
h[c] 20, 20, 20 (height)

How do I create a map from those values? :(

----------
I'm willing to trade custom scripts for... [see profile]
 
Aren't you going about it the wrong way - shouldn't you been detecting what element the sprite is over?

As in if it moves over an table - nothing.
If it moves over an image the world explodes.

Not exactly sure how this would fly - it's easy once you can figure out an event handler for the sprite, I wonder if onMouseOver etc just work for the cursor ? Let me know when you find out.

Just a thought.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top