Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  LWJGL doesn't draw my Particles  (Read 574 times)
0 Members and 1 Guest are viewing this topic.
Offline penguin

Senior Newbie





« Posted 2011-04-06 13:55:12 »

I've played a bit with particles and wrote my own particle system.
I had it working and changed somethings and now it won't work anymore.

I've searched a long time for the wrong code, but i didn't find anything.

Here's my code:

ParticleEmitter.java
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  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
package com.iceengine.particles;

/**
 *
 * @author penguin
 */

import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.Color;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import com.iceengine.j3d.objects.IObject;

public class ParticleEmitter implements IObject{
    private Vector3f position;
    private Vector3f Rotation;
    private int particleLifetime;
    private Color myColor;
    private Particle[] Particles;
    private float size;
    private Vector3f moveTo;
    private boolean GenerateLifetime;
    private boolean repeat;

    public void drawObject(Vector3f theTranslation, Vector3f rotation)
    {
        GL11.glTranslatef(theTranslation.x, theTranslation.y, theTranslation.z);
        GL11.glRotatef(rotation.x + Rotation.x, 1.0f, 0.0f, 0.0f);
        GL11.glRotatef(rotation.y + Rotation.y, 0.0f, 1.0f, 0.0f);
        GL11.glRotatef(rotation.z + Rotation.z, 0.0f, 0.0f, 1.0f);

        Update();
        drawParticles();
    }
    public void rotateX(float angle)
    {
        Rotation.x = angle;
    }
    public void rotateY(float angle)
    {
        Rotation.y = angle;
    }
    public void rotateZ(float angle)
    {
        Rotation.z = angle;
    }
    public ParticleEmitter(Vector3f Position,Vector3f MoveTo, Color color, int lifetime, int particles, float Size, boolean generateLifetime, boolean Repeat)
    {
        repeat = Repeat;
        GenerateLifetime = generateLifetime;
        moveTo = MoveTo;
        size = Size;
        position = Position;
        myColor = color;
        particleLifetime = lifetime;

        Rotation = new Vector3f(0,0,0);

        Particles = new Particle[particles];


        Random generator = new Random();

        for(int i = 0; i < particles; i++)
        {
            Particles[i] = createParticle();
        }
    }
    public void Update()
    {
        for(int i = 0; i < Particles.length; i++)
        {
            Particles[i].Update();
            if(!Particles[i].isParticleAlive())
            {
                if(repeat)
                Particles[i] = createParticle();
            }
        }
    }
    private Particle createParticle()
    {
        float V = (float)(Math.random() * 25);
        float angle = (float)(Math.random() * 360);
        Vector3f velocity = new Vector3f();

        velocity.x = (float)Math.sin(angle) * V;// * moveTo.x;
        velocity.y = (float)Math.cos(angle) * V;// * moveTo.y;
        velocity.z = (((float)Math.random() * 10) - 5) / 10 * V;// * moveTo.z;//generator.nextFloat() % 10)-5) / 10 * V;
        //System.out.println("NEW " + " " + position.x + " " + position.y + " " + position.z + " " + angle + " " + V + " " + velocity.x + " " + velocity.y + " " + velocity.z);
        int life = 0;
        if(GenerateLifetime)
        {
            life = (int)(Math.random() * particleLifetime);
        } else {
            life = particleLifetime;
        }

        return new Particle(size,life, new Vector3f(position.x, position.y, position.z),velocity, myColor);
    }
    public void drawParticles()
    {
        for(int i = 0; i < Particles.length; i++)
        {
            //System.out.println("DRAW " + i + " - " + Particles[i].position.x + " - " + Particles[i].position.y + " - " + Particles[i].position.z + " - " + Particles[i].velocity.x + " - " + Particles[i].velocity.y + " - " + Particles[i].velocity.z + " - " + (System.currentTimeMillis() - Particles[i].startTime));
            Particles[i].drawParticle();
        }
    }

    public boolean isWorkDone()
    {
        boolean workDone = false;
        for(int i = 0; i < Particles.length; i++)
        {
            if(!Particles[i].isParticleAlive())
                workDone = true;
        }
        return workDone;
    }
}


Particle.java
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  
package com.iceengine.particles;

/**
 *
 * @author penguin
 */

import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.Color;
import org.lwjgl.opengl.GL11;

public class Particle {
    public Vector3f position;
    private int lifetime;
    public Vector3f velocity;
    private Color myColor;
    public long startTime;
    private boolean alive;
    private float size;
    public Particle(float Size,int Lifetime, Vector3f Position, Vector3f Velocity, Color color)
    {
        size = Size;
        position = Position;
        lifetime = Lifetime;
        myColor = color;
        velocity = Velocity;

        startTime = System.currentTimeMillis();

        alive = true;
    }

    public void Update()
    {
        // Update Particle
        if(alive)
        {
            if((System.currentTimeMillis() - startTime) <= lifetime)
            {
                position.x += velocity.x / 250;
                position.y += velocity.y / 250;
                position.z += velocity.z / 250;

                velocity.x *= 0.975f;
                velocity.y *= 0.975f;
                velocity.z *= 0.975f;
            } else {
                alive = false;
            }
        }
    }

    public void drawParticle()
    {
        /*
        GL11.glBegin(GL11.GL_TRIANGLE_STRIP);

        GL11.glColor3f(myColor.getRed(), myColor.getGreen(), myColor.getBlue());
        position.z = - 2.0f;
        GL11.glVertex3f(position.x+size, position.y+size, position.z);
        GL11.glVertex3f(position.x-size, position.y+size, position.z);
        GL11.glVertex3f(position.x+size, position.y-size, position.z);
        GL11.glVertex3f(position.x-size, position.y-size, position.z);

        GL11.glEnd();
        */

       
        if (alive)
        {
            GL11.glBegin(GL11.GL_POINT);
                GL11.glColor3f(myColor.getRed(), myColor.getGreen(), myColor.getBlue());
                GL11.glVertex3f(position.x, position.y, position.z);
                System.out.println("drawParticle() - " + position);
            GL11.glEnd();
        }
         
    }
    public boolean isParticleAlive()
    {
        return alive;
    }
}


Here's how i use the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
          ParticleEmitter theEmitter = new ParticleEmitter(new Vector3f(0,0,0), new Vector3f(1,1,0), new org.lwjgl.util.Color(org.lwjgl.util.Color.WHITE), 4000, 100, 1.0f, true, true);
         
          while(!IceEngine.isEngineCloseRequested()) // Same as Display.isCloseRequested();
          {
              org.lwjgl.opengl.GL11.glClear(org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT | org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT);
              org.lwjgl.opengl.GL11.glLoadIdentity();

              theEmitter.Update();
              theEmitter.drawParticles();
             
              IceEngine.update(); // Same as Display.update();
          }


I hope someone can help me Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (21 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (63 views)
2014-11-03 22:04:50

BurntPizza (61 views)
2014-11-03 18:54:52

moogie (76 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!