Particle System Questions
Hello, i'm pretty new to java... anyway i've been trying to make a particle system so far i've made one class that can simulate one particle with simple collisions but i decided i'd need to move into using multiple classes. I have one class for simulation/updating and one for rendering how can I plug a variables constantly updating value into the x and y position values of a fillRect in my graphical class? heres the code if it helps... I might be going about this all wrong as i'm newish to java:
Simulation(Sorta) class:
Code :
import java.awt.Component;
import javax.swing.*;
public class Particle {
public static int Xpos = 50; //makes a int of the x position
public static int Ypos = 50; //makes a int of the y position
public float Xvel = 10; // makes a float of the x velocity
public float Yvel = -20; // makes a float of the y velocity
public int gravity = 2; //sets gravity to 2
public static void main(String[] args) {
JFrame f = new JFrame("Fluid");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Renderer r = new Renderer();
f.add(r);
f.setSize(500,500);
f.setVisible(true);
f.setResizable(false);
}
public void simulate(){
Yvel += gravity;
Ypos += Yvel;
if (Ypos >= 190 || Ypos <= 0) {
Yvel = -Yvel/2;
}
Xpos += Xvel;
if (Xpos >= 200 || Xpos <= 0) {
Xvel = -Xvel/2;
}
Yvel--;
}
}
Rendering Class:
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Renderer extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.CYAN);
g.fillRect(Particle.Xpos, Particle.Ypos, 3, 3);
}
}
Any help is appreciated also and help with making a emitter or making multiple particles would be very much appreciated as that is the next thing I will start working on and undoubtedly have trouble with. :)
Re: Particle System Questions
You really have to understand what the static keyword is doing before you use it.
I assume you're going to want multiple particles, right? So you need a Collection of them. To paint them, simply iterate over the Collection, doing any painting you want (or calling the paint method of each Particle).
That means that each Particle will have to keep track of its own position. Making the position variables static defeats that purpose.
Re: Particle System Questions
Quote:
Originally Posted by
KevinWorkman
You really have to understand what the static keyword is doing before you use it.
I assume you're going to want multiple particles, right? So you need a Collection of them. To paint them, simply iterate over the Collection, doing any painting you want (or calling the paint method of each Particle).
That means that each Particle will have to keep track of its own position. Making the position variables static defeats that purpose.
Yeah, i quickly figured out that making them static made the particle unable to move yet the application gives an error claiming that is the solution when they aren't static so i'll just look at more tutorials and try to figure this all out. I still have one question say i want 1000 particles something too big to do manually should i store their x,y data in a few arrays and do the math to the whole array? Also how could i check if one particle is close to any other ones?
Re: Particle System Questions
Yeah, you would store the particles in some kind of data structure. It's easier if each particle keeps track of its own position, speed, and direction, that way you can simple call a step function on each particle.
Testing for collisions is harder. I would wait to do that until you have the movement logic coded.