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

    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){
    				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
    Melbourne, Victoria, Australia
    Rep Power

    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:
    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
    Rep Power

    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-08-2011 at 12:42 AM.

Similar Threads

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