Transparent image mouse collision

I am currently creating a graphical applet which is structured in following way:

publicclass Clientextends Appletimplements Runnable, MouseListener...

{

//Variable decalraions excluded

//init() excluded

publicvoid run()

{

//main loop here

}

//update() excluded

publicvoid paint(Graphics g)

{

//ALL GRAPHICS PAINTED HERE (i.e. no Swing components)

}

//mouselistener/mouseclicked etc. excluded

}

I need to implement an algorithm to determine if the mouse is over the part of an image that is NOT transparent (i.e i am using gif's, some with transparency, and it is crucial that i detect wether the mouse is over the shape within the image, and not any transparent pixels).

I have been attempting to create methods that read the pixels of an image, and using getAlpha() to determine wether the mouse point is over a non-transparent pixel, but my attempts have been unsuccessful.

If anyone knows of any good methods of doing this, it would be much appreciated. I have been researching the topic for quite a while and im stumped. I did find one tutorial at http://www.permadi.com/tutorial/javaGetImagePixels/index.html which i thought was quite useful, but when i tried to alter this demonstration to suit my situation, it couldnt do what i wanted it to (Note: this example grabs the rgb values, not the alpha values)

cheers.

Message was edited by:

jazza_guy

[2100 byte] By [jazza_guya] at [2007-11-27 9:20:51]
# 1
I know little to nothing about Java2D - but there is an entire forum dedicated to it. I would suggest posting in that forum, providing a link to this thread.
Navy_Codera at 2007-7-12 22:14:18 > top of Java-index,Java Essentials,Java Programming...
# 2
cheers for the advice
jazza_guya at 2007-7-12 22:14:18 > top of Java-index,Java Essentials,Java Programming...
# 3
> cheers for the adviceSure thing. Hope you find a solution to your problem!
Navy_Codera at 2007-7-12 22:14:18 > top of Java-index,Java Essentials,Java Programming...
# 4

I have pondered this issue alot and come to the conclusion that pixel level collision detection should be done in hardware. :)

I was able to speed it up by making a 1 bit plane 32 bit mask of the alpha values and ORing them with the target's mask. This at least checks 32 bits at a time but is really still too slow for anything appreciable. ie: given 20 images of

100 X 100 pixels thats 200,000 compares.With a 32 bit mask compare its still 6250. Too many as far as I am concerned

You could further switch the source trajectory octant and start your image parse from the edge that faces that direction, and this would cut it in half agin, but no matter what you do you are faced with the exponential nature of a gazzilion checks.

My "theorized" solution to this. (I haven't coded it yet)

After much thought I have come to the conclusion that the best way to get

the results you want is to make a polygon array for every object you want to check and use the Polygon.contains() method. Thus each Object needs an associated Polygon defined for it.

If you want a "Hole" for example a spaceship flying through a doughnut,

then make another list of polygons; "your hole list" and in the event of a collision check it to see if you are in a hole.

I AM going to make a Polygon Editor/Generator for doing this quickly but it

is about 20 down my doto list ATM.

An example of doing this is the isColliding method of the my HyperView asteorids port. (I would likely not spin Objects all the time in the manner it does) https://hyperview.dev.java.net/source/browse/hyperview/www/HyperView299/Asteroids.java?rev=1.10&view=markup

In summary,

my solution to this is Make a polygon array for each Image and use the

Polgon.contains() method to determine collisions.

Hope this helps

(T)

tswaina at 2007-7-12 22:14:18 > top of Java-index,Java Essentials,Java Programming...
# 5

If your background is a BufferedImage, then you should be able to use the getAlpha(int) method of it's ColorModel to get the transparency of the pixel your mouse is currently over. When ever you get a mouse moved event, get the new location and make the comparison. That won't tell you if you passed over a transparent area in between mouse motion events though. For that you could interpolate each pixel between the current location and the previous location and compare test each of those.

hunter9000a at 2007-7-12 22:14:18 > top of Java-index,Java Essentials,Java Programming...