This article is an example of Android game development Learning ② Fireworks bloom effect implementation method. Share to everyone for your reference. Specifically as follows:
This section describes the mathematical physics applications that are commonly used in game development-particle systems. Particle systems are similar to the ones in the previous section, which simulate the trajectory of objects in the objective world by means of mathematical methods and physical formulas. The difference is that the ball more emphasis on individual movement, and fireworks particles such as particle systems more attention to the overall feeling.
First, the fireworks particle effect
1. Particle object class particle class and particle collection class Particleset class
Each particle is an object of a particle class, and all particle objects produced in the program are managed by a Particleset object.
Particle class:
Package com.particle;
public class Particle {
int color;//particle color
int r;//particle radius
double vertical_v;//Vertical speed
double horizontal_v; Horizontal velocity
int startx;//initial x coordinate
int starty;//initial y coordinate int
x;//real time x coordinate
int y;//Real time y-coordinate
double starttime; /Starting Time public
particle (int color, int r, double vertical_v, double horizontal_v, int x, int y, double starttime) {
super ();
This.color = color;
THIS.R = R;
This.vertical_v = Vertical_v;
This.horizontal_v = Horizontal_v;
This.startx = x;
This.starty = y;
this.x = x;
This.y = y;
This.starttime = StartTime;
}
Particleset class:
Package com.particle;
Import java.util.ArrayList;
Import Android.graphics.Color;
public class Particleset {arraylist<particle> particleset;
Public Particleset () {particleset = new arraylist<particle> (); /** * Adds a specified number of particle objects to the collection of particles */public void Add (int count, double starttime) {for (int i = 0; i < Coun T
i++) {int tempcolor = This.getcolor (i); int TEMPR = 1; Particle radius Double tempv_v = -30 + ten * (Math.random ()); A velocity double tempv_h = 10-20 * (Math.random ()) that randomly produces the vertical direction of the particle;
velocity int tempx = 160 randomly producing particle horizontal direction; int tempy = (int) (100-10 * (Math.random ())); Randomly generated y-coordinates of particles, between 90 and 100 particle particle = new particle (Tempcolor, TEMPR, Tempv_v, Tempv_h, tempx, Tempy,
StartTime);
Particleset.add (particle);
}/** * Gets the color of the specified index/public int getcolor (int i) {int i) {Int. color = color.red;
Switch (i%4) {case 0:color = color.red;
BreakCase 1:color = Color.green;
Break
Case 2:color = Color.yellow;
Break
Case 3:color = Color.gray;
Break
return color;
}
}
The resulting particles have a vertical initial velocity of 30 to 20, a direction upward, a horizontal initial velocity of 10 to 10, and a direction to the left or right.
2. Physics engine Particlethread Class
Package com.particle;
Import java.util.ArrayList;
public class Particlethread extends Thread {boolean flag;
Particleview father;
int sleepspan = 80; Double time = 0; Time axis of the physical engine double span = 0.15;
The interval public particlethread (Particleview father) {This.father = father is used for each time the particle displacement is calculated;
This.flag = true; @Override public void Run () {while (flag) {Father.ps.add (5, time);//Add 5 particles each arraylist<p article> tempset = Father.ps.particleSet;
Gets the particle set for (int i = Tempset.size ()-1; I >= 0; i--) {Particle particle = tempset.get (i); Double TimeSpan = time-particle.starttime;
Calculates the time from the beginning of the program to the current elapsed int tempx = (int) (PARTICLE.STARTX + particle.horizontal_v * timeSpan);
int tempy = (int) (Particle.starty + 4.9 * TimeSpan * timeSpan + particle.vertical_v * timeSpan);
if (Tempy > Particleview.die_out_line) {//If the particle exceeds the bottom edge of the screen tempset.remove (particle);
} particle.x = tempx;
Particle.y = Tempy;
} time = span;
try {thread.sleep (Sleepspan);
catch (Exception e) {e.printstacktrace ();
}
}
}
}
The physical engine in this example does not use the way to get the system time, but instead defines a timeline (member variable time). This allows you to determine how quickly the timeline travels (by changing the value of a member variable span) without having to rely on the system's time.
3. View class Particleview Class
Package com.particle;
Import java.util.ArrayList;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.RectF;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceHolder.Callback;
Import Android.view.SurfaceView;
public class Particleview extends Surfaceview implements Callback {public static final int die_out_line = 300;
Drawthread DT;
Particleset PS;
Particlethread pt;
String fps = "fps:n/a";
Public Particleview {Super (context);
This.getholder (). Addcallback (this);
DT = new Drawthread (this, Getholder ());
PS = new Particleset ();
PT = new Particlethread (this); public void Dodraw (Canvas Canvas) {canvas.drawcolor (color.black);//clear screen arraylist<particle> particle
Set = Ps.particleset;
Paint Paint = new Paint (); for (int i = 0; i < particleset.size (); i++) {particle p = PArticleset.get (i);
Paint.setcolor (P.color);
int tempx = p.x;
int tempy = P.Y;
int Tempradius = P.R;
RECTF Oval = new RECTF (tempx, Tempy, TEMPX + 2 * tempradius, Tempy + 2 * tempradius); Canvas.drawoval (oval, paint);
Draw elliptic Particle} paint.setcolor (Color.White);
Paint.settextsize (18);
Paint.setantialias (TRUE);
Canvas.drawtext (fps, MB, paint); @Override public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {} @Override Publ
IC void surfacecreated (Surfaceholder arg0) {if (!dt.isalive ()) {Dt.start ();
} if (!pt.isalive ()) {Pt.start ();
@Override public void surfacedestroyed (Surfaceholder arg0) {Dt.flag = false;
DT = NULL;
Pt.flag = false;
PT = NULL;
}
}
4. Drawing class Drawthread and Activity class
Basically the same as the previous section
Drawthread class:
Package com.particle;
Import Android.graphics.Canvas;
Import Android.view.SurfaceHolder;
public class Drawthread extends Thread {particleview PV;
Surfaceholder Surfaceholder;
Boolean flag=false;
int sleepspan=30; Long start =system.nanotime (); Record the start time, which is used to compute the frame rate int count=0;
Record frame number public drawthread (Particleview pv,surfaceholder surfaceholder) {THIS.PV=PV;
This.surfaceholder=surfaceholder;
This.flag=true;
public void Run () {Canvas canvas=null; while (flag) {try {Canvas=surfaceholder.lockcanvas (null);//Get Ballview canvas synchronized (SURFACEH
Older) {Pv.dodraw (canvas);
} catch (Exception e) {e.printstacktrace ();
finally {if (canvas!=null) {surfaceholder.unlockcanvasandpost (canvas);//Surfaceholder unlock and pass the canvas back to
}} this.count++;
if (count==20) {//Full 20 frames, calculate the first frame rate count=0; Long TEMPSTAMP=SYSTEM.NAnotime ();
Long Span=tempstamp-start;
Start=tempstamp;
Double Fps=math.round (100000000000.0/span*20)/100.0;
pv.fps= "fps:" +FPS;
try {thread.sleep (Sleepspan);
catch (Interruptedexception e) {e.printstacktrace ();
}
}
}
}
Mainactivity class:
Package com.particle;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Window;
Import Android.view.WindowManager;
public class Mainactivity extends activity {
Particleview PV;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title); Setting does not display title
GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG _fullscreen); Set Full-screen
pv=new Particleview (this);
Setcontentview (PV);
}
Effect Chart:
Second, waterfall particle effect
The waterfall particles are very similar to the fireworks particles, both of which are motion with the initial velocity. The difference is that the horizontal and vertical velocity of the fireworks is not zero, while the waterfall particle is only the initial velocity in the horizontal direction and the initial velocity of the vertical direction is zero. Simply modify it in the Particleset class of the fireworks particle generation section.
The Particleset class Add method is modified as follows:
/**
* Adds a specified number of particle objects (waterfall particle effect) to the collection of particles */public
void add2 (int count, double starttime) {for
(int i = 0; i < Count i++) {
int tempcolor = This.getcolor (i);
int TEMPR = 1; Particle radius
Double tempv_v = 0;//the velocity of the particle's vertical direction is 0
double tempv_h = ten + M (Math.random ());//velocity of randomly generated particle horizontal direction
int te MpX = m;
int tempy = (int) (50-10 * (Math.random ())); Randomly generated y-coordinates of particles, between 90 and 100
particle particle = new particle (Tempcolor, TEMPR, Tempv_v, Tempv_h, tempx, Tempy,
StartTime);
Particleset.add (particle);
}
Effect Chart:
I hope this article will help you with your Android program.