Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Collision Detection difficulties

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Collision Detection difficulties

    I am attempting to write a 2D game with collision detection for two moving objects (one player controlled and the other in one direction) using Rectangle2D's intersects() method. When the non-player controlled piece is not moving the method works fine, however when I implement the move() method the intersects() method seems to fail. Is there a known problem with this method and 2 moving objects?

    I am trying not to post the code because it is recycled from a tetris game I made in my first Java class (it is really, really messy >.<)

    If you need more info let me know, any help would be greatly appreciated,
    Thanks in advance!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Collision Detection difficulties

    Can you make a small program that executes and demonstrates the problem?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Collision Detection difficulties

    There should not be a problem with the intersects method. Likely, this is what I think is happening:

    Computers usually work with discrete time steps. This means that between frames, one (or both) of the rectangles will "magically" jump locations to it's new location. If that location happens to completely jump over the object you're checking collision detection with, then no collision will be registered. There are two ways to fix this:

    1. Calculate the area between the two discrete locations and check those areas for intersections. This can be done by taking a cross section based off of the current direction of travel, then projecting that line towards the new direction and orientation. Simply connect the two lines into a quadrilateral, and then check collisions of the quadrilaterals (unfortunately, that means you'll have to write your own collision detection code).
    2. Calculate an estimated time to collision based off of the current velocities. If the time is less than one frame, then you know on the next frame a collision has happened (assuming that current velocities don't change until after the discrete time step). This is basically trying to operate on a continuous timeline, though there will still be some discrete behaviors present.

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection difficulties

    Ugh I found my mistake... I was setting the location using a modulated number, but in my intersects() method I forgot to modulate it. As a result, whenever the objects intersected, it was comparing with numbers way out of the playing field.

    I think I ran into the discrete time steps problem when making tetris(and never finished) so ill have to try those solutions. The first one seems too complicated tho haha.

    Thanks again for the help!

Similar Threads

  1. Canny Edge Detection
    By tiny in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2012, 01:54 PM
  2. Having Difficulties With a Multi Thread Code... Help?
    By Allicat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 5th, 2011, 12:35 AM
  3. Collision detection - whats wrong with my code?
    By Javaz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 22nd, 2011, 08:20 PM
  4. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM
  5. How to detect brightest spot among all spots?
    By BharatT in forum Java Theory & Questions
    Replies: 4
    Last Post: February 6th, 2009, 09:12 PM