Pixel-accurate collision detection with Shapes
Hi, I'm still creating my 2D tank battle game, and I have finally come to the point where I need to deal with collisions between the tanks. The tank is simply a sprite with an rectangular shape that moves forward and backward and rotates left and right. I already have wall sprites that are rectangular and don't move and a collision is detected by using the method Shape.intersects(). However, it looks like I need something more accurate for collisions between tanks which can rotate all 360 degrees.
I've read a couple of tutorials on pixel-accurate collision detection and apparently I have to get the pixels from the image and use "masks" to check for collisions. Unfortunately, I use one whole VolatileImage for the whole screen and the sprites simply draw to this image. I'm also not exactly sure if I should use PixelGrabber to get the pixels. I'm pretty clueless in this whole endeavour, especially with the use of bytes and such, so I would be grateful if someone could explain this whole concept to me, and maybe suggest a solution that would be a little less difficult to implement.
Thanks,
Frumple
[1140 byte] By [
Frumple] at [2007-9-27 22:36:32]

I think that you can check for intersection with the "Line2D"
class. Maybe you could use that for each line of the rectangle of the tank and see if any of the lines in the rectangle intersect, even as the tank is rotating.
I'm going to be using rotating images myself, and am going to try and use this method if possible.
>
> I think that you can check for intersection with the
> "Line2D"
> class. Maybe you could use that for each line of the
> rectangle of the tank and see if any of the lines in
> the rectangle intersect, even as the tank is
> rotating.
>
> I'm going to be using rotating images myself, and am
> going to try and use this method if possible.
Thats how my 4k applet does it.
Its not fast, but it makes small code :)
(and if you also do a bounding circle check first, the inefficiency isn't too great.)
Abuse at 2007-7-7 13:22:15 >

im not sure if this will help but if you are doing it in 3d
then find the crossproduct of each side of tank 1 and tank 2
then compare all the points of tank2
with the crossproduct of each of the sides of tank1
by useing the dotproduct if any of the points compare to all the sides
negatively then the point is inside and has collided
> im not sure if this will help but if you are doing it
> in 3d
> then find the crossproduct of each side of tank 1 and
> tank 2
> then compare all the points of tank2
> with the crossproduct of each of the sides of tank1
> by useing the dotproduct if any of the points compare
> to all the sides
> negatively then the point is inside and has collided
thats not necessarily true.
2 rectangles can be in collision, even if neither contains the points of the other.
e.g.
X-X
||
x--x
||
||
x--x
||
X-X
Abuse at 2007-7-7 13:22:15 >

hey jo,try this:public static boolean collidating(Shape a, Shape b) { Area c = new Area(a); Area intersection = c.intersect(new Area(b)); return !intersection.isEmpty();}gtx pkc
pkc2k at 2007-7-7 13:22:15 >
