Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 430 times)
0 Members and 1 Guest are viewing this topic.
Offline penguin

Senior Newbie





« Posted 2011-04-06 15: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (77 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (121 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (179 views)
2013-05-12 17:10:36

kutucuk (179 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.39 seconds with 20 queries.