# Particle collisions...

• February 2nd, 2011, 11:26 PM
Macattack1459
Particle collisions...
I guess this is more of a physics question then a java question but i figured there would be a lot of smart people on this forum so here goes:

Code :

```import java.applet.*; import java.awt.*; import javax.swing.*;   import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.geom.*; import java.util.concurrent.*; import java.lang.reflect.Array; import java.util.ArrayList; import java.awt.event.*;   public class point extends Ellipse2D.Float implements MouseListener{ double y_speed; double x_speed; int gravity = 2; private int d; private int width = Particle.WIDTH; private int height = Particle.HEIGHT; private ArrayList<point> points;   // make particles randomly in screen ------V public point(int diameter, ArrayList<point> points){ super((int) (Math.random() * (Particle.WIDTH - 20) + 1), (int) (Math.random() * (Particle.HEIGHT - 20) + 1), diameter, diameter); this.d = diameter; this.x_speed = (int)(Math.random()* 10 + 1); this.y_speed = (int)(Math.random()* 10 + 1); this.points=points; }   public void move (){ Rectangle2D r = new Rectangle2D.Float(super.x, super.y, d+1, d+1);   // collision loop -------V for (point p : points){ if (p != this && p.intersects(r)){ double txspeed = x_speed; double tyspeed = y_speed; [COLOR="Red"]// need stuff in HERE!!!![/COLOR]   }   }   // movement stuff ------V if (super.x < 0){ super.x = 0; x_speed = Math.abs(x_speed)/2; } else if (super.x > width - d){ super.x = width-d; x_speed = -Math.abs(x_speed)/2; } if (super.y < 0){ super.y = 0; y_speed = Math.abs(y_speed)/2; } else if (super.y > height - d){ super.y = height-d; y_speed = -Math.abs(x_speed)/2; } if (x_speed>5){ x_speed--; } if (x_speed<5){ x_speed++; } if (y_speed>5){ y_speed--; } if (y_speed<5){ y_speed++; } y_speed+=gravity; y_speed*=0.99; x_speed*=0.99; super.x+=x_speed; super.y+=y_speed; }   @Override public void mouseClicked(MouseEvent e) { for (point p : points){ x_speed=x_speed+e.getX();   }   }   @Override public void mouseEntered(MouseEvent e) {   }   @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub   }   @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub   }   @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub   } }```
(ignore the mouse listener stuff i was just starting to mess with it)

I've been trying to get the particles in my simulation to collide, and yes, i know several methods of detecting collisions but i figured checking for rectangle intersection would be the quickest to code. Now what I am wondering is what mathematically i can do when a collision is detected to keep the particles out of other particles rectangles in a stable manner. (no explosive flying particles going 1,000,000 miles an hour) I've been trying to figure this out for days and i feel really dumb that after watching ton of tutorials and stuff the best i have got to work is a spastic blob of dots... thanks for any help given.

ALSO if you need the other classes for some reason you can but the collision things all that really matters. :-/
• February 3rd, 2011, 07:07 AM
KevinWorkman
Re: Particle collisions...
I don't really understand your question. What do you want to happen when you detect a collision? Should the particles bounce off each other? Veer off to avoid collisions? Something else?

When posting code, it should really be in the form of an SSCCE (that's a link). That means it should be runnable just by copying and pasting it into a single file and as small as possible while still demonstrating what you're talking about.
• February 3rd, 2011, 08:44 PM
Macattack1459
Re: Particle collisions...
Quote:

Originally Posted by KevinWorkman
I don't really understand your question. What do you want to happen when you detect a collision? Should the particles bounce off each other? Veer off to avoid collisions? Something else?

When posting code, it should really be in the form of an SSCCE (that's a link). That means it should be runnable just by copying and pasting it into a single file and as small as possible while still demonstrating what you're talking about.

I want them to avoid collisions as the error inherent in detecting collisions after they occur causes too many bugs and non-stability so yes veering away would be preferable... to do this don't you do something with averaging velocities but would i also need to change my method of detecting collisions?