aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how to check for collision Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to check for collision" Watch "how to check for collision" New topic
Author

how to check for collision

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
Hello All,
i have few images, moving inside a frame, i want to check collision between those images.
i tried it, i just prints the x and y coordinates of my images, but than what do i do i don't know.
do i need to do something like:-
create rectangles outside the images and check collision in between those rectangles, or something else??
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
these two threads deal with overlapping panels when dragged, but the principle
is basically the same if moving images in a JLabel

http://www.coderanch.com/t/582359/GUI/java/overlapping-jpanels
and
http://www.coderanch.com/t/573169/GUI/java/questions-JPanel

the second link is a bit long, but scattered throughout is some worthwhile reading relevant to your question
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
thank you michael for replying.
thanks for the links, but i am not getting solution of my problem.
can you please please tell me what basic steps i need to follow, to check the collision.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> can you please please tell me what basic steps i need to follow, to check the collision.

the code in those links is almost identical to your problem - it's all to do with setLocation(),
and checking whether the new location collides/overlaps.

in the first link, my second last post is an example of dragging/checking overlap.
modify the code from:
1) dragging to animation setting-the-location
2) taking into consideration each label's size, instead of the 25x25 of the panels

and that would be one of the ways to solve your problem of rectangles (labels with images).
if you want the actual fish shapes colliding, then that's a whole lot more complex.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
okay, i am doing.
thank you michael..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to check for collision