Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Easy to use particle engine  (Read 10603 times)
0 Members and 1 Guest are viewing this topic.
Offline GreenLantern101
« Posted 2014-12-27 18:13:01 »

So here are just two classes in my (super bare)particle engine. Basically to use, just make an arraylist of ParticleEmitters in the testing class, and then iterate through that for the moving and drawing methods.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.Random;
    public class Particle
    {
        final int lifetime = 30; Color color; double age = 0; int radius = 1;
        Random r = new Random();
        Point2D.Float  movement= new Point2D.Float(0,0);
      public Particle(Point2D.Float  loc, double angle_deg, Color color, float speed, int radius)
      {

            this.loc = loc; this.radius=radius; this.color = color;
         
            movement.x = speed * (float)Math.cos(Math.PI * angle_deg / 180);
            movement.y = speed * (float)Math.sin(Math.PI * angle_deg / 180);
      }
     
      Point2D.Float loc;

      public boolean shouldRemove = false;
     
      public void Move()
      {
            loc.x += movement.x; loc.y += movement.y;
         age ++;
         if (age >= lifetime) {
                shouldRemove = true;
         }
      }
     
     
     
      public void Draw(Graphics g)
      {
         Color col = new Color(color.getRed(), color.getGreen(), color.getBlue(),
               (int)Math.max(255 - (age / lifetime * 255), 0));
           
         g.setColor(col);        
         g.fillOval((int)(loc.x - 1), (int)(loc.y - 1),radius, radius);
      }
    }





1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Random;

public class ParticleEmitter
    {
        public ArrayList<Particle> particles = new ArrayList<Particle>();
        protected Random r= new Random();
        /// <summary>
        ///Initialize particle emitter
        /// </summary>
        public ParticleEmitter(Point2D.Float loc, Color colorA, Color colorB, int population)
        {
            if (population > 0)
            {
               //used to be min of 10
                int n = Math.max(10, (int)Math.floor(Math.sqrt(population)));
                int scatter = (int)Math.floor(Math.sqrt(population) / 4) + 5;
                for (int i = 0; i < n; i++)
                {
                    //alternate colors
                    Color c = colorA;
                    //if even
                    if (i%2 == 0) c = colorB;

                    //slightly random centering and angle and size
                    Point2D.Float pt_new=
                          new Point2D.Float(loc.x+ randInt(-scatter, scatter),
                          loc.y+ randInt(-scatter, scatter));
                    particles.add(new Particle(pt_new,
                        randInt(0, 360), c,
                        (float)Math.max(1, Math.sqrt(population) / 10) * 1f, randInt(2, 6)));


                }
            }
        }

        public ParticleEmitter() {
         // TODO Auto-generated constructor stub
      }

      /// <summary>
        ///draws all the particles from this particle emitter
        /// </summary>
        public void DrawParticles(Graphics g)
        {
           //basically, a foreach loop
            for(Particle p : particles)
            {
                p.Draw(g);
            }
               
        }
        /// <summary>
        ///steps particles one frame forward
        /// </summary>
        public void MoveParticles()
        {
           //basically, a foreach loop
           for(Particle p : particles)
            {
                p.Move();
            }
                for (int i = 0; i < particles.size(); i++)
                {
                    if (particles.get(i).shouldRemove)
                        particles.remove(i);
                }
        }
        // a helper method to generate random number in a range
        public int randInt(int min, int max)
        {
           return r.nextInt((max - min) + 1) + min;
        }
    }



It looks pretty good...


Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #1 - Posted 2014-12-27 18:40:05 »

You have a bug in the removal loop, the loop should be backwards, or else the particle after each one removed will be skipped. Alternatively, don't increment the induction variable when a particle is removed.
Also have fun with the ordered removal on large ArrayLists.

Might as well call move in that same loop instead of iterating twice as well.
Also, the java method naming convention is lower camelCase, not upper.
Just a few tips.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (76 views)
2018-06-10 19:43:48

EgonOlsen (56 views)
2018-06-10 19:43:44

EgonOlsen (76 views)
2018-06-10 19:43:20

DesertCoockie (258 views)
2018-05-13 18:23:11

nelsongames (156 views)
2018-04-24 18:15:36

nelsongames (155 views)
2018-04-24 18:14:32

ivj94 (896 views)
2018-03-24 14:47:39

ivj94 (160 views)
2018-03-24 14:46:31

ivj94 (809 views)
2018-03-24 14:43:53

Solater (173 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!