BallMovable(一)
物体下落运动在游戏中经常运用,本文在Android2.2上开发BallMovable,具体实现如下:
分析:物体下落运动的参数:初始X坐标、初始Y坐标、实时X坐标、实时Y坐标、初始竖直方向的速度、初始水平方向的速度、实时竖直方向的速度、实时水平方向的速度、物体的半径、X方向运动的时间、Y方向运动的时间、可移动物体的图片、物体移动的线程等等。
package wyf.wpf; //声明包语句
import android.graphics.Bitmap; //引入相关类
import android.graphics.Canvas; //引入相关类
//代表可移动物体的Movable类
public class Movable{
int startX=0; //初始X坐标
int startY=0; //初始Y坐标
int x; //实时X坐标
int y; //实时Y坐标
float startVX=0f; //初始竖直方向的速度
float startVY=0f; //初始水平方向的速度
float v_x=0f; //实时水平方向速度
float v_y=0f; //实时竖直方向速度
int r; //可移动物体半径
double timeX; //X方向上的运动时间
double timeY; //Y方向上的运动时间
Bitmap bitmap=null; //可移动物体图片
BallThread bt=null; //负责小球移动时
boolean bFall=false;//小球是否已经从木板上下落
float impactFactor = 0.25f; //小球撞地后速度的损失系数
//构造器
public Movable(int x,int y,int r,Bitmap bitmap){
this.startX = x; //初始化X坐标
this.x = x; //初始化X坐标
this.startY = y; //初始化Y坐标
this.y = y; //初始化Y坐标
this.r = r; //初始化
this.bitmap = bitmap; //初始化图片
timeX=System.nanoTime(); //获取系统时间初始化
this.v_x = BallView.V_MIN + (int)((BallView.V_MAX-BallView.V_MIN)*Math.random());
bt = new BallThread(this);//创建并启动BallThread
bt.start();
}
//方法:绘制自己到屏幕上
public void drawSelf(Canvas canvas){
canvas.drawBitmap(this.bitmap,x, y, null);
}
}
其中 public static final int V_MAX=35; //小球水平速度的最大值
public static final int V_MIN=15; //小球竖直速度的最大值
this.v_x = BallView.V_MIN + (int)((BallView.V_MAX-BallView.V_MIN)*Math.random());这行代码表明小球将取V_MIN和V_MAX之间的值作为运动的速度。
Math.random():
Returns a pseudo-random number between 0.0 (inclusive) and 1.0 (exclusive).产生一个0.0-1.0之间的随机数,包括0.0,但是不包括1.0。
timeX=System.nanoTime(); //获取系统时间初始化
public static long nanoTime ()
Returns the current timestamp of the most precise timer available on the local system. This timestamp can only be used to measure an elapsed period by comparing it against another timestamp. It cannot be used as a very exact system time expression.
Returns
the current timestamp in nanoseconds (纳秒 十亿分之一秒)
该类继承自Thread,在程序中每个Movable都将有一个BallThread,主要用于改变小球的运动轨迹。
为零判断:在小球上升和碰撞地面后都需要判断小球的速度是否为零,但是不同于真是情况,在程序中小球的各个物理量都是离散的,这种情况还是以为零判断小球的速度就会出错,比如:前一次计算小球的速度为正,后一次计算小球的速度为负,跳过了为零这个点,那么小球的速度永远不能为零。本程序使用阀值,当速度小于某一阀值时,就认为是为零。
package wyf.wpf; //声明包语句
//继承自Thread的线程类,负责修改球的位置坐标
public class BallThread extends Thread{
Movable father; //Movable对象引用
boolean flag = false; //线程执行标志位
int sleepSpan = 30; //休眠时间
float g = 200; //球下落的加速度
double current; //记录当前时间
//构造器:初始化Movable对象引用及线程执行标志位
public BallThread(Movable father){
this.father = father;
this.flag = true; //设置线程执行的标志位为true
}
//方法:负责根据物理公式修改小球位置
public void run(){
while(flag){
current = System.nanoTime();//获取当前时间,单位为纳秒
double timeSpanX = (double)((current-father.timeX)/1000/1000/1000);//获取从玩家开始到现在水平方向走过的时间
//处理水平方向上的运动
father.x = (int)(father.startX + father.v_x * timeSpanX);
//处理竖直方向上的运动
if(father.bFall){//判断球是否已经移出挡板
double timeSpanY = (double)((current - father.timeY)/1000/1000/1000);
father.y = (int)(father.startY + father.startVY * timeSpanY + timeSpanY*timeSpanY*g/2);
father.v_y = (float)(father.startVY + g*timeSpanY);
//判断小球是否到达最高点
if(father.startVY < 0 && Math.abs(father.v_y) <= BallView.UP_ZERO){
father.timeY = System.nanoTime(); //设置新的运动阶段竖直方向上的开始时间
father.v_y = 0; //设置新的运动阶段竖直方向上的实时速度
father.startVY = 0; //设置新的运动阶段竖直方向上的初始速度
father.startY = father.y; //设置新的运动阶段竖直方向上的初始位置
}
//判断小球是否撞地
if(father.y + father.r*2 >= BallView.GROUND_LING && father.v_y >0){//判断撞地条件
//改变水平方向的速度
father.v_x = father.v_x * (1-father.impactFactor); //衰减水平方向上的速度
//改变竖直方向的速度
father.v_y = 0 - father.v_y * (1-father.impactFactor); //衰减竖直方向上的速度并改变方向
if(Math.abs(father.v_y) < BallView.DOWN_ZERO){ //判断撞地后的速度,太小就停止
this.flag = false;
}
else{ //撞地后的速度还可以弹起继续下一阶段的运动
//撞地之后水平方向的变化
father.startX = father.x; //设置新的运动阶段的水平方向的起始位置
father.timeX = System.nanoTime(); //设置新的运动阶段的水平方向的开始时间
//撞地之后竖直方向的变化
father.startY = father.y; //设置新的运动阶段竖直方向上的起始位置
father.timeY = System.nanoTime(); //设置新的运动阶段竖直方向开始运动的时间
father.startVY = father.v_y; //设置新的运动阶段竖直方向上的初速度
}
}
}
else if(father.x + father.r/2 >= BallView.WOOD_EDGE){//判断球是否移出了挡板
father.timeY = System.nanoTime(); //记录球竖直方向上的开始运动时间
father.bFall = true; //设置表示是否开始下落标志位
}
try{
Thread.sleep(sleepSpan); //休眠一段时间
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
double timeSpanX = (double)((current-father.timeX)/1000/1000/1000);//获取从玩家开始到现在水平方向走过的时间,
这句代码可把水平方向的时间(纳秒)转换为(秒)。
father.x = (int)(father.startX + father.v_x * timeSpanX);其中
father.v_x=BallView.V_MIN + (int)((BallView.V_MAX-BallView.V_MIN)*Math.random()); 这句代码计算水平运动的位置。
father.y = (int)(father.startY + father.startVY * timeSpanY + timeSpanY*timeSpanY*g/2); //(S=v0*t + 1/2*a*t^2 位移计算公式)
father.v_y = (float)(father.startVY + g*timeSpanY);(vt=v0+a*t)速度计算公式。