-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|676|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Snippet Home -> AI and Movement


 
melmantheman
Created : 31 October 2011
Edited : 31 October 2011
System : Cross Platform
Language : Ruby

Moving particles in java

How to move particles in java. here is a class for it. just copy and paste

//Erik Decker
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.*;
/**
* Used to make a particle effect with variating rates, gravity, and motions
* Made by Erik Decker. Posted on socoder.net
* Hope you like it!
*/
public class Particle
{
double[][] particles;
private double rate;
private double gravity;
private double type;
private double width;
/**
* Makes a new set of particles
* @param p the number of particles
* @param r the speed at which they move (l/r)
* @param grav the speed at which they fall
* @param t the type of particle. Determines which equation to use. 0 = normal 1 = snow, 2 = dust, 3 = ash
* @param w the max x value of the particle
*/
public Particle(int p, int r, int grav, int t,int w)
{
particles = new double[p][3];
rate = r;
gravity = grav;
type = t;
makeParticles();
}

private void makeParticles()
{
Random r1 = new Random();
for(int x = 0;x< particles.length;x++)
{
particles[x][0] = r1.nextInt(500);
particles[x][1] = r1.nextInt(500);
particles[x][2] = r1.nextInt(500);
}
}

private void newPart(int x)
{
Random r1 = new Random();
//particles[x][0] = r1.nextInt(500);
particles[x][1] = r1.nextInt(500);
particles[x][2] = particles[x][1];//offset

}

/**
* moves Particles in designated direction
*/
public void moveParticles()
{
for(int x = 0; x < particles.length;x++)
{
if(type == 0)
{
particles[x][1] += gravity;
}
else if(type == 1)
{
particles[x][0] = ((20)*Math.sin(particles[x][1]))+particles[x][2];
//System.out.println(particles[x][1]);
particles[x][1] += gravity;
}
else if(type == 2)
{
particles[x][0] += rate;
particles[x][1] += gravity;
}
else if(type == 3)
{
particles[x][0] = ((20)*Math.sin(particles[x][1]))+particles[x][2];
particles[x][1] -= gravity;
}
//137
if(particles[x][1] > 500)
{
newPart(x);
}
}
}

/**
* Returns all particle x's and y's
* @return int[][] array of particle x's and y's
*/
public double[][] getParticles()
{
return particles;
}

/**
* change the rate of the particles to r
* @param r the new rate
*/
public void changeRate(int r)
{
rate = r;
}
}

 

Comments