Im an old coder who came back so to speak, learning Java I know what I want but I cannot find anyone asking it anywhere so here goes...

Im writing a basic tank game to code for practice but as is always the case my personal ambitions out pace my abilities.

Ive been reading up on the various methods used for collision detection using rectangles and point testing around the sprite for collisions but it all seems a little hap hazard and processor heavy to me.

I was thinking maybe a method would work by which I could logically compare two sprites -

So maybe I have a tank(me) and I come to a tile which is meant to be wall. OK so I can compare the square from an array and look up what type of tile is there and prevent movement in that direction, but again Im not happy with this because its not accurate enough. I mean what happens if I want that wall shootable, where it goes through several stages of destruction before its turned into a fully traversable square, it may have a few bits un-destroyed and I want to be able to detect even this.

So how about if I created a tank mask, black and white and grab the square the tank is entering and logically AND it with the tank sprite, creating zero unless a pixel appears in both locations, in which case it hit something.

Is this clear enough?

Do you think this would work and which coding direction do you recommend, Im still new to the syntax but as you can see I know what I want....I think!

  • \$\begingroup\$ "Do you think this would work" logically, there's nothing to prevent it. Whether coding it has the simplicity and performance you're looking for is something you can evaluate for yourself by writing it. What do you need from us? We're likely to just recommend the same methods you've already found documented elsewhere. \$\endgroup\$
    – DMGregory
    Aug 30, 2019 at 4:30
  • \$\begingroup\$ This is in fact how it used to be done! I remember first reading about it in the book Sex, Lies, and Video Games. \$\endgroup\$ Aug 30, 2019 at 5:00
  • \$\begingroup\$ The way you described will be more processor heavy by orders of magnitude compared to the rectangle approach, so first decide if you want speed (you really don't, it's 2019) or precision \$\endgroup\$
    – Bálint
    Aug 30, 2019 at 8:08
  • \$\begingroup\$ The reason for asking is to see if \$\endgroup\$
    – JackDaw
    Aug 30, 2019 at 16:38
  • \$\begingroup\$ there might be something already - as I said my Java knowledge is buiding up but still lacking and I didn't want to go 'reinventing the wheel' if there was something someone knows about already - \$\endgroup\$
    – JackDaw
    Aug 30, 2019 at 16:39

1 Answer 1


I would not do this the way you describe. How many pixels will you test in the event there is no collision (the probable case)? All of them! Instead define a polygonal outline for your tank and whichever objects exist on the map. Then, you can test each point of the tank polygon against each edge of the obstacle polygon and determine if there is a collision. Look up point/plane collision testing. This will be tremendously faster than your proposed method. Good luck!

  • \$\begingroup\$ Devil's advocate here. Perhaps JackDaw's approach could be massively accelerated by OpenGL? The GPU is dumping data into a color buffer for each poly x pixel, maybe it could fill a stencil buffer as well at zero cost? Then all you need is a primitive that computes the maximum of a stencil buffer. \$\endgroup\$ Oct 5, 2019 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .