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:
(ignore the mouse listener stuff i was just starting to mess with it)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 } }
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.