Android游戏开发之碰撞检测(矩形碰撞、圆形碰撞、像素碰撞)
程序员文章站
2024-03-05 09:52:24
本文为大家分享了android游戏开发之碰撞检测,供大家参考,具体内容如下
矩形碰撞 原理: 两个矩形位置 的四种情况 不是这四中情况 则碰撞
圆形碰撞 原理: 利...
本文为大家分享了android游戏开发之碰撞检测,供大家参考,具体内容如下
矩形碰撞 原理: 两个矩形位置 的四种情况 不是这四中情况 则碰撞
圆形碰撞 原理: 利用两个圆心之间的距离进行判定.当两个圆心的距离小于半径之和则碰撞.
像素碰撞 原理:不适用 遍历所有像素 检测 太多了
多矩形碰撞 原理:设置多个矩形碰撞检测区域 检测碰撞矩形数组 与另一碰撞矩形数组之间的位置关系.
矩形碰撞 代码:
public class mysurfaceview extends surfaceview implements callback, runnable { private surfaceholder sfh; private paint paint; private thread th; private boolean flag; private canvas canvas; private int screenw, screenh; //定义两个矩形的宽高坐标 private int x1 = 10, y1 = 110, w1 = 40, h1 = 40; private int x2 = 100, y2 = 110, w2 = 40, h2 = 40; //便于观察是否发生了碰撞设置一个标识位 private boolean iscollsion; /** * surfaceview初始化函数 */ public mysurfaceview(context context) { super(context); sfh = this.getholder(); sfh.addcallback(this); paint = new paint(); paint.setcolor(color.white); paint.setantialias(true); setfocusable(true); } /** * surfaceview视图创建,响应此函数 */ @override public void surfacecreated(surfaceholder holder) { screenw = this.getwidth(); screenh = this.getheight(); flag = true; //实例线程 th = new thread(this); //启动线程 th.start(); } /** * 游戏绘图 */ public void mydraw() { try { canvas = sfh.lockcanvas(); if (canvas != null) { canvas.drawcolor(color.black); if (iscollsion) { paint.setcolor(color.red); paint.settextsize(20); canvas.drawtext("collision!", 0, 30, paint); } else { paint.setcolor(color.white); } //绘制两个矩形 canvas.drawrect(x1, y1, x1 + w1, y1 + h1, paint); canvas.drawrect(x2, y2, x2 + w2, y2 + h2, paint); } } catch (exception e) { // todo: handle exception } finally { if (canvas != null) sfh.unlockcanvasandpost(canvas); } } /** * 触屏事件监听 */ @override public boolean ontouchevent(motionevent event) { //让矩形1随着触屏位置移动 x1 = (int) event.getx() - w1 / 2; y1 = (int) event.gety() - h1 / 2; if (iscollsionwithrect(x1, y1, w1, h1, x2, y2, w2, h2)) { iscollsion = true; } else { iscollsion = false; } return true; } /** * 按键事件监听 */ @override public boolean onkeydown(int keycode, keyevent event) { return super.onkeydown(keycode, event); } /** * 游戏逻辑 */ private void logic() { } public boolean iscollsionwithrect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) { if (x1 >= x2 && x1 >= x2 + w2) { return false; } else if (x1 <= x2 && x1 + w1 <= x2) { return false; } else if (y1 >= y2 && y1 >= y2 + h2) { return false; } else if (y1 <= y2 && y1 + h1 <= y2) { return false; } return true; } @override public void run() { while (flag) { long start = system.currenttimemillis(); mydraw(); logic(); long end = system.currenttimemillis(); try { if (end - start < 50) { thread.sleep(50 - (end - start)); } } catch (interruptedexception e) { e.printstacktrace(); } } } /** * surfaceview视图状态发生改变,响应此函数 */ @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } /** * surfaceview视图消亡时,响应此函数 */ @override public void surfacedestroyed(surfaceholder holder) { flag = false; } }
圆形碰撞 代码:
public class mysurfaceview extends surfaceview implements callback, runnable { private surfaceholder sfh; private paint paint; private thread th; private boolean flag; private canvas canvas; private int screenw, screenh; //定义两个圆形的半径与坐标 private int r1 = 20, r2 = 20; private int x1 = 50, y1 = 100, x2 = 150, y2 = 100; //定义一个碰撞标识位 private boolean iscollision; /** * surfaceview初始化函数 */ public mysurfaceview(context context) { super(context); sfh = this.getholder(); sfh.addcallback(this); paint = new paint(); paint.setcolor(color.white); paint.setantialias(true); setfocusable(true); } /** * surfaceview视图创建,响应此函数 */ @override public void surfacecreated(surfaceholder holder) { screenw = this.getwidth(); screenh = this.getheight(); flag = true; //实例线程 th = new thread(this); //启动线程 th.start(); } /** * 游戏绘图 */ public void mydraw() { try { canvas = sfh.lockcanvas(); if (canvas != null) { canvas.drawcolor(color.black); if (iscollision) { paint.setcolor(color.red); paint.settextsize(20); canvas.drawtext("collision!", 0, 30, paint); } else { paint.setcolor(color.white); } canvas.drawcircle(x1, y1, r1, paint); canvas.drawcircle(x2, y2, r2, paint); } } catch (exception e) { // todo: handle exception } finally { if (canvas != null) sfh.unlockcanvasandpost(canvas); } } /** * 触屏事件监听 */ @override public boolean ontouchevent(motionevent event) { x1 = (int) event.getx(); y1 = (int) event.gety(); if (iscollisionwithcircle(x1, y1, x2, y2, r1, r2)) { iscollision = true; } else { iscollision = false; } return true; } /** * 圆形碰撞 * @param x1 圆形1的圆心x坐标 * @param y1 圆形2的圆心x坐标 * @param x2 圆形1的圆心y坐标 * @param y2 圆形2的圆心y坐标 * @param r1 圆形1的半径 * @param r2 圆形2的半径 * @return */ private boolean iscollisionwithcircle(int x1, int y1, int x2, int y2, int r1, int r2) { //math.sqrt:开平方 //math.pow(double x, double y): x的y次方 if (math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2)) <= r1 + r2) { //如果两圆的圆心距小于或等于两圆半径则认为发生碰撞 return true; } return false; } /** * 按键事件监听 */ @override public boolean onkeydown(int keycode, keyevent event) { return super.onkeydown(keycode, event); } /** * 游戏逻辑 */ private void logic() { } @override public void run() { while (flag) { long start = system.currenttimemillis(); mydraw(); logic(); long end = system.currenttimemillis(); try { if (end - start < 50) { thread.sleep(50 - (end - start)); } } catch (interruptedexception e) { e.printstacktrace(); } } } /** * surfaceview视图状态发生改变,响应此函数 */ @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } /** * surfaceview视图消亡时,响应此函数 */ @override public void surfacedestroyed(surfaceholder holder) { flag = false; } }
多矩形碰撞 代码
public class mysurfaceview extends surfaceview implements callback, runnable { private surfaceholder sfh; private paint paint; private thread th; private boolean flag; private canvas canvas; private int screenw, screenh; //定义两个矩形图形的宽高坐标 private int rectx1 = 10, recty1 = 10, rectw1 = 40, recth1 = 40; private int rectx2 = 100, recty2 = 110, rectw2 = 40, recth2 = 40; //便于观察是否发生了碰撞设置一个标识位 private boolean iscollsion; //定义第一个矩形的矩形碰撞数组 private rect cliprect1 = new rect(0, 0, 15, 15); private rect cliprect2 = new rect(rectw1 - 15, recth1 - 15, rectw1, recth1); private rect[] arrayrect1 = new rect[] { cliprect1, cliprect2 }; //定义第二个矩形的矩形碰撞数组 private rect cliprect3 = new rect(0, 0, 15, 15); private rect cliprect4 = new rect(rectw2 - 15, recth2 - 15, rectw2, recth2); private rect[] arrayrect2 = new rect[] { cliprect3, cliprect4 }; /** * surfaceview初始化函数 */ public mysurfaceview(context context) { super(context); sfh = this.getholder(); sfh.addcallback(this); paint = new paint(); paint.setcolor(color.white); paint.setantialias(true); setfocusable(true); } /** * surfaceview视图创建,响应此函数 */ @override public void surfacecreated(surfaceholder holder) { screenw = this.getwidth(); screenh = this.getheight(); flag = true; //实例线程 th = new thread(this); //启动线程 th.start(); } /** * 游戏绘图 */ public void mydraw() { try { canvas = sfh.lockcanvas(); if (canvas != null) { canvas.drawcolor(color.black); paint.setcolor(color.white); paint.setstyle(style.fill); if (iscollsion) { paint.settextsize(20); canvas.drawtext("collision!", 0, 30, paint); } //绘制两个矩形 canvas.drawrect(rectx1, recty1, rectx1 + rectw1, recty1 + recth1, paint); canvas.drawrect(rectx2, recty2, rectx2 + rectw2, recty2 + recth2, paint); //---绘制碰撞区域使用非填充,并设置画笔颜色白色 paint.setstyle(style.stroke); paint.setcolor(color.red); //绘制第一个矩形的所有矩形碰撞区域 for (int i = 0; i < arrayrect1.length; i++) { canvas.drawrect(arrayrect1[i].left + this.rectx1, arrayrect1[i].top + this.recty1, arrayrect1[i].right + this.rectx1, arrayrect1[i].bottom + this.recty1, paint); } //绘制第二个矩形的所有矩形碰撞区域 for (int i = 0; i < arrayrect2.length; i++) { canvas.drawrect(arrayrect2[i].left + this.rectx2, arrayrect2[i].top + this.recty2, arrayrect2[i].right + this.rectx2, arrayrect2[i].bottom + recty2, paint); } } } catch (exception e) { // todo: handle exception } finally { if (canvas != null) sfh.unlockcanvasandpost(canvas); } } /** * 触屏事件监听 */ @override public boolean ontouchevent(motionevent event) { //让矩形1随着触屏位置移动 rectx1 = (int) event.getx() - rectw1 / 2; recty1 = (int) event.gety() - recth1 / 2; if (iscollsionwithrect(arrayrect1, arrayrect2)) { iscollsion = true; } else { iscollsion = false; } return true; } /** * 按键事件监听 */ @override public boolean onkeydown(int keycode, keyevent event) { return super.onkeydown(keycode, event); } /** * 游戏逻辑 */ private void logic() { } //rect 类中的四个属性 top bottom left right //分别表示这个矩形的 上 下 左 右 public boolean iscollsionwithrect(rect[] rectarray, rect[] rect2array) { rect rect = null; rect rect2 = null; for (int i = 0; i < rectarray.length; i++) { //依次取出第一个矩形数组的每个矩形实例 rect = rectarray[i]; //获取到第一个矩形数组中每个矩形元素的属性值 int x1 = rect.left + this.rectx1; int y1 = rect.top + this.recty1; int w1 = rect.right - rect.left; int h1 = rect.bottom - rect.top; for (int j = 0; j < rect2array.length; j++) { //依次取出第二个矩形数组的每个矩形实例 rect2 = rect2array[j]; //获取到第二个矩形数组中每个矩形元素的属性值 int x2 = rect2.left + this.rectx2; int y2 = rect2.top + this.recty2; int w2 = rect2.right - rect2.left; int h2 = rect2.bottom - rect2.top; //进行循环遍历两个矩形碰撞数组所有元素之间的位置关系 if (x1 >= x2 && x1 >= x2 + w2) { } else if (x1 <= x2 && x1 + w1 <= x2) { } else if (y1 >= y2 && y1 >= y2 + h2) { } else if (y1 <= y2 && y1 + h1 <= y2) { } else { //只要有一个碰撞矩形数组与另一碰撞矩形数组发生碰撞则认为碰撞 return true; } } } return false; } @override public void run() { while (flag) { long start = system.currenttimemillis(); mydraw(); logic(); long end = system.currenttimemillis(); try { if (end - start < 50) { thread.sleep(50 - (end - start)); } } catch (interruptedexception e) { e.printstacktrace(); } } } /** * surfaceview视图状态发生改变,响应此函数 */ @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } /** * surfaceview视图消亡时,响应此函数 */ @override public void surfacedestroyed(surfaceholder holder) { flag = false; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读