How would you go about coding this?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 32
Reputation: coveredinflies is an unknown quantity at this point 
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

How would you go about coding this?

 
0
  #1
Nov 10th, 2008
Hi,
Basically I want to simulate lots of circles bouncing off of each other in a box.
My current plan is to form a grid with lots of points and give each particle a position and speed which is a multiple of the grid spacings. Then at each time move the particles and see if any intersect. I am fairly new to programming so this way is easier in that respect but it seems like this way will be very computationally intensive though.

Do you think this is a good way to do it or will I be wasting my time?


Thanks.

P.S I may do it in C++ or java I haven't decided yet if that makes any difference. (I am not sure yet how easy it is to display it graphically in java yet).
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: How would you go about coding this?

 
0
  #2
Nov 10th, 2008
fairly tough assignment to start programming with

here is an example with a single circle
http://www.java2s.com/Code/Java/Swin...cingCircle.htm

you can use that concept, on top of the bounds, you can check the other circles positions
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 32
Reputation: coveredinflies is an unknown quantity at this point 
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Re: How would you go about coding this?

 
0
  #3
Nov 10th, 2008
Thanks, I am not too concerned with the graphics to start with (it is a science thing) that website looks quite useful though.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: How would you go about coding this?

 
1
  #4
Nov 10th, 2008
ahhh, math junkie then lol


this might be helpful as well, more of the logic side, than the graphical
http://bytes.com/forum/thread645269.html
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,649
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: How would you go about coding this?

 
0
  #5
Nov 10th, 2008
If you are simulating lots of circles bouncing off of each other, you would have to compute whether or not they are currently touching/adjacent by giving each circle a radius, and keeping the current position of the circle's center. Then, as the circle bounced, you could do a calculation between it and all the other circles to figure out if the circle is currently occupying space which is touching another circle's space. If so, bounce them in different directions. Thats the only way I can think to do this assignment. It would be much more difficult to treat each circle as having "multiple points" and then try to see if any of the points are touching than to use each circle's current center point as a marker for whether or not it is adjacent to any other circles (based on their center, radius, and area formulas).
Last edited by BestJewSinceJC; Nov 10th, 2008 at 9:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 32
Reputation: coveredinflies is an unknown quantity at this point 
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Re: How would you go about coding this?

 
0
  #6
Nov 12th, 2008
Thanks you too, giving it a whirl now.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How would you go about coding this?

 
1
  #7
Nov 12th, 2008
If you haven't already, you may want to look through the Ellipse2D API. It has methods related to containment and intersection that may prove useful.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 32
Reputation: coveredinflies is an unknown quantity at this point 
Solved Threads: 0
coveredinflies coveredinflies is offline Offline
Light Poster

Re: How would you go about coding this?

 
0
  #8
Nov 12th, 2008
Originally Posted by Ezzaral View Post
If you haven't already, you may want to look through the Ellipse2D API. It has methods related to containment and intersection that may prove useful.
Thanks, unfortunately I had already started before I read your post but at least I will think to look in the class file next time. Anyway for a first approximation I thought up a simple way to do it so the physics side is coming along nicely .

Not sure if I should post in the same thread or not but I have (stupidly) decided to try and animate using the ActionListener (I can't figure out the Threads and Runnable stuff, at least not without more time anyway). I have a couple of problems though if someone could point me in the right direction I would be very grateful.

Firstly when I repaint() my blob doesn't come back . Does repaint() simply call the paintcomponent function again? I can't figure out why it isn't working. I pretty much 'stole' the graphics part of my program from the net and my textbook so it might be (hopefully) an obvious problem.

Here is my program with the irrelevant physics taken out.

public class Brownain
{ 
  public static void main(String[] args)
  {
	    JFrame box = new JFrame();
	    Container background = box.getContentPane();
	    background.setBackground(new Color(255,250,202));
	    box.setSize(700, 700);
	    box.setTitle("Browniand Motion.");
	    box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    
	    
	    Particle particle = new Particle();
	    box.add(particle);
	    box.setVisible(true);
	 
	    ActionListener listener = new Particle(); Does this mean it doesn't update the particle I made earlier but this 'listener' particle?
	 
	   
	    Timer mover = new Timer(25, listener);
	 
	    mover.start();
	 
	  }
	  
} 


  
class Particle extends JComponent implements ActionListener
{
	public Particle()
	{
		rand = new Random();
		xcentre = rand.nextDouble()*600;
		ycentre = rand.nextDouble()*600;
		System.out.println(xcentre);
		particle = new Ellipse2D.Double(xcentre, ycentre, 30, 30);
	}
	
	public double getx()
	{
		return xcentre;
		
	}
	
	public double gety()
	{
		return ycentre;
	}
	
	public double getRadius()
	{
		return radius;
	}
	
	public void actionPerformed(ActionEvent event)
    {
	
		xcentre+=rand.nextGaussian()*10;
		ycentre+=rand.nextGaussian()*10;
		
		particle.setFrame(xcentre,ycentre,radius,radius);
		
		repaint();
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		Graphics2D g2 = (Graphics2D) g;
		g2.setPaint(Color.PINK);
		
		g2.fill(particle);
		g2.draw(particle);
	}
	
	private double xcentre;
	private double ycentre;
	private double radius;
	private double mass;
	private double xvel;
	private double yvel;
	private Random rand;
	private Ellipse2D.Double particle;
	

}

Also eventually I want to have many particles on the screen at once and have all there positions updating with the ActionListener but when I add

  1. for(int i=0; i<50; i++)
  2. {
  3. molecule[i] = new Particle();
  4. box.add(molecule[i]);
  5. }

Only 1 extra particle appears which I find very strange.

Anyway sorry about the long post hopefully it won't get lost hidden in my old thread and someone can help me out
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 138
dickersonka dickersonka is offline Offline
Veteran Poster

Re: How would you go about coding this?

 
0
  #9
Nov 12th, 2008
sorry not able to confirm this and test your code right now, but try adding validate() before your repaint()
Last edited by dickersonka; Nov 12th, 2008 at 6:44 pm.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How would you go about coding this?

 
0
  #10
Nov 12th, 2008
>Does this mean it doesn't update the particle I made earlier but this 'listener' particle?
Yes. Add the one that you already created, since that is the one you want the timer to call on.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 856 | Replies: 15
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC