Guest User

Particle.java

a guest
Aug 13th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. package elec0.particleEngine;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4.  
  5. import java.util.Random;
  6.  
  7. import org.newdawn.slick.Color;
  8.  
  9. import elec0.utils.Timing;
  10. import elec0.utils.Vector3f;
  11.  
  12. public class Particle
  13. {
  14. // Everything can be public because this class shouldn't ever be used outside of a ParticleEmitter, which might need to change things after initilization
  15. public Vector3f position, moveVector;
  16. public float speed, lifetime;
  17. public Color startcolor1, startcolor2, startcolor3; // Starting and ending colors, transition through life of particle
  18. public Color endcolor1, endcolor2, endcolor3;
  19. public long birthTime;
  20. public String behavior;
  21.  
  22. private Behaviors enumBehaviors;
  23. public float size = 1;
  24.  
  25. // Constructors
  26. Particle(Vector3f position)
  27. {
  28. this.position = position;
  29. this.birthTime = Timing.getTime();
  30. this.behavior = "default";
  31. // this.position.setY(size);
  32. initParticle();
  33. }
  34. Particle(Vector3f position, Color startcolor1)
  35. {
  36. this.position = position;
  37. this.startcolor1 = startcolor1;
  38. this.birthTime = Timing.getTime();
  39. this.behavior = "default";
  40. // this.position.setY(size);
  41. initParticle();
  42. }
  43. Particle(Vector3f position, Color startcolor1, String behavior)
  44. {
  45. // this.position = new Vector3f(0, 0, 0);
  46. this.position = position;
  47. this.startcolor1 = startcolor1;
  48. this.birthTime = Timing.getTime();
  49. this.behavior = behavior;
  50. // this.position.setY(size);
  51. initParticle();
  52. }
  53. // End constructors
  54.  
  55. private void initParticle()
  56. {
  57. Random random = new Random();
  58. enumBehaviors = Behaviors.valueOf(behavior.toUpperCase());
  59.  
  60. switch(enumBehaviors)
  61. {
  62. case FIRE: // Uses three colors
  63.  
  64. break;
  65.  
  66. case DIAMOND: // Default (atm) with diamonds instead of squares and no texture
  67.  
  68. // Get a random spread and direction
  69. float diSpread =(float)(random.nextInt(100))/200.0f;
  70. float diAngle =(float)(random.nextInt(157))/10.0f; // Quarter circle
  71.  
  72. float dixMove = (float)(Math.cos(diAngle)*diSpread)/5;
  73. float diyMove = (float)(Math.sin(diAngle)*diSpread)/5;
  74. // Calculate X and Z vectors
  75. moveVector = new Vector3f(dixMove, 0, diyMove);
  76.  
  77. // Get a random initial speed
  78. moveVector.setY(random.nextInt(500)/1000f);
  79.  
  80. // Get a random life and 'yellowness'
  81. lifetime = random.nextFloat()*10000;
  82. break;
  83.  
  84. case DEFAULT: // Uses one color, random spread and speed, affected by gravity, lifespan 1-10 seconds
  85.  
  86. // Get a random spread and direction
  87. float dSpread=(float)(random.nextInt(100))/200.0f;
  88. float dAngle=(float)(random.nextInt(157))/10.0f; // Quarter circle
  89.  
  90. float dxMove = (float)(Math.cos(dAngle)*dSpread)/5;
  91. float dyMove = (float)(Math.sin(dAngle)*dSpread)/5;
  92. // Calculate X and Z vectors
  93. moveVector = new Vector3f(dxMove, 0, dyMove);
  94.  
  95. // Get a random initial speed
  96. moveVector.setY(random.nextInt(500)/1000f);
  97.  
  98. // Get a random life and 'yellowness'
  99. lifetime = random.nextFloat()*10000;
  100.  
  101. break;
  102. }
  103. }
  104.  
  105. // Pass this any non-null Colors you want it to change the internal variables for
  106. public void setColors(Color sc1, Color sc2, Color sc3, Color ec1, Color ec2, Color ec3)
  107. {
  108. if(sc1 != null)
  109. startcolor1 = sc1;
  110. if(sc2 != null)
  111. startcolor2 = sc2;
  112. if(sc3 != null)
  113. startcolor3 = sc3;
  114.  
  115. if(ec1 != null)
  116. endcolor1 = ec1;
  117. if(ec2 != null)
  118. endcolor2 = ec2;
  119. if(ec3 != null)
  120. endcolor3 = ec3;
  121. }
  122.  
  123. public void draw()
  124. {
  125. glPushMatrix();
  126.  
  127. switch(enumBehaviors)
  128. {
  129. case DIAMOND:
  130. glColor4f(startcolor1.r, startcolor1.g, startcolor1.b, startcolor1.a); // Color the particle
  131.  
  132. glDisable(GL_TEXTURE_2D);
  133.  
  134. // Place the quad and rotate to face the viewer
  135. glTranslatef(position.getX(), position.getY(), position.getZ());
  136.  
  137. //glRotatef(-ViewYaw,0.0f,1.0f,0.0f);
  138. //glRotatef(-ViewPitch,1.0f,0.0f,0.0f);
  139.  
  140. glBegin(GL_QUADS);
  141. glTexCoord2f(1.0f,0.0f); glVertex3f(0, 0, 0.0f);
  142. glTexCoord2f(0.0f,0.0f); glVertex3f(-size/2, size/2, 0.0f);
  143. glTexCoord2f(0.0f,1.0f); glVertex3f(0, size, 0.0f);
  144. glTexCoord2f(1.0f,1.0f); glVertex3f(size/2, size/2, 0.0f);
  145. glEnd();
  146.  
  147. break;
  148. case DEFAULT:
  149. glColor4f(startcolor1.r, startcolor1.g, startcolor1.b, startcolor1.a); // Color the particle
  150.  
  151. glEnable(GL_TEXTURE_2D);
  152.  
  153. // Place the quad and rotate to face the viewer
  154. glTranslatef(position.getX(), position.getY(), position.getZ());
  155.  
  156. //glRotatef(-ViewYaw,0.0f,1.0f,0.0f);
  157. //glRotatef(-ViewPitch,1.0f,0.0f,0.0f);
  158.  
  159. glBegin(GL_QUADS);
  160. glTexCoord2f(1.0f,0.0f); glVertex3f(size, size,0.0f);
  161. glTexCoord2f(0.0f,0.0f); glVertex3f(-size, size,0.0f);
  162. glTexCoord2f(0.0f,1.0f); glVertex3f(-size,-size,0.0f);
  163. glTexCoord2f(1.0f,1.0f); glVertex3f(size,-size,0.0f);
  164. glEnd();
  165. break;
  166. }
  167.  
  168.  
  169. glPopMatrix();
  170. }
  171.  
  172.  
  173. private enum Behaviors
  174. {
  175. DEFAULT,
  176. FIRE,
  177. DIAMOND,
  178. SQUARE
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment