Libgdx learning record 26 -- polygon Collision Detection

Source: Internet
Author: User
Tags libgdx

Math in libgdx encapsulates the polygon class, which is described by Multiple Fixed Points. During collision between objects, the object contour is sometimes irregular, at this time, we can use a polygon to outline its approximate contour and simulate it.

The polygon function includes the point contains function. Through this function, we can determine whether two changeable rows collide, that is, whether each point of two polygon is in another polygon.

Check Code:

 1     public static boolean isOverlap(Polygon polygon1, Polygon polygon2){ 2         for(int i=0; i<polygon2.getVertices().length; i+=2){ 3             if( polygon1.contains(polygon2.getVertices()[i], polygon2.getVertices()[i+1]) ){ 4                 return true; 5             } 6         } 7         for(int i=0; i<polygon1.getVertices().length; i+=2){ 8             if( polygon2.contains(polygon1.getVertices()[i], polygon1.getVertices()[i+1]) ){ 9                 return true;10             }11         }12         return false;13     }

Instance code:

  1 package com.fxb.Gam003;  2   3 import com.badlogic.gdx.ApplicationAdapter;  4 import com.badlogic.gdx.Gdx;  5 import com.badlogic.gdx.InputAdapter;  6 import com.badlogic.gdx.graphics.Color;  7 import com.badlogic.gdx.graphics.GL10;  8 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;  9 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; 10 import com.badlogic.gdx.math.Polygon; 11 import com.badlogic.gdx.math.Vector2; 12  13 public class Lib051_Polygon extends ApplicationAdapter{ 14  15     Polygon polygon1, polygon2; 16     ShapeRenderer rend; 17     float[] vertices1, vertices2; 18     Vector2 point = new Vector2(100, 50); 19      20     InputAdapter adapter = new InputAdapter(){ 21         @Override 22         public boolean touchDown(int screenX, int screenY, int pointer, int button) {             23             for(int i=0; i<polygon2.getVertices().length; i+=2){ 24                 polygon2.getVertices()[i  ] += screenX - point.x; 25                 polygon2.getVertices()[i+1] += Gdx.graphics.getHeight()-screenY - point.y; 26             } 27             polygon2.dirty(); 28              29             point.set(screenX, Gdx.graphics.getHeight()-screenY); 30             //polygon2.setVertices(new float[]{ 100+point.x, 50+point.y, 200+point.x, 70+point.y, 300+point.x, 150+point.y, 150+point.x, 100+point.y}); 31                          32             return true; 33         } 34          35     }; 36      37     @Override 38     public void create() { 39         // TODO Auto-generated method stub 40         super.create(); 41          42         polygon1 = new Polygon(); 43         vertices1 = new float[]{ 100, 100, 200, 100, 300, 300, 100, 200 }; 44         polygon1.setVertices(vertices1); 45          46         vertices2 = new float[]{ 100, 50, 200, 70, 300, 150, 150, 100}; 47         polygon2 = new Polygon(vertices2); 48          49         rend = new ShapeRenderer(); 50         Gdx.input.setInputProcessor(adapter); 51     } 52  53      54     public static boolean isOverlap(Polygon polygon1, Polygon polygon2){ 55         for(int i=0; i<polygon2.getVertices().length; i+=2){ 56             if( polygon1.contains(polygon2.getVertices()[i], polygon2.getVertices()[i+1]) ){ 57                 return true; 58             } 59         } 60         for(int i=0; i<polygon1.getVertices().length; i+=2){ 61             if( polygon2.contains(polygon1.getVertices()[i], polygon1.getVertices()[i+1]) ){ 62                 return true; 63             } 64         } 65         return false; 66     } 67      68      69      70     @Override 71     public void render() { 72         // TODO Auto-generated method stub 73         super.render(); 74         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 75         Gdx.gl.glClearColor(1, 1, 1, 1); 76          77         rend.begin(ShapeType.Line); 78         rend.setColor(Color.RED); 79         for(int i=0; i<polygon1.getVertices().length; i+=2){ 80             rend.line(vertices1[i], vertices1[i+1], vertices1[(i+2)%vertices1.length], vertices1[(i+3)%vertices1.length]); 81         } 82          83         float[] vertices3 = polygon2.getVertices(); 84         for(int i=0; i<polygon2.getVertices().length; i+=2){ 85             rend.line(vertices3[i], vertices3[i+1], vertices3[(i+2)%vertices3.length], vertices3[(i+3)%vertices3.length]); 86         } 87         rend.end(); 88          89         //if(polygon1.contains(point.x, point.y)){ 90         if( isOverlap(polygon1, polygon2) ){ 91             rend.setColor(Color.RED); 92         }else{ 93             rend.setColor(Color.BLUE); 94         } 95         rend.begin(ShapeType.Filled); 96         rend.circle(point.x, point.y, 5); 97         rend.end(); 98          99     }100 101     @Override102     public void dispose() {103         // TODO Auto-generated method stub104         super.dispose();105     }106 107 }

Running result:

Three cases are shown. Of course, we only perform a simple test here, and we can draw any polygon for detection.

Libgdx learning record 26 -- polygon Collision Detection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.