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. ...
随机推荐
- Yarn 安装 on centos7
本文演示如何在CentOS7上安装Yarn.注意这个Yarn是Js包管理器,不是Hadoop的资源调度器. 1 准备工作 1.1 浏览器访问安装包下载地址: https://github.com/ya ...
- 手动将经典 VM 从 VHD 迁移到新的 ARM 托管磁盘 VM
本部分有助于将现有 Azure VM 从经典部署模型迁移到资源管理器部署模型中的托管磁盘. 计划迁移到托管磁盘 本部分可帮助你针对 VM 和磁盘类型做出最佳决策. 位置 选取 Azure 托管磁盘可用 ...
- 【HANA系列】SAP HANA XS的JavaScript安全事项
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA XS使用Jav ...
- Linux regulator framework(1) - 概述【转】
转自蜗窝科技:http://www.wowotech.net/pm_subsystem/regulator_framework_overview.html 1. 前言 Regulator,中文名翻译为 ...
- sklearn中各种分类器回归器都适用于什么样的数据呢?
作者:匿名用户链接:https://www.zhihu.com/question/52992079/answer/156294774来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- IDEA 如何查看一个类里面的所有方法
快捷键:Alt+7
- JAVA中实现单例(Singleton)模式的八种方式
单例模式 单例模式,是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例.即一个类只有一个对象实例. 基本的实现思路 单 ...
- java 三种循环及注意事项
package debug; public class Demo8 { public static void main(String[] args) { //采用for循环打印10次Java好 for ...
- 【BZOJ4946】[NOI2017]蔬菜(贪心)
[BZOJ4946][NOI2017]蔬菜(贪心) 题面 BZOJ 洛谷 UOJ 题解 忽然发现今年\(NOI\)之前的时候切往年\(NOI\)的题目,就\(2017\)年的根本不知道怎么下手(一定是 ...
- swift语言混编--语言交互的接口
FFI stands for Foreign Function Interface. A foreign function interface is the popular name for the ...