1
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 中有类似的解决方案。

4

1 回答 1

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;
}
于 2012-09-05T21:52:31.187 回答