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. ...
随机推荐
- LaTeX:图形的填充(生成阴影图形)
将内网和外网看到的综合整理. 韦恩图Venn \documentclass{standalone} \usepackage{tikz} %导出为图片需要安装imagemagick %https://t ...
- springboot 升级到2.0后 context-path 配置 不起作用,不生效 不管用 皆是因为版本改动导致的在这里记录一下
不知不觉,新的项目已经将springboot升级为2.0版本了.刚开始没有配置server.contextpath,默认的“/”,然后今天放到自己的服务器上,所以就要规范名称. 结果,失败了,无论我 ...
- sqlserver 2017 docker安装(启动代理)
从 Docker Hub 中拉出 SQL Server 2017 Linux 容器映像. docker pull microsoft/mssql-server-linux:2017-latest 运行 ...
- EF 排序扩展
public static class LinqOrderEx { private static IOrderedQueryable<T> OrderingHelper<T>( ...
- Python基础知识:字符串
1.Python中大写字母命名的变量,默认为常量,不可修改:列如:MYSQL_CONNECTION = '192.168.1.1' 2.字符串换行输入格式:换行用\隔开,两行分别用引号,制表符\t,换 ...
- 【PAT】B1052 卖个萌(20 分)
实在不想写这个题,好费劲,头疼,这是粘的柳婼的代码 ,等我有空再自己用c写吧 #include <iostream> #include <vector> using names ...
- Mysql基础之 事务
MySql事务 Mysql事务主要处理操作量大,复杂度高的数据. Mysql事务需要注意的三点: 1.在mysql中只有使用innodb数据库引擎的数据库或表才支持事务 2.事务处理可以用来维护数据库 ...
- Markdown图片存储解决方法-利用阿里云OSS
我们在用markdown写一些博客或者文章的时候,常常需要引用一些图片,一般都是找一个免费的图床上传,然后复制图片链接在我们的markdown文章中.类似像这样: 存在的隐患 一般的免费图片托管网站有 ...
- Java设计模式之十一 ---- 策略模式和模板方法模式
前言 在上一篇中我们学习了行为型模式的访问者模式(Visitor Pattern)和中介者模式(Mediator Pattern).本篇则来学习下行为型模式的两个模式,策略模式(Strategy Pa ...
- 类的反射_reflex
JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个实体类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为j ...