Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  [LIBGDX] Particles disappearing and then reappearing  (Read 935 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2014-04-05 18:13:09 »

Hey,

I've just, hopefully, finished converting my particle system from pure Java to libGDX. Everything seems to be working properly, except for the 'death' of the particles. The particles die and then reappear, or so it seems, for some reason unknown to me.

Each particle starts with a certain lifetime, and each update to the particle decreases the remaining lifetime. The alpha of the particle is calculated using (float)(remainingLife/totalLife). When the lifeTime reaches 0 then the particle is deleted because it's alpha is 0.

Here's the compiled project, you can see how the particles disappear and reappear. Exit with Alt+F4.
https://mega.co.nz/#!IstGjR6b!VZCkZ9ClDBUedZLHIPnchx9thb4Gs_asFvnlZpX7qo8



I'm thinking that the error is in how I setup libGDX in the Driver/Main classes, but it could also be with how I set the color/alpha in the RainbowSnow/Particle classes. Normally I'd be able to figure this kind of thing out pretty quickly, but I barely know how to use libGDX atm. Thanks for any ideas.

Driver:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
package core.desktop;

import java.awt.Dimension;
import java.awt.Toolkit;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

import core.Main;

public class DesktopLauncher {
   public static void main (String[] arg) {
      Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
     
      LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
      config.title = "Particle System";
      config.width = (int)screenDimensions.getWidth();
      config.height = (int)screenDimensions.getHeight();
      config.fullscreen = true;
      config.useGL30 = true;
     
        new LwjglApplication(new Main(), config);
   }
}


Main:
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  
package core;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import effect.Effect;
import effect.RainbowSnow;

public class Main extends ApplicationAdapter {
   Effect e;
   ShapeRenderer shapeRenderer;

   @Override
   public void create() {
      e = new RainbowSnow(0.0, Gdx.graphics.getHeight() + 50, Gdx.graphics.getWidth());
      shapeRenderer = new ShapeRenderer();
   }

   @Override
   public void render() {
      ((RainbowSnow)e).update();
     
      Gdx.gl.glClearColor(0, 0, 0, 0);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
     
      ((RainbowSnow)e).render(shapeRenderer, e.getIsOval());
   }

   @Override
   public void resize(int width, int height) {
   }
   
   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }

   @Override
   public void dispose() {
   }
}


Effect:
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  
package effect;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import particle.Particle;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

public class Effect {
   protected final static Random RANDOM = new Random();
   /** A collection of particles that make up the snow.*/
   protected final List<Particle> PARTICLES = new ArrayList<Particle>();
   /** The origin of this snow on the X-axis.*/
   protected final double ORIGIN_X;
   /** The origin of this snow on the Y-axis.*/
   protected final double ORIGIN_Y;
   /** Whether or not to render the particles as ovals. If not then render as squares. Ovals are extremely CPU intensive for large effects*/
   protected final boolean IS_OVAL;
   protected Iterator<Particle> iterator;
   protected int counter = 0;
   
   public Effect(final double ORIGIN_X, final double ORIGIN_Y, final boolean IS_OVAL) {
      this.ORIGIN_X = ORIGIN_X;
      this.ORIGIN_Y = ORIGIN_Y;
      this.IS_OVAL = IS_OVAL;
   }
   
   /**
    * Updates the effect.
    */

   public void Update() {}
   
   /**
    *  Renders the snow to the screen.
    * @param g Graphics object with which to draw.
    */

   public void render(final ShapeRenderer SHAPE_RENDERER, final boolean IS_OVAL) {
      if(counter == 0) { System.out.println(PARTICLES.size()); }
     
      Gdx.gl.glEnable(GL30.GL_BLEND);
      Gdx.gl.glBlendFunc(GL30.GL_SRC_ALPHA, GL30.GL_ONE_MINUS_SRC_ALPHA);
      SHAPE_RENDERER.begin(ShapeType.Filled);
      iterator = PARTICLES.iterator();
      while(iterator.hasNext()) {
         iterator.next().render(SHAPE_RENDERER, IS_OVAL);
      }
      SHAPE_RENDERER.end();
      Gdx.gl.glDisable(GL30.GL_BLEND);
   }
   
   /** @return Whether or not to render the particles as ovals. If not then render as squares. */
   public boolean getIsOval() { return IS_OVAL; }
}


