|
Particles are small 3D objects used together in large numbers and with a notable pattern to create effects like fire, smoke, fairy dust or asteroid fragments. But, For this project, I developed a radial particle engine that can simulates things exploding in space. I suppose if I update the radial particle engine to include gravity, I could simulate things exploding near large gravatational fields, like on Earth - but I didn't.
Anyhow, my particle engine is not very difficult to implement or understand. The code below should reveal everything.
Model mPlayer, mBullet, mRock, mRPart; // Update Line
Actor aPlayer;
Particle pRock; // New Lin
In the first line, mRPart is added as to the list of Models. This model is the small rock shards that break away from the rock when hit with a bullet. The third line assigns pRock to the Particle class. The particle class simple hold data for initialization of the particle engine.
private void makeRockParts(Actor act) // New Method
{ pRock.x =act.x;
pRock.y =act.y;
pRock.z =act.z;
pRock.xdr=act.model.maxRadius;
pRock.ydr=act.model.maxRadius;
pRock.zdr=act.model.maxRadius;
gEngine.addRadParticles(pRock);
makeRockParts() is the only new method. It initializes several variables in the pRock Particle class. Specifically, it sets the x, y and z location of the Particle engine to that of the Actor (a just destroyed rock) passed to this method. Each particle of the particle engine can start anywhere from the origin of the particle engine to its
maxRadius
. If this offset from the origin is not assigned, all particles would start from the center of the rock and the effect that the rock has broke is changed to an effect that the rock had no mass around the center! Finally, addRadParticles() is invoked, which starts the particle engine.
public void run()
{ RunAsteroids runAsteroids = new RunAsteroids();
gEngine = new GameEngine(getSize().width, getSize().height, getCodeBase());
add("Center", gEngine);
gEngine.models.add(mPlayer = new Model(getCodeBase() + "Ship.glo"));
gEngine.models.add(mBullet = new Model(getCodeBase() + "Bullet.glo"));
gEngine.models.add(mRock = new Model(getCodeBase() + "Rock1.glo"));
gEngine.models.add(mRPart = new Model(getCodeBase() + "Rock.glo")); // New Line
gEngine.glFonts.add(fntBasic = new GLFont(getCodeBase() + "Basic.glf"));
while(gEngine.modelCount<5) sleep(100); // Update Line
pRock = new Particle(mRPart, 32); // New Line
pRock.xs = pRock.ys = pRock.zs = 40f; // New Line
pRock.xrs = pRock.yrs = pRock.zrs = 5f; // New Line
pRock.die = 128; // New Lin
Several new lines are added to the run() method and one is updated. The first new line loads the small rock particles, mRPart. Since one extra model is loaded, the model count delay should be increased by one. The final five new lines initialize pRock. We do this here, instead of in makeRockParts(), because these values will never differ throughout the game. So, we can save a billionth of a second, or so, be initializing them here. The first assignment creates a new instance of the Particle class and store its reference in pRock. mRPart is the model that the particle engine will use and we want 32 particles (for bigger explosions try using a value like 64 or maybe 1000, or maybe not). The x, y and z speed is assigned to 40f. This is actually the maximum speed that a particle can randomly be initialized to. The x, y and z rotational speed is also set accordingly. die is set to 128, which limits the time that the particles exists.
|