This article for everyone to share the Android game development Collision detection, for your reference, the specific content as follows
The principle of rectangular collision : Four kinds of two rectangular positions not in these four cases are collisions
Circular Collision principle: Using the distance between the two centers to determine. When the distance between two centers is less than the radius of the collision.
Pixel Collision principle: Not applicable traversal all pixel detection too much
Multi-Rectangular collision principle: To set up multiple rectangular collision detection areas to detect the position relationship between a collision rectangular array and another colliding rectangular array.
Rectangular Collision Code:
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;
Defines a two-rectangle wide-high coordinate private int x1 = ten, y1 =, W1 =, H1 = 40;
private int x2 = y2 =, W2 =, H2 = 40;
Easy to see if there is a collision setting an identity bit private Boolean iscollsion;
/** * Surfaceview initialization function/public Mysurfaceview {super (context);
SFH = This.getholder ();
Sfh.addcallback (this);
Paint = new paint ();
Paint.setcolor (Color.White);
Paint.setantialias (TRUE);
Setfocusable (TRUE); /** * Surfaceview View creation, response to this function/@Override public void surfacecreated (Surfaceholder holder) {screenw = t
His.getwidth ();
Screenh = This.getheight ();
Flag = true;
instance thread th = new thread (this);
Start thread Th.start (); /** * Game drawing/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, paint);
else {paint.setcolor (color.white);
//Draw two rectangular 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.unlock
Canvasandpost (canvas); /** * Touchscreen Event Monitor/@Override public boolean ontouchevent (Motionevent event) {//Let rectangle 1 move with touchscreen position 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; /** * Button Event Monitor/@Override public boolean onKeyDown (int KeyCode, KeyEvent event) {return Super.onkeydown (KeyCode, event); /** * Game logic/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 <) {Thread.Sleep (End-start));
} catch (Interruptedexception e) {e.printstacktrace (); /** * Surfaceview view state changed in response to this function/* @Override public voidSurfacechanged (surfaceholder holder, int format, int width, int height) {}/** * Surfaceview View dies, respond to this function * *
Override public void surfacedestroyed (Surfaceholder holder) {flag = false;
}
}
Round Collision Code:
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;
Defines two rounded radii with coordinates private int r1 = r2 = 20;
private int x1 = Y1 = +, x2 = $, y2 = 100;
Defines a collision identifier bit private Boolean iscollision;
/** * Surfaceview initialization function/public Mysurfaceview {super (context);
SFH = This.getholder ();
Sfh.addcallback (this);
Paint = new paint ();
Paint.setcolor (Color.White);
Paint.setantialias (TRUE);
Setfocusable (TRUE); /** * Surfaceview View creation, response to this function/@Override public void surfacecreated (Surfaceholder holder) {screenw = t
His.getwidth ();
Screenh = This.getheight ();
Flag = true;
instance thread th = new thread (this);
Start thread Th.start ();
/** * Game drawing/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, 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.unlock
Canvasandpost (canvas);
/** * Touchscreen Event Monitor/@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;
/** * Circular collision * @param X1 Circle 1 's Center x coordinate * @param y1 Circle 2 's Center x coordinate * @param x2 Circle 1 's Center y coordinate * @param y2 Circle 2 's Center y-coordinate * @param R1 Circle 1 radius * @param R2 Circle 2 radius *return */Private Boolean iscollisionwithcircle (int x1, int y1, int x2, int y2, int r1, int r2) {//math.sqrt: Open squared
Math.pow (double x, double y): X's Y-square if (Math.sqrt (Math.pow (X1-X2, 2) + Math.pow (Y1-y2, 2)) <= R1 + R2) {
If the center distance of two circles is less than or equal to two circle radius, the collision return true is considered;
return false; /** * Button Event Monitor/@Override public boolean onKeyDown (int keycode, keyevent event) {return Super.onkeydow
N (keycode, event); /** * Game logic/private void logic () {} @Override public void Run () {while (flag) {Long STA
RT = System.currenttimemillis ();
Mydraw ();
Logic ();
Long end = System.currenttimemillis ();
try {if (End-start <) {Thread.Sleep (End-start));
} catch (Interruptedexception e) {e.printstacktrace (); /** * Surfaceview view state changed in response to this function */@Override public void surfacechanged (surfaceholder holder, int ForMat, int width, int height) {}/** * Surfaceview View dies, respond to this function */@Override public void surfacedestroyed (Surf
Aceholder holder) {flag = false;
}
}
Multi-Rectangular collision code
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;
Defines the wide-high coordinates of two rectangular graphics private int rectX1 = ten, rectY1 = ten, rectW1 =, rectH1 = 40;
private int rectX2 = RectY2 =, rectW2 =, rectH2 = 40;
Easy to see if there is a collision setting an identity bit private Boolean iscollsion;
Defines the rectangular array of collisions for the first rectangle 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};
Defines a rectangular array of collisions for the second rectangle 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 initialization function/public Mysurfaceview {super (context);
SFH = This.getholder ();Sfh.addcallback (this);
Paint = new paint ();
Paint.setcolor (Color.White);
Paint.setantialias (TRUE);
Setfocusable (TRUE); /** * Surfaceview View creation, response to this function/@Override public void surfacecreated (Surfaceholder holder) {screenw = t
His.getwidth ();
Screenh = This.getheight ();
Flag = true;
instance thread th = new thread (this);
Start thread Th.start ();
/** * Game drawing/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, paint);
//Draw two rectangular canvas.drawrect (rectX1, rectY1, rectX1 + rectW1, rectY1 + rectH1, paint);
Canvas.drawrect (rectX2, rectY2, rectX2 + rectW2, rectY2 + rectH2, paint); ---Draw the collision area using a paint.setstyle and set the brush color white (style.stroke);
Paint.setcolor (color.red); Draws all rectangular collision areas for the first rectangle 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 + thi
S.recty1, paint); ///Draw all rectangular collision areas for the second rectangle 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); (Exception e) {//Todo:handle Exception} finally {if (Canvas!= null) SF
H.unlockcanvasandpost (canvas); /** * Touchscreen Event Monitor/@Override public boolean ontouchevent (Motionevent event) {//Let rectangle 1 move with touchscreen position Rectx
1 = (int) event.getx ()-rectw1/2;
rectY1 = (int) event.gety ()-recth1/2; if (Iscollsionwithrect (ArrayRect1, Arrayrect2)) {iscollsion = true;
else {iscollsion = false;
return true; /** * Button Event Monitor/@Override public boolean onKeyDown (int keycode, keyevent event) {return Super.onkeydow
N (keycode, event); }/** * Game logic/private void logic () {}//rect class four properties top bottom left right//respectively indicates the top and bottom of this rectangle
Left and right public boolean iscollsionwithrect (rect[] Rectarray, rect[] rect2array) {Rect Rect = null;
Rect rect2 = null;
for (int i = 0; i < rectarray.length; i++) {//to sequentially remove each rectangular instance of the first rectangular array rect = rectarray[i];
Gets the property value int x1 = rect.left + this.rectx1 for each rectangle element in the first rectangular array;
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 +) {//to sequentially remove each rectangular instance of the second rectangular array rect2 = rect2array[j];
Gets the property value int x2 = rect2.left + this.rectx2 for each rectangle element in the second rectangular array; int y2 = Rect2.top + This.recty2;
int w2 = Rect2.right-rect2.left;
int h2 = Rect2.bottom-rect2.top; Loop through two rectangular collisions. The position relationship between all elements of an array if (x1 >= x2 && x1 >= x2 + w2) {} else if (x1 <= x2 &&am P x1 + W1 <= x2) {} else if (y1 >= y2 && y1 >= y2 + h2) {} else if (y1 <= y2 && ;
Y1 + h1 <= y2) {} else {//As long as there is a collision between a colliding rectangular array and another colliding rectangular array, the collision is considered to be true;
}} return false;
@Override public void Run () {while (flag) {Long start = System.currenttimemillis ();
Mydraw ();
Logic ();
Long end = System.currenttimemillis ();
try {if (End-start <) {Thread.Sleep (End-start));
} catch (Interruptedexception e) {e.printstacktrace (); /** * Surfaceview view state changed in response to this function */@Override public void surfacechanged (surfaceholder holder, int format, int Width, int height) {}/** * Surfaceview View dies, respond to this function */@Override public void surfacedestroyed (Surfaceholder hol
Der) {flag = false;
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.