libgdx学习记录26——Polygon多边形碰撞检测
libgdx中Math封装了Polygon这个类,它是由多个定点进行描述实现的,在进行物体间的碰撞时,物体轮廓有时候是不规则的,这时候可以用一个多边形勾勒出其大概的轮廓,对其进行模拟。
Polygon内部自带是否包含点contains这个函数,通过这个函数我们可以判断两个多变行是否碰撞,即检测两个多边形的每个点是否在另一个多边形中。
检测代码:
public static boolean isOverlap(Polygon polygon1, Polygon polygon2){
for(int i=0; i<polygon2.getVertices().length; i+=2){
if( polygon1.contains(polygon2.getVertices()[i], polygon2.getVertices()[i+1]) ){
return true;
}
}
for(int i=0; i<polygon1.getVertices().length; i+=2){
if( polygon2.contains(polygon1.getVertices()[i], polygon1.getVertices()[i+1]) ){
return true;
}
}
return false;
}
实例代码:
package com.fxb.Gam003; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2; public class Lib051_Polygon extends ApplicationAdapter{ Polygon polygon1, polygon2;
ShapeRenderer rend;
float[] vertices1, vertices2;
Vector2 point = new Vector2(100, 50); InputAdapter adapter = new InputAdapter(){
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
for(int i=0; i<polygon2.getVertices().length; i+=2){
polygon2.getVertices()[i ] += screenX - point.x;
polygon2.getVertices()[i+1] += Gdx.graphics.getHeight()-screenY - point.y;
}
polygon2.dirty(); point.set(screenX, Gdx.graphics.getHeight()-screenY);
//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}); return true;
} }; @Override
public void create() {
// TODO Auto-generated method stub
super.create(); polygon1 = new Polygon();
vertices1 = new float[]{ 100, 100, 200, 100, 300, 300, 100, 200 };
polygon1.setVertices(vertices1); vertices2 = new float[]{ 100, 50, 200, 70, 300, 150, 150, 100};
polygon2 = new Polygon(vertices2); rend = new ShapeRenderer();
Gdx.input.setInputProcessor(adapter);
} public static boolean isOverlap(Polygon polygon1, Polygon polygon2){
for(int i=0; i<polygon2.getVertices().length; i+=2){
if( polygon1.contains(polygon2.getVertices()[i], polygon2.getVertices()[i+1]) ){
return true;
}
}
for(int i=0; i<polygon1.getVertices().length; i+=2){
if( polygon2.contains(polygon1.getVertices()[i], polygon1.getVertices()[i+1]) ){
return true;
}
}
return false;
} @Override
public void render() {
// TODO Auto-generated method stub
super.render();
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(1, 1, 1, 1); rend.begin(ShapeType.Line);
rend.setColor(Color.RED);
for(int i=0; i<polygon1.getVertices().length; i+=2){
rend.line(vertices1[i], vertices1[i+1], vertices1[(i+2)%vertices1.length], vertices1[(i+3)%vertices1.length]);
} float[] vertices3 = polygon2.getVertices();
for(int i=0; i<polygon2.getVertices().length; i+=2){
rend.line(vertices3[i], vertices3[i+1], vertices3[(i+2)%vertices3.length], vertices3[(i+3)%vertices3.length]);
}
rend.end(); //if(polygon1.contains(point.x, point.y)){
if( isOverlap(polygon1, polygon2) ){
rend.setColor(Color.RED);
}else{
rend.setColor(Color.BLUE);
}
rend.begin(ShapeType.Filled);
rend.circle(point.x, point.y, 5);
rend.end(); } @Override
public void dispose() {
// TODO Auto-generated method stub
super.dispose();
} }
运行结果:

展示了三种情况,当然,这里只是进行简单的测试,可以任意绘制多边形进行检测。
libgdx学习记录26——Polygon多边形碰撞检测的更多相关文章
- libgdx学习记录3——动画Animation
libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...
- libgdx学习记录11——平铺地图TiledMap
地图对于游戏场景十分重要,很多游戏都需要对地图进行编辑,可使用TileMap进行编辑并生成对应的tmx格式地图文件. 编辑好后,可通过TmxMapLoader来读取地图文件.可通过一个正交相机Otho ...
- libgdx学习记录2——文字显示BitmapFont
libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...
- libgdx学习记录22——3d物体创建
libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...
- libgdx学习记录20——多线程MultiThread资源处理
在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...
- libgdx学习记录19——图片动态打包PixmapPacker
libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...
- libgdx学习记录18——Box2d物理引擎
libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...
- libgdx学习记录17——照相机Camera
照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...
- libgdx学习记录16——资源加载器AssetManager
AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...
随机推荐
- JavaScript大杂烩7 - 理解内置集合
JavaScript内置了很多对象,简单的类型如String,Number,Boolean (相应的"值类型"拥有相同的方法),复杂一点的如Function,Object,Arra ...
- const int *p 和int * const p 的区别
看例子: int sloth = 3; const int *p1 = &sloth; int * p2 const = &sloth; 这样申明的话,不允许使用p1来修改sloth的 ...
- CPUFreq驱动
CPUFreq子系统位于 drivers/cpufreq目录下,负责进行运行过程中CPU频率和电压的动态调整,即DvFS( Dynamic Voltage Frequency Scaling,动态电压 ...
- Centos7.5.1804永久生效修改主机名
原来主机名 [root@node1 ~]# 查看Centos的版本: [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Cor ...
- python曲线拟合
http://blog.sina.com.cn/s/blog_aed5bd1d0102vid7.html 1.多项式拟合范例: import matplotlib.pyplot as plt impo ...
- C++基础算法学习——熄灯问题
有一个由按钮组成的矩阵, 其中每行有6个按钮, 共5行– 每个按钮的位置上有一盏灯– 当按下一个按钮后, 该按钮以及周围位置(上边, 下边,左边, 右边)的灯都会改变状态26熄灯问题 POJ1222– ...
- SSM框架下使用websocket实现后端发送消息至前端
本篇文章本人是根据实际项目需求进行书写的第一版,里面有些内容对大家或许没有用,但是核心代码本人已对其做了红色标注.文章讲解我将从maven坐标.HTML页面.js文件及后端代码一起书写. 一.mave ...
- 【TJOJI\HEOI2016】求和
[TJOI/HEOI2016]求和 这题好难啊!! 斯特林数+NTT. 首先我们将第二类斯特林数用容斥展开,具体原理不解释了. \(\displaystyle S(i,j)=\frac{1}{j!}\ ...
- WPFの实现word的缩放效果
ms-word做出的效果令人十分欣喜,那么如何用wpf达到这个效果,下面我们来进行讨论. 界面上我用一个WrapPanel作为父级控件,动态添加InkCanvas作为子控件 <Grid> ...
- python五十八课——正则表达式(替换)
替换:sub(regex,repl,string,count,[flags=0]): 替换数据,返回字符串(已经被替换完成后的内容)subn(regex,repl,string,count,[flag ...