Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (778)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
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 10863 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: 486
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  
 
 

 
hadezbladez (350 views)
2018-11-16 13:46:03

hadezbladez (183 views)
2018-11-16 13:41:33

hadezbladez (349 views)
2018-11-16 13:35:35

hadezbladez (85 views)
2018-11-16 13:32:03

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

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

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

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

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

nelsongames (2309 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!