| | |
How would you go about coding this?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 32
Reputation:
Solved Threads: 0
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).
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).
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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
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
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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
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
www.houseshark.net
•
•
Join Date: Sep 2008
Posts: 1,649
Reputation:
Solved Threads: 206
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.
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.
•
•
Join Date: Jul 2008
Posts: 32
Reputation:
Solved Threads: 0
•
•
•
•
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.
.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
Java Syntax (Toggle Plain Text)
for(int i=0; i<50; i++) { molecule[i] = new Particle(); box.add(molecule[i]); }
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
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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
www.houseshark.net
![]() |
Similar Threads
- CODING QUESTION: What is the MOST importanting thing to do when...... (Computer Science)
- So drunk. Still coding. (Geeks' Lounge)
- Help Needed Urgently With Coding (Java)
- C++ Coding :: First Ever (C++)
- Is their a way to earn money from freelance VB coding on the net (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Weird Question
- Next Thread: question on java
Views: 856 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api appinventor apple applet application arguments array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component database draw eclipse error event exception file fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jetbrains jmf jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie notdisplaying number object oracle page print problem program programming project qt recursion scanner screen server set size sms socket sort spamblocker sql string swing system test threads time transfer tree variablebinding windows xor






