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多边形碰撞检测的更多相关文章

  1. libgdx学习记录3——动画Animation

    libgdx动画采用Animation实现,即通过帧动画实现. 代码如下: package com.fxb.newtest; import com.badlogic.gdx.ApplicationAd ...

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

    地图对于游戏场景十分重要,很多游戏都需要对地图进行编辑,可使用TileMap进行编辑并生成对应的tmx格式地图文件. 编辑好后,可通过TmxMapLoader来读取地图文件.可通过一个正交相机Otho ...

  3. libgdx学习记录2——文字显示BitmapFont

    libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现.本文简单介绍最基本的bitmapfont的用法. 代码如下: package com.fxb.newtest; import ...

  4. libgdx学习记录22——3d物体创建

    libgdx是一个强大的游戏框架,不仅支持2d部分,同时还支持3d部分. libgdx的3d部分投影主要通过PerspectiveCamera实现. 物体的显示过程: 1. 创建远景相机,角度一般设为 ...

  5. libgdx学习记录20——多线程MultiThread资源处理

    在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中. 一般情形下,在这个线程中处理就行了.但是当某些逻辑处理 ...

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

    libgdx中,opengl 1.x要求图片长宽必须为2的整次幂,一般有如下解决方法 1. 将opengl 1.x改为opengl 2.0.(libgdx 1.0版本后不支持1.x,当然不存在这个问题 ...

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

    libgdx封装了Box2D物理引擎,通过这个引擎能够模拟物理现实,使设计出的游戏更具有真实感. libgdx中,Box2d程序的大概过程: 1. 创建物理世界world,并设置重力加速度. 2. 创 ...

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

    照相机在libgdx中的地位举足轻重,贯穿于整个游戏开发过程的始终.一般我们都通过Stage封装而间接使用Camera,同时我们也可以单独使用Camera以完成背景的移动.元素的放大.旋转等操作. C ...

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

    AssetManager用于对游戏中的资源进行加载.当游戏中资源(图片.背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题. 主要优点: 1. ...

随机推荐

  1. 根据学习廖雪峰老师的git教程做的笔记

    根据学习廖雪峰老师的git教程做的笔记 安装git 进行git的配置 配置您的用户名和邮箱地址,使用--global 这个参数表明了在此台机器上的所有仓库都会使用该配置 $ git config -- ...

  2. python第六十五天--python操作mysql

    pymysql模块对mysql进行 import pymysql # 创建连接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='ro ...

  3. python第三十天-类

    编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很 ...

  4. 用Python实现数据结构之映射

    映射与字典 字典dict是Python中重要的数据结构,在字典中,每一个键都对应一个值,其中键与值的关系就叫做映射,也可以说是每一个键都映射到一个值上. 映射(map)是更具一般性的数据类型,具体到P ...

  5. 【PAT】B1052 卖个萌(20 分)

    实在不想写这个题,好费劲,头疼,这是粘的柳婼的代码 ,等我有空再自己用c写吧 #include <iostream> #include <vector> using names ...

  6. January 25th, 2018 Week 04th Thursday

    What made something precious? Losing it and finding it. 一件东西怎样才会变得珍贵无比?先弄丢了它,然后又找到了. A short time ag ...

  7. ES5-ES6-ES7_const声明只读常量

    const 概述const声明一个只读的常量.一旦声明,常量的值就不能改变.const声明的变量不得改变值,这意味着,const一旦声明变量,就必须立即初始化,不能留到以后赋值. // const P ...

  8. Java:传值还是传引用?

    这是一个Java的经典问题,大部分人从C,C++语言入门,C语言有三种传递方式:值传递,地址传递和引用传递.详细的对C语言指针,引用的我个人的理解,见链接. Java所有操作都是传值操作!都是传值操作 ...

  9. JS控制台打印佛祖加持护身符

    console.log([     "                   _ooOoo_",     "                  o8888888o" ...

  10. P2731 骑马修栅栏 欧拉函数

    题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个 ...