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. ...
随机推荐
- Element隐藏组件:scrollbar
scrollbar是用来替代浏览器原生滚动条的组件,element的文档中并没有对scrollbar的描述. 使用方法:以<el-scrollbar/>包裹要滚动的元素,并设置固定高度.在 ...
- centos7 Linux 安装jdk1.8
在CentOS7上安装JDK1.8 1 通过 xshell 连接到CentOS7 服务器: 2 进入到目录 /usr/local/ 中(一般装应用环境我们都会在这个目录下装,也可自行选择目录): cd ...
- tinypng upload一键压缩上传工具,告别人肉
地址 项目地址:tinypng-upload 有兴趣的可以玩一玩,因为平时经常会用到图片压缩,上传,如果你也觉得很繁琐的话,这个将会解决你的痛点. 关于 tinypng-upload 这是一个基于 e ...
- Linux : Vim 使用与配置 (附 GitHub 自动化配置脚本)
由于经常使用 vim 编辑配置文件,有时候也会进行使用vim 编写一些脚本和c/c++ 程序,所以配置一个常用的 vim 是很是必要的.这篇博文主要是记录vim使用和配置相关的一些知识点. 关于vim ...
- row_number() over() 一句话概括,以及max()函数的一种查询分组中最大值的用法
row_number() over(partition by col1 order by col2) 根据COL1分组可能会有多个组,每组组内根据COL2进行排序.每组内都有自动生成的序号,从1开始, ...
- React路由 + 绝对路径引用
路由: 哈希路由(在url地址后加 #name) // 实现页面监听 window.onhashchange = function(){ console.log(‘hash:’,window.lo ...
- Beta冲刺(3/5)(麻瓜制造者)
今日已完成 邓弘立:完成了登录功能的重构,完成了部分商品管理功能 符天愉:利用ci开始写队友写好的管理员界面,由于后台独立开始使用一个仓库,所以晚上将alpha的版本更新到了git,并且添加了.git ...
- VS Code 快捷键使用小技巧
相关文档 官方文档(英文版):Documentation for Visual Studio Code 中文文档(未完成):GitHub - jeasonstudio/CN-VScode-Docs: ...
- JavaScipt中的Math.ceil() 、Math.floor() 、Math.round()、Math.pow() 三个函数的理解
以前一直会三个函数的使用产生混淆,现在通过对三个函数的原型定义的理解,其实很容易记住三个函数. 现在做一个总结: 1. Math.ceil()用作向上取整. 2. Math.floor()用作向下取整 ...
- PC端和移动APP端CSS样式初始化
CSS样式初始化分为PC端和移动APP端 1.PC端:使用Normalize.css Normalize.css是一种CSS reset的替代方案. 我们创造normalize.css有下面这几个目的 ...