Android游戲開(kāi)發(fā)之碰撞檢測(cè)(矩形碰撞、圓形碰撞、像素碰撞)
本文為大家分享了Android游戲開(kāi)發(fā)之碰撞檢測(cè),供大家參考,具體內(nèi)容如下
矩形碰撞 原理: 兩個(gè)矩形位置 的四種情況 不是這四中情況 則碰撞

圓形碰撞 原理: 利用兩個(gè)圓心之間的距離進(jìn)行判定.當(dāng)兩個(gè)圓心的距離小于半徑之和則碰撞.
像素碰撞 原理:不適用 遍歷所有像素 檢測(cè) 太多了
多矩形碰撞 原理:設(shè)置多個(gè)矩形碰撞檢測(cè)區(qū)域 檢測(cè)碰撞矩形數(shù)組 與另一碰撞矩形數(shù)組之間的位置關(guān)系.
矩形碰撞 代碼:
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;
//定義兩個(gè)矩形的寬高坐標(biāo)
private int x1 = 10, y1 = 110, w1 = 40, h1 = 40;
private int x2 = 100, y2 = 110, w2 = 40, h2 = 40;
//便于觀察是否發(fā)生了碰撞設(shè)置一個(gè)標(biāo)識(shí)位
private boolean isCollsion;
/**
* SurfaceView初始化函數(shù)
*/
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視圖創(chuàng)建,響應(yīng)此函數(shù)
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
flag = true;
//實(shí)例線程
th = new Thread(this);
//啟動(dòng)線程
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);
}
//繪制兩個(gè)矩形
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);
}
}
/**
* 觸屏事件監(jiān)聽(tīng)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//讓矩形1隨著觸屏位置移動(dòng)
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;
}
/**
* 按鍵事件監(jiān)聽(tīng)
*/
@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視圖狀態(tài)發(fā)生改變,響應(yīng)此函數(shù)
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
/**
* SurfaceView視圖消亡時(shí),響應(yīng)此函數(shù)
*/
@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;
//定義兩個(gè)圓形的半徑與坐標(biāo)
private int r1 = 20, r2 = 20;
private int x1 = 50, y1 = 100, x2 = 150, y2 = 100;
//定義一個(gè)碰撞標(biāo)識(shí)位
private boolean isCollision;
/**
* SurfaceView初始化函數(shù)
*/
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視圖創(chuàng)建,響應(yīng)此函數(shù)
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
flag = true;
//實(shí)例線程
th = new Thread(this);
//啟動(dòng)線程
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);
}
}
/**
* 觸屏事件監(jiān)聽(tīng)
*/
@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坐標(biāo)
* @param y1 圓形2的圓心X坐標(biāo)
* @param x2 圓形1的圓心Y坐標(biāo)
* @param y2 圓形2的圓心Y坐標(biāo)
* @param r1 圓形1的半徑
* @param r2 圓形2的半徑
* @return
*/
private boolean isCollisionWithCircle(int x1, int y1, int x2, int y2, int r1, int r2) {
//Math.sqrt:開(kāi)平方
//Math.pow(double x, double y): X的Y次方
if (Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)) <= r1 + r2) {
//如果兩圓的圓心距小于或等于兩圓半徑則認(rèn)為發(fā)生碰撞
return true;
}
return false;
}
/**
* 按鍵事件監(jiān)聽(tīng)
*/
@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視圖狀態(tài)發(fā)生改變,響應(yīng)此函數(shù)
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
/**
* SurfaceView視圖消亡時(shí),響應(yīng)此函數(shù)
*/
@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;
//定義兩個(gè)矩形圖形的寬高坐標(biāo)
private int rectX1 = 10, rectY1 = 10, rectW1 = 40, rectH1 = 40;
private int rectX2 = 100, rectY2 = 110, rectW2 = 40, rectH2 = 40;
//便于觀察是否發(fā)生了碰撞設(shè)置一個(gè)標(biāo)識(shí)位
private boolean isCollsion;
//定義第一個(gè)矩形的矩形碰撞數(shù)組
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 };
//定義第二個(gè)矩形的矩形碰撞數(shù)組
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初始化函數(shù)
*/
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視圖創(chuàng)建,響應(yīng)此函數(shù)
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
flag = true;
//實(shí)例線程
th = new Thread(this);
//啟動(dòng)線程
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);
}
//繪制兩個(gè)矩形
canvas.drawRect(rectX1, rectY1, rectX1 + rectW1, rectY1 + rectH1, paint);
canvas.drawRect(rectX2, rectY2, rectX2 + rectW2, rectY2 + rectH2, paint);
//---繪制碰撞區(qū)域使用非填充,并設(shè)置畫(huà)筆顏色白色
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
//繪制第一個(gè)矩形的所有矩形碰撞區(qū)域
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);
}
//繪制第二個(gè)矩形的所有矩形碰撞區(qū)域
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);
}
}
/**
* 觸屏事件監(jiān)聽(tīng)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//讓矩形1隨著觸屏位置移動(dòng)
rectX1 = (int) event.getX() - rectW1 / 2;
rectY1 = (int) event.getY() - rectH1 / 2;
if (isCollsionWithRect(arrayRect1, arrayRect2)) {
isCollsion = true;
} else {
isCollsion = false;
}
return true;
}
/**
* 按鍵事件監(jiān)聽(tīng)
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
/**
* 游戲邏輯
*/
private void logic() {
}
//Rect 類中的四個(gè)屬性 top bottom left right
//分別表示這個(gè)矩形的 上 下 左 右
public boolean isCollsionWithRect(Rect[] rectArray, Rect[] rect2Array) {
Rect rect = null;
Rect rect2 = null;
for (int i = 0; i < rectArray.length; i++) {
//依次取出第一個(gè)矩形數(shù)組的每個(gè)矩形實(shí)例
rect = rectArray[i];
//獲取到第一個(gè)矩形數(shù)組中每個(gè)矩形元素的屬性值
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++) {
//依次取出第二個(gè)矩形數(shù)組的每個(gè)矩形實(shí)例
rect2 = rect2Array[j];
//獲取到第二個(gè)矩形數(shù)組中每個(gè)矩形元素的屬性值
int x2 = rect2.left + this.rectX2;
int y2 = rect2.top + this.rectY2;
int w2 = rect2.right - rect2.left;
int h2 = rect2.bottom - rect2.top;
//進(jìn)行循環(huán)遍歷兩個(gè)矩形碰撞數(shù)組所有元素之間的位置關(guān)系
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 {
//只要有一個(gè)碰撞矩形數(shù)組與另一碰撞矩形數(shù)組發(fā)生碰撞則認(rèn)為碰撞
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視圖狀態(tài)發(fā)生改變,響應(yīng)此函數(shù)
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
/**
* SurfaceView視圖消亡時(shí),響應(yīng)此函數(shù)
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android仿開(kāi)心消消樂(lè)大樹(shù)星星無(wú)限循環(huán)效果
- Android游戲源碼分享之2048
- Unity3D游戲引擎實(shí)現(xiàn)在Android中打開(kāi)WebView的實(shí)例
- Android游戲開(kāi)發(fā)實(shí)踐之人物移動(dòng)地圖的平滑滾動(dòng)處理
- Android 游戲開(kāi)發(fā)之Canvas畫(huà)布的介紹及方法
- Android五子棋游戲程序完整實(shí)例分析
- 以一個(gè)著色游戲展開(kāi)講解Android中區(qū)域圖像填色的方法
- Android高仿2048小游戲?qū)崿F(xiàn)代碼
- Android開(kāi)心消消樂(lè)代碼實(shí)例詳解
- Android 2d游戲開(kāi)發(fā)之貪吃蛇基于surfaceview
相關(guān)文章
Android 軟鍵盤(pán)狀態(tài)并隱藏輸入法的實(shí)例
這篇文章主要介紹了Android 軟鍵盤(pán)狀態(tài)并隱藏輸入法的實(shí)例的相關(guān)資料,這里提供實(shí)例實(shí)現(xiàn)軟鍵盤(pán)切換并隱藏輸入法的鍵盤(pán),需要的朋友可以參考下2017-09-09
Android中分析Jetpack?Compose動(dòng)畫(huà)內(nèi)部的實(shí)現(xiàn)原理
這篇文章主要介紹了Android中分析Jetpack?Compose動(dòng)畫(huà)內(nèi)部的實(shí)現(xiàn)原理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09
Android中ViewPager實(shí)現(xiàn)滑動(dòng)條及與Fragment結(jié)合的實(shí)例教程
ViewPager類主要被用來(lái)實(shí)現(xiàn)可滑動(dòng)的視圖功能,這里我們就來(lái)共同學(xué)習(xí)Android中ViewPager實(shí)現(xiàn)滑動(dòng)條及與Fragment結(jié)合的實(shí)例教程,需要的朋友可以參考下2016-06-06
Android條紋進(jìn)度條的實(shí)現(xiàn)(調(diào)整view寬度仿進(jìn)度條)
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)條紋進(jìn)度條的方法,主要是通過(guò)調(diào)整view寬度仿進(jìn)度條,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-09-09
Android使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播圖片
這篇文章主要為大家詳細(xì)介紹了Android使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01

