typedef struct
{
float lifetime; // total lifetime of the particle
float decay; // decay speed of the particle
float r,g,b; // color values of the particle
float xpos,ypos,zpos; // position of the particle
float xspeed,yspeed,zspeed; // speed of the particle
boolean active; // is particle active or not?
} PARTICLE;
void CreateParticle(int i)
{
particle[i].lifetime= (float)random(500000)/500000.0;
particle[i].decay=0.001;
particle[i].r = 0.7;
particle[i].g = 0.7;
particle[i].b = 1.0;
particle[i].xpos= 0.0;
particle[i].ypos= 0.0;
particle[i].zpos= 0.0;
particle[i].xspeed = 0.0005-(float)random(100)/100000.0;
particle[i].yspeed = 0.01-(float)random(100)/100000.0;
particle[i].zspeed = 0.0005-(float)random(100)/100000.0;
particle[i].active = true;
}
我如何在Java中做这样的事情?我认为这看起来是一种很好的方式,我希望在Java中有类似的解决方案。
答案 0 :(得分:1)
您可以创建Particle
类:
import java.util.Random;
public class Particle {
// Data fields:
private double lifetime;
private double decay;
private double r, g, b;
private double xpos, ypos, zpos;
private double xspeed, yspeed, zspeed;
private boolean active;
// Constructor
public Particle() {
r = 0.7; g = 0.7; b = 1.0;
decay = 0.001;
Random rand = new Random();
lifetime = rand.nextInt(500000)/500000.0;
xspeed = 0.0005 - rand.nextInt(100)/100000.0;
yspeed = 0.01 - rand.nextInt(100)/100000.0;
zspeed = 0.0005 - rand.nextInt(100)/100000.0;
active = true;
// all others are 0 by default
}
}
您还可以轻松制作方法来返回数据字段的值,例如:
public double lifetime() {
return lifetime;
}