RainbowSnow:
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  
79  
80  
81  
82  
83  
84  
85  
86  
87  
package effect;


import com.badlogic.gdx.graphics.Color;

import particle.Particle;

/**
 * Represents rainbow snow fall.
 * @author Valkryst
 * --- Last Edit 05-Apr-2014
 */

public class RainbowSnow extends Effect {
   /** The length (x-axis) of the screen. */
   private final double SCREEN_LENGTH;
   private final double COLOR_CHANGE_CONSTANT = 0.25;
   private float red = 255, green = 0, blue = 0;
   private boolean isRed = false, isGreen = true, isBlue = false;
   
   /**
    * Constructs a new Snow object.
    */

   public RainbowSnow(final double ORIGIN_X, final double ORIGIN_Y, final double SCREEN_LENGTH) {
      super(ORIGIN_X, ORIGIN_Y - 50, false);
      this.SCREEN_LENGTH = SCREEN_LENGTH;
   }
   
   /**
    * Updates the snow.
    */

   public void update() {
      Color c = new Color();
      c.set(red/255f, green/255f, blue/255f, 1.0f);

      if(counter == 10) {
         for(int i=0;i<RANDOM.nextInt(1800) + 120;i++) { newParticle(c, RANDOM.nextInt(8), 40); }
         counter = 0;
      } else {
         counter++;
      }
     
      if(isRed) {
         if(blue > 0) { blue -= COLOR_CHANGE_CONSTANT; }

         if(red < 255) {
            red += COLOR_CHANGE_CONSTANT;
         } else if(red == 255 && blue == 0) {
            isRed = false;
            isGreen = true;
         }
      } else if(isGreen) {
         if(red > 0) { red -= COLOR_CHANGE_CONSTANT; }
         
         if(green < 255) {
            green += COLOR_CHANGE_CONSTANT;
         } else if(green == 255 && red == 0) {
            isGreen = false;
            isBlue = true;
         }
      } else if(isBlue) {
         if(green > 0) { green -= COLOR_CHANGE_CONSTANT; }
         
         if(blue < 255) {
            blue += COLOR_CHANGE_CONSTANT;
         } else if(blue == 255 && green == 0) {
            isBlue = false;
            isRed = true;
         }
      }

      iterator = PARTICLES.iterator();
      while(iterator.hasNext()) {
         if(iterator.next().update()) {
            iterator.remove();
         }
      }
   }
   
   /**
    * Creates a new Particle object.
    * @param sizeIn The size, in pixels^2, of the new Particle.
    * @param decayTimeIn The number of movements before the new Particle decays.
    */

   public void newParticle(final Color COLOR, int SIZE, int LIFE) {
      PARTICLES.add(new Particle(RANDOM.nextInt((int)SCREEN_LENGTH), super.ORIGIN_Y, RANDOM.nextDouble() * (RANDOM.nextBoolean() ? -2 : 2), RANDOM.nextDouble() * -2.5, 0.0050 *(RANDOM.nextBoolean() ? -1 : 1), 0.0, SIZE + RANDOM.nextInt(8), LIFE + RANDOM.nextInt(800), COLOR));
   }
}


Particle:
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  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
package particle;

import java.awt.Dimension;
import java.awt.Toolkit;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

/**
 * Represents a particle in 2D space.
 * @author Valkryst
 * --- Last Edit 05-Apr-2014
 */

public class Particle {
   private final static Dimension SCREEN_DIMENSIONS = Toolkit.getDefaultToolkit().getScreenSize();
   /** The location of the particle on the X-axis. */
   private double x;
   /** The location of the particle on the Y-axis. */
   private double y;
   /** The change in X, per update, of the particle. */
   private double dx;
   /** The change in Y, per update, of the particle. */
   private double dy;
   /** The gravitational pull to the left (negative) and right (positive) acting on this particle. */
   private final double GRAVITY_X;
   /** The gravitational pull to the up (negative) and down (positive) acting on this particle. */
   private final double GRAVITY_Y;
   /** The size in pixels^2 of the particle. */
   private final int SIZE;
   /** The remaining lifetime of the particle. */
   private double currentLife;
   /** The total lifetime of the particle. */
   private final double TOTAL_LIFE;
   /** The color of the particle. */
   private Color color;
   
   /**
    * Constructs a new particle with the specified data.
    * @param X The location of the particle on the X-axis.
    * @param Y The location of the partivcle on the Y-axis.
    * @param DX The change in X, per update, of the particle.
    * @param DY The change in Y, per update, of the particle.
    * @param GRAVITY_X The gravitational pull to the left (negative) and right (positive) acting on this particle.
    * @param GRAVITY_Y The gravitational pull to the up (negative) and down (positive) acting on this particle.
    * @param SIZE The size in pixels^2 of the particle.
    * @param LIFE The remaining lifetime of the particle.
    * @param COLOR The color of the particle.
    */

   public Particle(final double X, final double Y, final double DX, final double DY, final double GRAVITY_X, final double GRAVITY_Y, final int SIZE, final double LIFE, final Color COLOR) {
      x = X;
      y = Y;
      dx = DX;
      dy = DY;
      this.GRAVITY_X = GRAVITY_X;
      this.GRAVITY_Y = GRAVITY_Y;
      this.SIZE = SIZE;
      currentLife = LIFE;
      TOTAL_LIFE = LIFE;
      color = COLOR;
   }
   
   /**
    * Updates the particle's position, change in x, change in y,
    * remaining lifetime, and color.
    * @return Whether the particle is 'dead' or not.
    */

   public boolean update() {
      if(x > SCREEN_DIMENSIONS.width + 32 || x < -32 || y > SCREEN_DIMENSIONS.height + 32) {
         return true;
      } else {
         x += dx;
         y += dy;
         dx += GRAVITY_X;
         dy += GRAVITY_Y;
         currentLife--;
         color.set(color.r, color.g, color.b, (float)(currentLife/TOTAL_LIFE));
         return (currentLife <= 0 ? true : false);
      }
   }
   
   /**
    * Renders the particle to the screen.
    * @param G The shaperenderer object to render with.
    * @param IS_OVAL Whether or not to render the particle as an oval.
    */

   public void render(final ShapeRenderer SHAPE_RENDERER, final boolean IS_OVAL) {
      SHAPE_RENDERER.setColor(color);

      if(IS_OVAL) {
         SHAPE_RENDERER.circle((int)x-(SIZE / 2), (int)y-(SIZE / 2), SIZE, SIZE); //x-(size/2) & y-(size/2) make sure the particle is rendered at (x, y).
     } else {
         SHAPE_RENDERER.rect((int)x-(SIZE / 2), (int)y-(SIZE / 2), SIZE, SIZE); //x-(size/2) & y-(size/2) make sure the particle is rendered at (x, y).
     }
   }
}



Edit:
Ignore the JavaDocs if they don't make sense. I haven't updated them recently.
Offline Gibbo3771
« Reply #1 - Posted 2014-04-05 18:42:46 »

Fading out and them removing I guess?

If the rgb color values go < 0, they reset to 1.

This could be it.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline tyeeeee1
« Reply #2 - Posted 2014-04-05 18:48:12 »

Fading out and them removing I guess?

If the rgb color values go < 0, they reset to 1.

This could be it.

The rgb colors should never go below 0 with the way I have it set up. I'll double check that though.

Edit:
It doesn't seem to be related to the rgb values as far as I've seen in my tests.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2014-04-05 19:56:20 »

Disable all texturing and coloring and see if the actual geometry disappears. That way you can rule out a lot of stuff.

Myomyomyo.
Offline Gibbo3771
« Reply #4 - Posted 2014-04-05 20:14:20 »

Fading out and them removing I guess?

If the rgb color values go < 0, they reset to 1.

This could be it.

The rgb colors should never go below 0 with the way I have it set up. I'll double check that though.

Edit:
It doesn't seem to be related to the rgb values as far as I've seen in my tests.

Fair enuff, I took a guess as you can probably imagine that amount of code, may as well suggest from my previous experience.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline tyeeeee1
« Reply #5 - Posted 2014-04-05 20:15:40 »

Disable all texturing and coloring and see if the actual geometry disappears. That way you can rule out a lot of stuff.

Huh? There are no textures or geometry. I'm just drawing rectangles on the screen using rgba colors and a ShapeRenderer object.
Offline Phibedy

Senior Member


Medals: 8



« Reply #6 - Posted 2014-04-05 20:17:12 »

I found your error. Hell that's a shitty one  Grin
I shouldn't tell you because you shouldn't code that way  Smiley
Offline BurntPizza
« Reply #7 - Posted 2014-04-05 20:20:09 »

Huh? There are no textures or geometry. I'm just drawing rectangles on the screen using rgba colors and a ShapeRenderer object.

What do you think that rectangle is? A quad perhaps? Hmm...

Also,
I shouldn't tell you because you shouldn't code that way  Smiley

Makes no sense. Just tell him.
Offline tyeeeee1
« Reply #8 - Posted 2014-04-05 20:23:44 »

What do you think that rectangle is? A quad perhaps? Hmm...

A point combined with a width and height that's sent somewhere as an instruction for which pixels on the screen to color. No idea what a quad is, just started with libGDX a few hours ago.
Offline Phibedy

Senior Member


Medals: 8



« Reply #9 - Posted 2014-04-05 20:25:59 »

It took me 1.3 hours to find it, I love debugging. Indeed I just hate to not know the solution  Roll Eyes
So I will go for a quizz  Grin
(No that's not because I am evil, that's just how some other guy helped me and you learn most)
First hint:
You create an object but you don't use it every time you create it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #10 - Posted 2014-04-05 20:28:56 »

It took me 1.3 hours to find it, I love debugging. Indeed I just hate to not know the solution  Roll Eyes
So I will go for a quizz  Grin
(No that's not because I am evil, that's just how some other guy helped me and you learn most)
First hint:
You create an object but you don't use it every time you create it.


The Color object in the update() method of the RainbowSnow class? Moving that within the if statement gives no change.
Offline Phibedy

Senior Member


Medals: 8



« Reply #11 - Posted 2014-04-05 20:31:54 »

It took me 1.3 hours to find it, I love debugging. Indeed I just hate to not know the solution  Roll Eyes
So I will go for a quizz  Grin
(No that's not because I am evil, that's just how some other guy helped me and you learn most)
First hint:
You create an object but you don't use it every time you create it.


The Color object in the update() method of the RainbowSnow class? Moving that within the if statement gives no change.
Good job, next hint.
add this in Particle.java

1  
2  
3  
4  
5  
6  
7  
lastAlpha =(float)(currentLife/TOTAL_LIFE); //in your update method in Particle

if(lastAlpha != color.a){ //in your render method
        System.out.println(" DIFF: lastAlpha: " + lastAlpha+ " color.a "+ color.a);
      }else{
         System.out.println(" SAME: lastAlpha: " + lastAlpha+ " color.a "+ color.a);
      }
Offline BurntPizza
« Reply #12 - Posted 2014-04-05 20:35:21 »

No idea what a quad is, just started with libGDX a few hours ago.

May I direct you to the recent OGL tut by SHC outlining the basics of what goes on 'under the covers' of libGDX:
http://www.java-gaming.org/topics/lwjgl-tutorial-series-the-opengl-rendering-pipeline/32661/view.html
Offline tyeeeee1
« Reply #13 - Posted 2014-04-05 20:37:06 »

No idea what a quad is, just started with libGDX a few hours ago.

May I direct you to the recent OGL tut by SHC outlining the basics of what goes on 'under the covers' of libGDX:
http://www.java-gaming.org/topics/lwjgl-tutorial-series-the-opengl-rendering-pipeline/32661/view.html

Thanks, I'll check it out in a few minutes.
Offline Phibedy

Senior Member


Medals: 8



« Reply #14 - Posted 2014-04-05 20:42:00 »

hint 3: Your error is pure java

Your methods should look like this

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  
      private float lastAlpha;
   /**
    * Updates the particle's position, change in x, change in y,
    * remaining lifetime, and color.
    * @return Whether the particle is 'dead' or not.
    */

   public boolean update() {
      if(x > SCREEN_DIMENSIONS.width + 32 || x < -32 || y > SCREEN_DIMENSIONS.height + 32) {
         return true;
      } else {
         x += dx;
         y += dy;
         dx += GRAVITY_X;
         dy += GRAVITY_Y;
         currentLife--;
         lastAlpha = (float)(currentLife/TOTAL_LIFE);
         color.set(color.r, color.g, color.b, lastAlpha);
         return (currentLife <= 0 ? true : false);
      }
   }

   /**
    * Renders the particle to the screen.
    * @param G The shaperenderer object to render with.
    * @param IS_OVAL Whether or not to render the particle as an oval.
    */

   public void render(final ShapeRenderer SHAPE_RENDERER, final boolean IS_OVAL) {

      if(lastAlpha != color.a){
         System.out.println(" DIFF: lastAlpha: " + lastAlpha+ " color.a "+ color.a);
      }else{
         System.out.println(" SAME: lastAlpha: " + lastAlpha+ " color.a "+ color.a);
      }
     
      SHAPE_RENDERER.setColor(color);

      if(IS_OVAL) {
         SHAPE_RENDERER.circle((int)x-(SIZE / 2), (int)y-(SIZE / 2), SIZE, SIZE); //x-(size/2) & y-(size/2) make sure the particle is rendered at (x, y).
     } else {
         SHAPE_RENDERER.rect((int)x-(SIZE / 2), (int)y-(SIZE / 2), SIZE, SIZE); //x-(size/2) & y-(size/2) make sure the particle is rendered at (x, y).
     }
   }


hint 4: Why should the last calculated alpha be different from color.alpha?
Offline tyeeeee1
« Reply #15 - Posted 2014-04-05 20:47:45 »

Had it slightly different. Yeah, the values are always the same when I change it to how you've written it. Still looks like it's working properly.

Offline Phibedy

Senior Member


Medals: 8



« Reply #16 - Posted 2014-04-05 20:51:03 »

Yours should look like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
 DIFF: lastAlpha: 0.9939099 color.a 0.9889625
 DIFF: lastAlpha: 0.9925706 color.a 0.9889625
 DIFF: lastAlpha: 0.9736842 color.a 0.9889625
 DIFF: lastAlpha: 0.9019608 color.a 0.9889625
 DIFF: lastAlpha: 0.97652584 color.a 0.9889625
 DIFF: lastAlpha: 0.875 color.a 0.9889625
 DIFF: lastAlpha: 0.99233127 color.a 0.9889625
 DIFF: lastAlpha: 0.98316497 color.a 0.9889625
 DIFF: lastAlpha: 0.96428573 color.a 0.9889625
 DIFF: lastAlpha: 0.954955 color.a 0.9889625
 DIFF: lastAlpha: 0.9904398 color.a 0.9889625
 DIFF: lastAlpha: 0.99360615 color.a 0.9889625
 DIFF: lastAlpha: 0.9934124 color.a 0.9889625
 DIFF: lastAlpha: 0.99291784 color.a 0.9889625
 SAME: lastAlpha: 0.9889625 color.a 0.9889625


if your snowflakes work properly it should look like:

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  
 SAME: lastAlpha: 0.9868735 color.a 0.9868735
 SAME: lastAlpha: 0.97713095 color.a 0.97713095
 SAME: lastAlpha: 0.9790476 color.a 0.9790476
 SAME: lastAlpha: 0.97393364 color.a 0.97393364
 SAME: lastAlpha: 0.98256737 color.a 0.98256737
 SAME: lastAlpha: 0.92142856 color.a 0.92142856
 SAME: lastAlpha: 0.98253965 color.a 0.98253965
 SAME: lastAlpha: 0.978 color.a 0.978
 SAME: lastAlpha: 0.98509485 color.a 0.98509485
 SAME: lastAlpha: 0.9586466 color.a 0.9586466
 SAME: lastAlpha: 0.97951585 color.a 0.97951585
 SAME: lastAlpha: 0.9859155 color.a 0.9859155
 SAME: lastAlpha: 0.973301 color.a 0.973301
 SAME: lastAlpha: 0.8513514 color.a 0.8513514
 SAME: lastAlpha: 0.95849055 color.a 0.95849055
 SAME: lastAlpha: 0.9763441 color.a 0.9763441
 SAME: lastAlpha: 0.9691877 color.a 0.9691877
 SAME: lastAlpha: 0.7708333 color.a 0.7708333
 SAME: lastAlpha: 0.98286605 color.a 0.98286605
 SAME: lastAlpha: 0.97759676 color.a 0.97759676
 SAME: lastAlpha: 0.97577095 color.a 0.97577095
 SAME: lastAlpha: 0.98214287 color.a 0.98214287
 SAME: lastAlpha: 0.98651963 color.a 0.98651963
 SAME: lastAlpha: 0.7924528 color.a 0.7924528
 SAME: lastAlpha: 0.9822581 color.a 0.9822581
 SAME: lastAlpha: 0.98073554 color.a 0.98073554
 SAME: lastAlpha: 0.98677886 color.a 0.98677886
 SAME: lastAlpha: 0.9808028 color.a 0.9808028
 SAME: lastAlpha: 0.9740566 color.a 0.9740566
 SAME: lastAlpha: 0.97736627 color.a 0.97736627
 SAME: lastAlpha: 0.9832317 color.a 0.9832317
 SAME: lastAlpha: 0.97002727 color.a 0.97002727
 SAME: lastAlpha: 0.976087 color.a 0.976087
 SAME: lastAlpha: 0.959854 color.a 0.959854
 SAME: lastAlpha: 0.9860936 color.a 0.9860936
 SAME: lastAlpha: 0.98410404 color.a 0.98410404
....

So there should be something wrong with your color.
Offline tyeeeee1
« Reply #17 - Posted 2014-04-05 20:54:00 »

I have the code modified to test with a single snowflake on the screen. The numbers just reduce down to almost 0.0, and before it can get to 0.0 it's removed just as it should be.

http://pastebin.com/WsGXAp4K

I assume that you're testing with the unmodified code which would have a ton of snowflakes falling at once and not show the numbers going straight down.
Offline Phibedy

Senior Member


Medals: 8



« Reply #18 - Posted 2014-04-05 20:56:39 »

Yes but the code is working.
Just concentrate on the update-method in rainbowSnow  Smiley
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
       Color c = new Color();
       c.set(red/255f, green/255f, blue/255f, 1.0f);
      if(counter == 10) {
         for(int i=0;i<RANDOM.nextInt(1800) + 120;i++) {
            newParticle(c, RANDOM.nextInt(8), 40);
            }
         counter = 0;
      } else {
         counter++;
      }

So, how many particles do I get if counter == 10? How many colors do those particles have?
Offline tyeeeee1
« Reply #19 - Posted 2014-04-05 21:03:59 »

You should have between 120 to 1920 particles every time the counter hits 10. They all use the same color object so 1 color.

Annnd after a second I thought that since they were all referring to the same color object then I just changed it to create a new color object for every single particle and it was fixed. Never had this bug with the non-libGDX version.
Offline Phibedy

Senior Member


Medals: 8



« Reply #20 - Posted 2014-04-05 21:08:52 »

Yes it's because in java2d if you had 471 particles you had 471 colors.

Back to your code:
So what would happen if you create 5 red particles.
Then you take one particle and set the color to blue. What color will the 4 other particles have? You use color.set(...)
Offline Phibedy

Senior Member


Medals: 8



« Reply #21 - Posted 2014-04-05 21:13:42 »

Yes you got it Smiley

I would change:
In Particle.class
1  
2  
3  
4  
5  
   /** The color of the particle. */
   private final Color color = new Color();

and in the constructor:
      color.set(COLOR);


in RainbowSnow.class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   private final Color snowColor = new Color();
   /**
    * Updates the snow.
    */

   public void update() {
     snowColor.set(red/255f, green/255f, blue/255f, 1.0f);
      if(counter == 10) {
         for(int i=0;i<RANDOM.nextInt(1800) + 120;i++) {
            newParticle(snowColor, RANDOM.nextInt(8), 40);
            }
         counter = 0;
      } else {
         counter++;
      }
...


Are you always writing so many comments in your code? That's nice Smiley
Offline matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #22 - Posted 2014-04-05 21:18:19 »

Immutability for the win!

Should all functional languages rise! Grin

(This is probably a dangerous statement in a java forum  persecutioncomplex ... Anyways, go on, I didn't want to derail Roll Eyes )

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline tyeeeee1
« Reply #23 - Posted 2014-04-05 21:27:27 »

Thanks for the help Phil. I have the code looking similar to what you put, but I kept it closer to my original way so that the transition looks better.

1  
2  
3  
4  
5  
6  
if(counter == 10) {
         for(int i=0;i<RANDOM.nextInt(1800) + 120;i++) { newParticle(new Color(red/255f, green/255f, blue/255f, 1.0f), RANDOM.nextInt(8) + 2, 40); }
         counter = 0;
      } else {
         counter++;
      }



Working for up to 200k particles, when I hit 300k it slows down to a snails crawl.
Offline Phibedy

Senior Member


Medals: 8



« Reply #24 - Posted 2014-04-05 21:36:03 »

Thanks for the help Phil. I have the code looking similar to what you put, but I kept it closer to my original way so that the transition looks better.
Working for up to 200k particles, when I hit 300k it slows down to a snails crawl.
No problem  Smiley

Immutability for the win!

Should all functional languages rise! Grin

(This is probably a dangerous statement in a java forum  persecutioncomplex ... Anyways, go on, I didn't want to derail Roll Eyes )
The quizz is done, the participant won, the quiz-(not even close to master) has to go  Roll Eyes

But anyways, an Immutability-modifier would be awesome in java.
1  
private immutable Object o; //if you try to change a value inside o an ImmutableExeption will be thrown
Offline matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #25 - Posted 2014-04-05 21:43:32 »

But anyways, an Immutability-modifier would be awesome in java.
1  
private immutable Object o; //if you try to change a value inside o an ImmutableExeption will be thrown


It's not that easy... There is reflection too and stuff like that. It's possible to change the
char[]
inside a String via reflection, although the String should actually be immutable.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline BurntPizza
« Reply #26 - Posted 2014-04-05 21:47:03 »

It's possible to change the
char[]
inside a String via reflection, although the String should actually be immutable.

Isn't that particularly nasty since the strings are interned, any other string 'objects' that have (had) the same text as the one you change will also change? I may be wrong, been a while since I messed around with interning strings.
Offline Phibedy

Senior Member


Medals: 8



« Reply #27 - Posted 2014-04-06 12:43:12 »

But anyways, an Immutability-modifier would be awesome in java.
1  
private immutable Object o; //if you try to change a value inside o an ImmutableExeption will be thrown


It's not that easy... There is reflection too and stuff like that. It's possible to change the
char[]
inside a String via reflection, although the String should actually be immutable.
It doesn't have to throw an exception if you use reflection.
it just should throw an exception if you do something like

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
class Whatever{
String myString;

immutable Whatever w; //if you try to change a value inside o an ImmutableExeption will be thrown


void test(){
 w.myString = "wasd"; //throw exception
w.setString("wasd2");

}

void setString(String s){
 this.myString = s; //throw exception
}
}
Offline matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #28 - Posted 2014-04-06 20:30:08 »

So what you describe is almost what
final
does, but you want it to recursively mark all fields of that method as final...

1. It doesn't need to be an exception. It would be much better if it was a compiler error. Or else you might not notice your error it until some of your users has a PC that makes your code jump into some strange if-branch and then fail. The compiler can immediately see if you tried to change something from an Object that was marked to be
immutable
, just like it's with
final
.

2. I don't think this would be too useful. There is even a problem with My first point, that I just noticed Wink ... Let's take LibGDX's Color class as an example. If you'd mark it as immutable (and you had the compiler error method that I described in point 1) it wouldn't compile, because there'd be a compiler error with the
Color.set()
method... Therfore it would make sense to use Exceptions, yeah...

Hrm...

I don't like the idea of adding more runtime errors, though. I think the future lies in compilers that find out that your program is wrong before it even executed once. They find the bugs for you easily, because the coder is constrained in what he can do so that It's easy for the compiler to spot bugs. That's exactly why compile-time type-checkers were invented. They didn't want you to accidently use your "Color" as String, because that'd be impossible. They constrain us, though. We can't simply cast a "Vector3f" into a "Color", though, even though in C it would work...


See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline BurntPizza
« Reply #29 - Posted 2014-04-06 20:40:43 »

They didn't want you to accidently use your "Color" as String, because that'd be impossible. They constrain us, though. We can't simply cast a "Vector3f" into a "Color", though, even though in C it would work...

So then you fight back and use typeclasses and other interesting magic, make everything immutable, safer, and suddenly Haskell.

Quote
I think the future lies in compilers that find out that your program is wrong before it even executed once.

It does seem to be the way things are progressing. It has a certain elegance to it that I feel is quite attractive to us programmers, but so far is also quite elusive.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (26 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (71 views)
2014-09-10 06:39:09

Tekkerue (36 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (41 views)
2014-09-07 01:13:42

Longarmx (27 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (33 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!