This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Game Development and the fly likes Checking if two objects from different arraylists intersect. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Checking if two objects from different arraylists intersect." Watch "Checking if two objects from different arraylists intersect." New topic
Author

Checking if two objects from different arraylists intersect.

Adam Miszczak
Greenhorn

Joined: Sep 24, 2012
Posts: 24
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

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.
Tyson Lindner
Ranch Hand

Joined: May 16, 2012
Posts: 135
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.
Adam Miszczak
Greenhorn

Joined: Sep 24, 2012
Posts: 24
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]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking if two objects from different arraylists intersect.
 
Similar Threads
How to know the number of items in your Seralization/Deserialization stream?
Using this() keyword in constructors
Can't find bug in my Rectangle class. Think it might be the fault of my setup.
MouseEvent too slow or am I missing something?
Parallel dragging