Android游戏开发学习②焰火绽放效果实现方法
作者:w93223010 时间:2023-10-26 08:54:53
本文实例讲述了Android游戏开发学习②焰火绽放效果实现方法。分享给大家供大家参考。具体如下:
本节介绍在游戏开发中常用到的数学物理应用——粒子系统。粒子系统与上一节的小球有类似的地方,都是通过数学方法和物理公式模拟客观世界中的物体的运动轨迹。不同的是小球更强调个体运动,而焰火粒子等粒子系统更注重整体感觉。
一、焰火粒子效果
1.粒子对象类Particle类和粒子集合类ParticleSet类
每个粒子都为一个Particle类的对象,程序中产生的所有Particle对象都由一个ParticleSet对象来管理。
Particle类:
package com.particle;
public class Particle {
int color; // 粒子颜色
int r; // 粒子半径
double vertical_v; // 垂直速度
double horizontal_v; // 水平速度
int startX; // 初始X坐标
int startY; // 初始Y坐标
int x; // 实时X坐标
int y; // 实时Y坐标
double startTime; // 起始时间
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类:
package com.particle;
import java.util.ArrayList;
import android.graphics.Color;
public class ParticleSet {
ArrayList<Particle> particleSet;
public ParticleSet() {
particleSet = new ArrayList<Particle>();
}
/**
* 向粒子集合中添加指定数量的粒子对象
*/
public void add(int count, double startTime) {
for (int i = 0; i < count; i++) {
int tempColor = this.getColor(i);
int tempR = 1; // 粒子半径
double tempv_v = -30 + 10 * (Math.random()); // 随机产生粒子竖直方向的速度
double tempv_h = 10 - 20 * (Math.random()); // 随机产生粒子水平方向的速度
int tempX = 160;
int tempY = (int) (100 - 10 * (Math.random())); // 随机产生粒子Y坐标,90到100之间
Particle particle = new Particle(tempColor, tempR, tempv_v,
tempv_h, tempX, tempY, startTime);
particleSet.add(particle);
}
}
/**
* 获取指定索引的颜色
*/
public int getColor(int i) {
int color = Color.RED;
switch (i%4) {
case 0:
color = Color.RED;
break;
case 1:
color = Color.GREEN;
break;
case 2:
color = Color.YELLOW;
break;
case 3:
color = Color.GRAY;
break;
}
return color;
}
}
产生的粒子竖直初速度为-30至-20,方向向上;水平初速度为-10至10,方向向左或向右。
2.物理引擎ParticleThread类
package com.particle;
import java.util.ArrayList;
public class ParticleThread extends Thread {
boolean flag;
ParticleView father;
int sleepSpan = 80;
double time = 0; // 物理引擎的时间轴
double span = 0.15; // 每次计算粒子位移时采用的时间间隔
public ParticleThread(ParticleView father) {
this.father = father;
this.flag = true;
}
@Override
public void run() {
while (flag) {
father.ps.add(5, time); // 每次添加5个粒子
ArrayList<Particle> tempSet = father.ps.particleSet; // 获取粒子集合
for (int i = tempSet.size() - 1; i >= 0; i--) {
Particle particle = tempSet.get(i);
double timeSpan = time - particle.startTime; // 计算从程序开始到现在经过的时间
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) { // 如果粒子超过屏幕下边沿
tempSet.remove(particle);
}
particle.x = tempX;
particle.y = tempY;
}
time += span;
try {
Thread.sleep(sleepSpan);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
本例中的物理引擎没有采用获取系统时间的方式,而是自己定义了一个时间轴(成员变量time)。这样可以自己确定时间轴行进的快慢程度(通过改变成员变量span的值),而不必依赖于系统的时间。
3.视图类ParticleView类
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(Context context) {
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); // 清屏
ArrayList<Particle> particleSet = 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); // 绘制椭圆粒子
}
paint.setColor(Color.WHITE);
paint.setTextSize(18);
paint.setAntiAlias(true);
canvas.drawText(fps, 15, 15, paint);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public 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.绘图类DrawThread及Activity类
基本与上节相同
DrawThread类:
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(); //记录起始时间,该变量用于计算帧速率
int count=0 ; //记录帧数
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); //获取BallView的画布
synchronized (surfaceHolder) {
pv.doDraw(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(canvas!=null) {
surfaceHolder.unlockCanvasAndPost(canvas); // surfaceHolder解锁,并将画布传回
}
}
this.count++;
if(count==20) { //计满20帧时计算一次帧速率
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类:
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); //设置不显示标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置全屏
pv=new ParticleView(this);
setContentView(pv);
}
}
效果图:
二、瀑布粒子效果
瀑布粒子和焰火粒子十分类似,二者的运动都是带有初速度的下落运动。所不同的是焰火粒子水平方向和竖直方向的速度均不为零,而瀑布粒子只有水平方向初速度,竖直方向初速度为零。只需在焰火粒子的生成部分ParticleSet类中修改即可。
ParticleSet类add方法修改如下:
/**
* 向粒子集合中添加指定数量的粒子对象(瀑布粒子效果)
*/
public void add2(int count, double startTime) {
for (int i = 0; i < count; i++) {
int tempColor = this.getColor(i);
int tempR = 1; // 粒子半径
double tempv_v = 0; // 粒子竖直方向的速度为0
double tempv_h = 10 + 20 * (Math.random()); // 随机产生粒子水平方向的速度
int tempX = 50;
int tempY = (int) (50 - 10 * (Math.random())); // 随机产生粒子Y坐标,90到100之间
Particle particle = new Particle(tempColor, tempR, tempv_v,
tempv_h, tempX, tempY, startTime);
particleSet.add(particle);
}
}
效果图:
希望本文所述对大家的Android程序设计有所帮助。
标签:Android,游戏
0
投稿
猜你喜欢
Springmvc返回html页面问题如何解决
2021-07-05 09:29:45
轻松学习C#的结构和类
2023-12-10 13:46:19
MyBatis如何进行双重foreach循环
2022-11-24 22:54:17
如何把spring boot项目部署到tomcat容器中
2023-10-08 18:53:51
C#将Unicode编码转换为汉字字符串的简单方法
2021-06-05 02:11:38
java实现斗地主发牌系统
2023-06-11 01:48:40
SpringBoot2.7 WebSecurityConfigurerAdapter类过期配置
2022-09-23 00:27:32
手机方向传感器的缺点及解决方法探究
2021-12-27 08:58:58
微服务搭建集成Spring Cloud Turbine详解
2023-03-29 12:39:07
教你怎么用Java操作Redis
2023-07-07 22:17:53
详解Java Proxy动态 代理机制
2023-07-24 21:01:58
Android使用Intent传递组件大数据
2023-09-30 12:21:46
Java中高效的判断数组中某个元素是否存在详解
2021-05-25 17:32:08
Spring Boot集成Spring Cache过程详解
2023-08-02 19:20:16
C#用NPOI导出导入Excel帮助类
2022-09-27 00:23:43
c#使用filesystemwatcher监视文件系统的变化
2022-08-06 15:28:43
Java实现克隆的三种方式实例总结
2021-11-21 15:26:14
利用C#守护Python进程的方法
2023-01-05 13:25:56
详解如何全注解方式构建SpringMVC项目
2023-08-13 07:14:41
Java如何执行cmd命令
2022-05-02 09:54:45