Results 1 to 3 of 3
Thread: Collision Detection
- 10-07-2011, 05:55 PM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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).
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.Java Code:public class Particle{ private int xPos, yPos; private int dX, dY; //delta x, delta y ... }
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:
Where distance calculates the distance as: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; }
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:
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.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); } }
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
- 10-07-2011, 11:13 PM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
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:
That is two of the methods in the shape class, but you should read them all.Java Code:public Rectangle getBounds() { } and then: object.intersects(rectangle)
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.
- 10-07-2011, 11:40 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
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
-
Collision Detection?
By Alerhau in forum New To JavaReplies: 39Last Post: 09-07-2011, 04:55 PM -
Java3D collision detection
By abbeywell in forum Java GamingReplies: 5Last Post: 04-27-2011, 02:33 PM -
Really Need help with some collision detection
By Harwad in forum New To JavaReplies: 1Last Post: 01-23-2011, 12:38 AM -
Collision Detection
By dotabyss in forum Java GamingReplies: 0Last Post: 03-14-2010, 06:13 PM -
Collision Detection (Game)
By mscwd in forum Sun Java Wireless ToolkitReplies: 0Last Post: 01-28-2008, 08:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks