Results 1 to 3 of 3
  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Collision Detection

    Hey, I am hoping I can get some advice on this topic. I am currently playing around with very basic physic simulation (I should stress the basic). I created a class called particle, which has a location, and velocities (I'm using ints for positioning).

    Java Code:
    public class Particle{
      private int xPos, yPos;
      private int dX, dY;  //delta x, delta y
      ... 
    }
    Now I draw them to a buffered image and animate them with swing timer. All of that I got working, they bounce around the screen and the collisions on the walls are handled.

    I'd like to implement collision detection between the balls, simple enough, right?

    My thought is to create the following method in the particle class:
    Java Code:
    public boolean collide(Particle p){
    		Point center = new Point(xPos + (DIAMETER / 2), yPos + (DIAMETER/2));
    		Point pCenter = new Point(xPos + (DIAMETER/2), yPos +(DIAMETER/2));
    		if(distance(center, pCenter) <= DIAMETER)
    			return true;
    		return false;
    	}
    Where distance calculates the distance as:
    change in x square + change in y squared and then take the square root of the result.

    Which, in case it's not clear simply calculates the center point of the circle (since the xPos, yPos, will be the rectangular corner on the top left -- to my knowledge) then sees if the two particles are within a certain range(DIAMETER = 20). With this working it seems easy enough to simply do the following to handle collisions:

    Java Code:
    for(Particle x : rainDrops){
    			if(p.collide(x)){
    				p.setDX(p.getDX() * -1);
    				p.setDY(p.getDY() * -1);
    				x.setDX(x.getDX() * -1);
    				x.setDY(x.getDY() * -1);
    			}
    		}
    rainDrops is an array of particles. I added in a line which would print when some collision occurs and it spams quite quickly. Now my problem is that very rarely do the particles bounce off each other. Quite often it seems they just move right through each other.

    It my approach to collision detection well thought? Or should I be doing it a different way?

    The code isn't too much but I'd rather not dump 200 lines of code if it's not necessary. My basic process is

    Java Code:
    for all p
      for all x
        if p hits x
          reverse direction of p and x
        end if
      end for
    end for

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Collision Detection

    That looks pretty good, Take a read of this: Shape (Java 2 Platform SE v1.4.2)
    I have seen other people use:
    Java Code:
    public Rectangle getBounds() {
    
    }
    
    and then:
    
    object.intersects(rectangle)
    That is two of the methods in the shape class, but you should read them all.
    If you made your own methods to return if you have a collision and not being lazy and use what i just did, I think its well thought. I like your way of doing it.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Collision Detection

    Consider something like a Quadtree to organize the objects into data structure. The tree can then be used to limit the number of calculations necessary between each object by avoiding comparisons between very distant objects. Also note that in the algorithm above, avoid duplicate collision checks between the same 2 objects (which would negate the affects of the collision).
    Last edited by doWhile; 10-07-2011 at 11:42 PM.

Similar Threads

  1. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 04:55 PM
  2. Java3D collision detection
    By abbeywell in forum Java Gaming
    Replies: 5
    Last Post: 04-27-2011, 02:33 PM
  3. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 12:38 AM
  4. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 06:13 PM
  5. Collision Detection (Game)
    By mscwd in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-28-2008, 08:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •