Slick Forums
http://slick.ninjacave.com/forum/

Particle - oriented
http://slick.ninjacave.com/forum/viewtopic.php?f=1&t=8431
Page 1 of 1

Author:  Siggi05 [ Wed Jul 19, 2017 8:11 am ]
Post subject:  Particle - oriented

I've discovered a problem with particles.
I wanted to make a firy animation, which you can move around with dragging, like this:
Code:
public class MainClass extends BasicGame{

   ParticleSystem ps;
   ConfigurableEmitter c;
   Image particle;
   Input in;
   boolean pressed = false;
   
   public MainClass(String title) {
      super(title);
      // TODO Auto-generated constructor stub
   }

   public static void main(String[] args) throws SlickException {
      // TODO Auto-generated method stub
      AppGameContainer app = new AppGameContainer(new MainClass("Hi"));
      app.setDisplayMode(800, 600, false);
      app.start();
   }

   @Override
   public void render(GameContainer window, Graphics g) throws SlickException {
      // TODO Auto-generated method stub
      
      ps.render();
      if(pressed)
      {
         c.setPosition(in.getMouseX(), in.getMouseY(), false);
      }
   }

   @Override
   public void init(GameContainer window) throws SlickException {
      // TODO Auto-generated method stub
      try
      {
         particle = new Image("data/particle.png");
         ps = new ParticleSystem(particle, 750);
         File XmlFile = new File("data/test_emitter.xml");
         c = ParticleIO.loadEmitter(XmlFile);
         c.setPosition(0, 0);
         c.setEnabled(false);
         ps.addEmitter(c);
      }
      catch (Exception e)
      {
         System.out.println(e.getMessage());
      }
      ps.setBlendingMode(ParticleSystem.BLEND_ADDITIVE);
      ps.setRemoveCompletedEmitters(false);
      in = window.getInput();
   }
   
   public void mousePressed(int button, int x, int y)
   {
      pressed = true;
      c.length.setEnabled(false);
      c.setEnabled(true);
      c.reset();
   }
   
   public void mouseReleased(int button, int x, int y)
   {
      pressed = false;
      c.length.setEnabled(true);
   }

   @Override
   public void update(GameContainer window, int delta) throws SlickException {
      // TODO Auto-generated method stub
      ps.update(delta);
   }

}


Here is the problem:
I wanted to include little sparks into the animation, which fly from the center of my emitter away.
To find out which function I need for the rotation of the sparks, I took a look at the ConfigurbleEmitter page.
There it was:
Code:
c.useOriented = true

But the problem is, that the particles seemed to rotate around (0, 0).
That's the case, because the particle-class defines it's rotation as following:
Code:
public void render() {
      if ((engine.usePoints() && (usePoints == INHERIT_POINTS))
            || (usePoints == USE_POINTS)) {
         TextureImpl.bindNone();
         GL.glEnable(SGL.GL_POINT_SMOOTH);
         GL.glPointSize(size / 2);
         color.bind();
         GL.glBegin(SGL.GL_POINTS);
         GL.glVertex2f(x, y);
         GL.glEnd();
      } else if (oriented || scaleY != 1.0f) {
         GL.glPushMatrix();

         GL.glTranslatef(x, y, 0f);

         [b]if (oriented) {
            float angle = (float) (Math.atan2(y, x) * 180 / Math.PI);
            GL.glRotatef(angle, 0f, 0f, 1.0f);
         }[/b]

         // scale
         GL.glScalef(1.0f, scaleY, 1.0f);

         image.draw((int) (-(size / 2)), (int) (-(size / 2)), (int) size,
               (int) size, color);
         GL.glPopMatrix();
      } else {
         color.bind();
         image.drawEmbedded((int) (x - (size / 2)), (int) (y - (size / 2)),
               (int) size, (int) size);
      }
}

Could any of the developers please include a boolean which states whether to orient the particles at the ParticleSystem or at the Emitter in the particle class and copy the position functions of the ConfigurableEmitter into the Emitter class?
Code:
public void render() {
      if ((engine.usePoints() && (usePoints == INHERIT_POINTS))
            || (usePoints == USE_POINTS)) {
         TextureImpl.bindNone();
         GL.glEnable(SGL.GL_POINT_SMOOTH);
         GL.glPointSize(size / 2);
         color.bind();
         GL.glBegin(SGL.GL_POINTS);
         GL.glVertex2f(x, y);
         GL.glEnd();
      } else if (oriented || scaleY != 1.0f) {
         GL.glPushMatrix();

         GL.glTranslatef(x, y, 0f);

         [b]if (oriented) {
                                if (orientAtEmitter) {
                                    float angle = (float) (Math.atan2(y - emitter.getY(), x - emitter.getX()) * 180 / Math.PI);
                                }
                                else {
            float angle = (float) (Math.atan2(y, x) * 180 / Math.PI);
                                }
            GL.glRotatef(angle, 0f, 0f, 1.0f);
         }[/b]

         // scale
         GL.glScalef(1.0f, scaleY, 1.0f);

         image.draw((int) (-(size / 2)), (int) (-(size / 2)), (int) size,
               (int) size, color);
         GL.glPopMatrix();
      } else {
         color.bind();
         image.drawEmbedded((int) (x - (size / 2)), (int) (y - (size / 2)),
               (int) size, (int) size);
      }
}

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/