Advertisement
Guest User

Particles in Java

a guest
Aug 15th, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1. package rdlloyd;
  2. import java.awt.Frame;
  3. import java.awt.event.WindowAdapter;
  4. import java.awt.event.WindowEvent;
  5. import java.io.File;
  6. import java.util.Random;
  7. import org.lwjgl.LWJGLUtil;
  8. import org.lwjgl.opengl.Display;
  9. import org.newdawn.slick.BasicGame;
  10. import org.newdawn.slick.CanvasGameContainer;
  11. import org.newdawn.slick.Color;
  12. import org.newdawn.slick.GameContainer;
  13. import org.newdawn.slick.Graphics;
  14. import org.newdawn.slick.Input;
  15. import org.newdawn.slick.SlickException;
  16. import org.newdawn.slick.geom.Vector2f;
  17.  
  18. /**
  19.  * Simple particle stuff. It's hacky but it works :D
  20.  *
  21.  * @author Stefan Lange & Florian Valerius
  22.  * @version 1.0.0
  23.  * @since 15.08.2011
  24.  */
  25. public class ParticleAction extends BasicGame {
  26.    
  27.     private static int width = 800, height = 600;
  28.     private Random RND = new Random();
  29.    
  30.     private boolean h4c7orzBool = true, gravityRandom = false;
  31.    
  32.     private Particle particles[];
  33.    
  34.     private Vector2f temp = new Vector2f();
  35.    
  36.     private float minColor = 0.1f, maxColor = 0.6f, colorSpeed = 0.5f;
  37.     private int state = 0;
  38.     private Color loopyColorShit = new Color(maxColor, minColor, minColor, 1.0f);
  39.    
  40.     private int maxNum;
  41.    
  42.     public ParticleAction() {
  43.         super("AWESOME PARTICLE LOLOLOL");
  44.     }
  45.     public static void main(String[] args) throws SlickException {
  46.        
  47.         System.setProperty("org.lwjgl.librarypath",
  48.                 new File(new File(System.getProperty("user.dir"), "native"), LWJGLUtil.getPlatformName()).getAbsolutePath());
  49.         System.setProperty("net.java.games.input.librarypath", System.getProperty("org.lwjgl.librarypath"));
  50.        
  51.        
  52.         // enable resizing
  53.         CanvasGameContainer container = new CanvasGameContainer(new ParticleAction());
  54.        
  55.         Frame frame = new Frame("AWESOME PARTICLE LOLOLOL");
  56.         frame.setSize(800,600);
  57.         frame.add(container);
  58.         frame.addWindowListener(new WindowAdapter() {
  59.             public void windowClosing(WindowEvent e) {
  60.                 Display.destroy();
  61.                 System.exit(0);
  62.             }
  63.         });
  64.         frame.setVisible(true);
  65.         container.start();
  66.         container.requestFocus();
  67.        
  68.         // Old version with fixed window size
  69.         /*
  70.         AppGameContainer app = new AppGameContainer(new ParticleAction());
  71.         app.setDisplayMode(width, height, false);
  72.         app.start();
  73.         app.setClearEachFrame(false);
  74.         */
  75.     }
  76.    
  77.     // yes.
  78.     public void init(GameContainer container) throws SlickException {
  79.         particles = new Particle[100000];
  80.        
  81.         maxNum = 50000;
  82.        
  83.         for(int i = 0; i < particles.length; i++)
  84.             particles[i] = new Particle();
  85.         for(Particle p : particles) {
  86.             p.px = RND.nextFloat() * width;
  87.             p.py = RND.nextFloat() * height;
  88.            
  89.             p.vx = 0;
  90.             p.vy = 0;
  91.             p.color = loopyColorShit;
  92.         }  
  93.     }
  94.    
  95.     // stuff gets updated
  96.     public void update(GameContainer container, int delta) throws SlickException {
  97.         Input in = container.getInput();
  98.        
  99.         h4c7orzBool = true;
  100.         int pressState = 0;
  101.         if(in.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
  102.             pressState = 1;
  103.         else if(in.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
  104.             pressState = 2;
  105.         if(in.isKeyDown(Input.KEY_LEFT))
  106.             maxNum -= 1 * delta;
  107.         if(in.isKeyDown(Input.KEY_RIGHT))
  108.             maxNum += 1 * delta;
  109.         if(in.isKeyPressed(Input.KEY_S))
  110.             gravityRandom = !gravityRandom;
  111.         if(in.isKeyPressed(Input.KEY_ESCAPE)) {
  112.             Display.destroy();
  113.             System.exit(0);
  114.         }
  115.            
  116.         width = container.getWidth();
  117.         height = container.getHeight();
  118.        
  119.         System.out.println(width);
  120.        
  121.         if(maxNum < 100)
  122.             maxNum = 100;
  123.         if(maxNum > particles.length)
  124.             maxNum = particles.length;
  125.        
  126.         if(pressState == 1)
  127.             h4c7orzBool = false;
  128.        
  129.         cycleColor();
  130.        
  131.         for(int i = 0; i < maxNum; i++) {
  132.             particles[i].update(delta);
  133.             particles[i].color = loopyColorShit;
  134.            
  135.             if(pressState == 1) {
  136.                 suckHard( i, in, delta );
  137.                
  138.             }else if( pressState == 2 ) {
  139.                 suckHard( i, in, delta );
  140.                 spinHard( i, in, delta );
  141.                
  142.             }
  143.         }
  144.     }
  145.    
  146.     // goes trough min <-> max color values
  147.     private void cycleColor() {
  148.         switch (state) {
  149.             case 0:
  150.                 loopyColorShit.g += colorSpeed * 1.0f / 255f;
  151.                 if(loopyColorShit.g >= maxColor) {
  152.                     state = 1;
  153.                     loopyColorShit.g = maxColor;
  154.                 }
  155.                 break;
  156.             case 1:
  157.                 loopyColorShit.r -= colorSpeed * 1.0f / 255f;
  158.                 if(loopyColorShit.r <= minColor) {
  159.                     state = 2;
  160.                     loopyColorShit.r = minColor;
  161.                 }
  162.                 break;
  163.             case 2:
  164.                 loopyColorShit.b += colorSpeed * 1.0f / 255f;
  165.                 if(loopyColorShit.b >= maxColor) {
  166.                     state = 3;
  167.                     loopyColorShit.b = maxColor;
  168.                 }
  169.                 break;
  170.             case 3:
  171.                 loopyColorShit.g -= colorSpeed * 1.0f / 255f;
  172.                 if(loopyColorShit.g <= minColor) {
  173.                     state = 4;
  174.                     loopyColorShit.g = minColor;
  175.                 }
  176.                 break;
  177.             case 4:
  178.                 loopyColorShit.r += colorSpeed * 1.0f / 255f;
  179.                 if(loopyColorShit.r >= maxColor) {
  180.                     state = 5;
  181.                     loopyColorShit.r = maxColor;
  182.                 }
  183.                 break;
  184.             case 5:
  185.                 loopyColorShit.b -= colorSpeed * 1.0f / 255f;
  186.                 if(loopyColorShit.b <= minColor) {
  187.                     state = 0;
  188.                     loopyColorShit.b = minColor;
  189.                 }
  190.                 break;
  191.         }
  192.     }
  193.    
  194.     // sucks the dots to the mouse bro
  195.     private void suckHard( int i, Input in, int delta ) {
  196.        
  197.         temp.x = (in.getMouseX() - particles[i].px);
  198.         temp.y = (in.getMouseY() - particles[i].py);
  199.         temp.normalise();
  200.        
  201.         if(gravityRandom) {
  202.             particles[i].vx += temp.x * delta * 0.01f * Math.random();
  203.             particles[i].vy += temp.y * delta * 0.01f * Math.random();
  204.         }else {
  205.             particles[i].vx += temp.x * delta * 0.01f;
  206.             particles[i].vy += temp.y * delta * 0.01f;
  207.         }
  208.     }
  209.    
  210.     // spins the dots around you!
  211.     private void spinHard( int i, Input in, int delta ) {
  212.        
  213.         temp.x = (in.getMouseX() - particles[i].px);
  214.         temp.y = (in.getMouseY() - particles[i].py);
  215.         temp.normalise();
  216.         temp = temp.getPerpendicular();
  217.        
  218.         if(gravityRandom) {
  219.             particles[i].vx += temp.x * delta * 0.004f * Math.random();
  220.             particles[i].vy += temp.y * delta * 0.004f * Math.random();
  221.         }else {
  222.             particles[i].vx += temp.x * delta * 0.004f;
  223.             particles[i].vy += temp.y * delta * 0.004f;
  224.         }
  225.     }
  226.    
  227.     // stuff gets rendered
  228.     public void render(GameContainer container, Graphics g) throws SlickException {
  229.        
  230.         g.setDrawMode(Graphics.MODE_ADD);
  231.         for(int i = 0; i < maxNum; i++)
  232.             particles[i].render(g);
  233.         g.setDrawMode(Graphics.MODE_NORMAL);
  234.        
  235.         g.setColor(Color.white);
  236.         g.drawString("Number of Particles: " + maxNum, 10, 25);
  237.         g.drawString("-> De-/Increase with Left/Right", 10, 40);
  238.         g.drawString("Random Gravitation(S): " + gravityRandom, 10, 55);
  239.     }
  240.    
  241.     // Single Particle
  242.     private class Particle {
  243.         public float px, py, vx, vy, opx, opy;
  244.         public Color color;                         // REF HOLDER
  245.        
  246.         public Particle() {}
  247.  
  248.         public void update(int delta) {    
  249.             opx = px;
  250.             opy = py;
  251.  
  252.             px += vx * delta * 0.1f;
  253.             py += vy * delta * 0.1f;
  254.            
  255.             if(h4c7orzBool) {
  256.                 vx -= vx * delta * 0.0003f;
  257.                 vy -= vy * delta * 0.0003f;
  258.             }else {
  259.                 vx -= vx * delta * 0.0001f;
  260.                 vy -= vy * delta * 0.0001f;
  261.             }
  262.             if( px < 0.0f && vx < 0.0f ) {
  263.                 px = 0.0f;
  264.                 vx *= -0.7f;
  265.             }else
  266.             if( px > width && vx > 0.0f ) {
  267.                 px = width;
  268.                 vx *= -0.7f;
  269.             }
  270.             if( py < 0.0f && vy < 0.0f ) {
  271.                 py = 0.0f;
  272.                 vy *= -0.7f;
  273.             }else
  274.             if( py > height && vy > 0.0f ) {
  275.                 py = height;
  276.                 vy *= -0.7f;
  277.             }
  278.         }
  279.        
  280.         public void render(Graphics g) {
  281.             g.setColor(color);
  282.             // this makes the dots disappear when slowing down, since the line is not even a pixel long and stuff
  283.             g.drawGradientLine(px, py, color,opx, opy, new Color(0, 0, 0, 0.2f));
  284.         }
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement