Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Particle collisions...

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:


    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.
    Last edited by Macattack1459; February 3rd, 2011 at 12:31 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Particle collisions...

    Quote Originally Posted by KevinWorkman View Post
    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?

Similar Threads

  1. Particle System Questions
    By Macattack1459 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 26th, 2011, 07:49 AM