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 4 of 4

Thread: Particle System Questions

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

    Red face 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:

    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:

    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.


  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 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.
    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 System Questions

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

  4. #4
    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 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.
    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!

Similar Threads

  1. System.out.print -align
    By juwan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 17th, 2010, 04:36 PM
  2. Screwed up system variables
    By paisa90 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 19th, 2010, 11:56 PM
  3. remote system shutdown
    By prince joseph in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 28th, 2010, 02:35 AM
  4. System.CurrentTimeMilliseconds
    By Dave in forum Java SE APIs
    Replies: 1
    Last Post: August 26th, 2009, 11:02 AM
  5. [SOLVED] Mobile operating system using Java technology
    By blackJava in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: April 16th, 2009, 02:44 PM