libgdx学习记录26——Polygon多边形碰撞检测

libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟。

Polygon内部自带是否包含点contains这个函数,通过这个函数我们可以判断两个多变行是否碰撞,即检测两个多边形的每个点是否在另一个多边形中。

检测代码:

 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     }

实例代码:

  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     @Override
102     public void dispose() {
103         // TODO Auto-generated method stub
104         super.dispose();
105     }
106
107 }

运行结果:

  

展示了三种情况,当然,这里只是进行简单的测试,可以任意绘制多边形进行检测。

时间: 2024-08-08 21:30:20

libgdx学习记录26——Polygon多边形碰撞检测的相关文章

libgdx学习记录11——平铺地图TiledMap

地图对于游戏场景十分重要,很多游戏都需要对地图进行编辑,可使用TileMap进行编辑并生成对应的tmx格式地图文件. 编辑好后,可通过TmxMapLoader来读取地图文件.可通过一个正交相机OthographicCamera和正交地图渲染器OrthogonalTiledMapRenderer来进行显示. 实例如下: 1 package com.fxb.newtest; 2 3 import com.badlogic.gdx.ApplicationAdapter; 4 import com.ba

libgdx学习记录19——图片动态打包PixmapPacker

libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题,这里针对的是0.9.9版本) 2. 使用TexturePacker将图片打包好然后作成一张大图添加进来. 第二种方法是常用方法,但是不太灵活,添加.删除某些图片不太方便,改动较大.这里可以考虑使用PixmapPacker将图片进行动态打包. 主要方法: pack(String name, Pixm

libgdx学习记录23——图片移动选择

模拟移动选择图片,采用相机实现. 1 package com.fxb.newtest; 2 3 import com.badlogic.gdx.ApplicationAdapter; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.graphics.Color; 6 import com.badlogic.gdx.graphics.GL10; 7 import com.badlogic.gdx.graphics.Texture;

libgdx学习记录17——照相机Camera

照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. Camera分为PerspectiveCamera(远景照相机)和OrthographicCamera(正交照相机). PerspectiveCamera为正常的照相机,当距离物体越远,则物体越小,一般在3D空间中使用. OrthographicCamera忽略了其Z轴,不管距离物体多远,其大小始终不

libgdx学习记录16——资源加载器AssetManager

AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. 大多数资源加载器AssetLoader都是异步加载,可以避免阻塞渲染线程. 2. 通过引用计数来进行释放资源. 3. 通过一个对象来管理所有其他资源. 主要函数: load(path,type)加载某个路径的资源文件,后面type指定所要加载的资源类型.这个函数只是将资源文件加入到资源队列中,并不会

libgdx学习记录18——Box2d物理引擎

libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创建正交相机,并设置其宽高.Box2d中使用物理世界中米作为单位,而不是图像中的像素,通常设一个比值,这里为了方便,直接设置为10. 3. 创建世界中的动态物体(一般是方块.圆环或其他形状物体)和静态物体(主要指地面.墙壁等). 4. 在渲染函数中添加world时间布,并利用DebugRenderer

libgdx学习记录5——演员Actor

Actor也是libgdx中非常重要的一个元素,一般与stage配合一起使用.Actor能够设置大小,位置,旋转和动画等. 我们自定义的Actor一般需要继承于Actor,并且重写其中的act和draw方法. 自定义的actor是一个图片. 1 class MyActor extends Actor{ 2 TextureRegion region; 3 4 public MyActor(){ 5 Texture texture = new Texture( Gdx.files.internal(

libgdx学习记录12——圆角矩形CircleRect

libgdx提供了ShapeRenderer这个工具,用它可以画点.画线.画圆.画矩形.画椭圆.画扇形,但是没有提供画圆角矩形的方法. 刚开始自己尝试分成8端,4端画直线,4端画扇形,发现多了半径几部分,于是又改成全部画线. 4端弧采用逐个描点实现. 具体代码: 1 package com.fxb.newtest; 2 3 import com.badlogic.gdx.ApplicationAdapter; 4 import com.badlogic.gdx.Gdx; 5 import com

libgdx学习记录27——线段与线段相交检测

给定p1, p2, p3, p4四个点,p1,p2为一条线段,p3,p4为一条线段,检测其是否有交点. 可分为三种情况: 1. L2与x轴平行 2. L2与y轴平行 3. L2与坐标轴不平行. (L1与坐标轴平行,类似处理) 基本思路,求出交点坐标,并检测其是否在两个线段内即可. 检测代码: 1 public static float min(float x, float y) { return x<y? x: y; } 2 public static float max(float x, fl