• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checking if two objects from different arraylists intersect.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I apologize if this is in the wrong board. The moderators can move it if it is.

I have two abstract classes. Rocket and Projectile, which look like this (I have only left the relevant parts)





Then I have subclasses extending those two classes (I will show NormalRocket and MachineGunProjectile) which call the super of bounds like so:





I have a level class which contains two lists, one for rockets and the other for projectiles, and rockets and projectiles are added to the respective lists:



I am using the following code to check for collision between the projectiles and rockets. The problem is, that "HIT" is not printed and I cannot figure out why:



I do not know why this does not work, I have tried 2 different techniques so far and neither work. Can anyone help me solve this problem? I can post more code if it is necessary.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, assuming that you do actually have some entries in both lists, the problem is obviously that the intersects() method is always returning false. So you would need to find out why. Since that's a method of java.awt.Rectangle (am I right?) that would mean that you are always asking it whether two non-intersecting rectangles intersect. You would need to look at the rectangles to see why that is.

Again, that's assuming you are actually passing non-empty lists. You ought to check that as well, a little bit of debugging would work well here.
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah best to just do some debugging, print out the bounds of each rocket and projectile in your lists, so you can manually see if they intersect.
 
Wilson Cash
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the help guys. I have attached a screen shot of the projectile just as it touches the rocket. The numbers in the top left are the coordinates of the rocket (top) and projectile (bottom). (The rocket is a gray square for now)
I am pretty sure that the arraylists contain something because the projectiles and rockets are all rendered from the arraylist and removed when they are not on screen which indicates that the arraylist does contain some entries.
ffh.png
[Thumbnail for ffh.png]
 
Goodbye moon men. Hello tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic