游戏碰撞OBB算法(java代码)
业务需求
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaDM0ODU5MjUzMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
方向包围盒(Oriented bounding box),简称OBB。方向包围盒类似于AABB。可是具有方向性、能够旋转。AABB不能旋转。
如图3所看到的。
就说明这两个矩形在x轴(也相当于直线A3A4,A1A2)的投影不相交,x轴不是真正意义上的x轴,我仅仅是让大家看得明了一点。
由于矩形是有规则的图形,仅仅要分别给他们检測矩形每一条边的垂直线的投影就能够了,两个矩形总共8条边,由于每一个矩形的边都有两两平行,和两两垂直。所以我们仅仅要检測两个矩形到每一个矩形相邻两条边的投影是否相交,仅仅要4次检測中有当中一次两个矩形的投影没有相交就能够推断这两个矩形没有相交,反之就是相交
(a.b)/|b|
我这是以4个顶点确定一个矩形,client须要这么做的,事实上我认为一个中心点,长,宽,旋转的角度来确定应该矩形比較好,不然四个顶点。万一传错了当中一个,拼成的不是矩形那不是出问题了。
package game; /**
* @author hjn
*
*/
public abstract class AbstractShape implements IShape { protected int x; protected int y; public AbstractShape(int x, int y) {
super();
this.x = x;
this.y = y;
} public int getX() {
return x;
} public void setX(int x) {
this.x = x;
} public int getY() {
return y;
} public void setY(int y) {
this.y = y;
} }
package game; /**
* @author hjn
*
*/
public interface IShape { int getX(); int getY(); boolean collision(IShape other); boolean contains(int px, int py); IShape copy(); void moveTo(int px, int py); }
/**
*
*/
package game; /**
* 圆形
*
* @author hjn
*
*/
public class Circle extends AbstractShape { /** 半径 */
private int radius; public Circle(int px, int py, int radius) {
super(px, py);
this.radius = radius;
}
/**
* 跟别的图像是否有重叠
*/
@Override
public boolean collision(IShape other) {
if (other instanceof Circle) {
Circle otherCircle = (Circle) other;
return (this.radius + otherCircle.radius) * (this.radius + otherCircle.radius) >= ((this.x - otherCircle.x) * (this.x - otherCircle.x) + (this.y - otherCircle.y) * (this.y - otherCircle.y)); }else if(other instanceof Rectangle){
return other.collision(this);
}
return false;
} /**
* 是否包括某个点
*/
@Override
public boolean contains(int px, int py) {
return this.radius * radius - ((this.x - px) * (this.x - px) + (this.y - py) * (this.y - py)) >= 0;
} public int getRadius() {
return radius;
} public void setRadius(int radius) {
this.radius = radius;
} @Override
public Circle copy() {
return new Circle(radius, radius, radius);
} @Override
public void moveTo(int px, int py) {
this.x = px;
this.y = py;
}
}
/**
*
*/
package game; /**
* 矩形
*
* @author hjn
*
*/
public class Rectangle extends AbstractShape { /** 矩形的顶点 */
private int[][] vertex; public Rectangle(int px, int py, int[][] vertex) {
super(px, py);
this.vertex = vertex;
} /**
* 是否有重叠,圆心到矩形6条边的距离小于圆的半径而且圆心不在矩形内那么这个矩形跟这个圆重叠
*/
@Override
public boolean collision(IShape other) {
try {
if (other instanceof Circle) {
Circle circle = (Circle) other;
boolean fla = this.getLength(circle.x, circle.y, vertex[0][0],
vertex[0][1], vertex[1][0], vertex[1][1])
- circle.getRadius() < 0;
if (fla) {
return true;
}
boolean fla2 = this.getLength(circle.x, circle.y, vertex[0][0],
vertex[0][1], vertex[2][0], vertex[2][1])
- circle.getRadius() < 0;
if (fla2) {
return true;
}
boolean fla3 = this.getLength(circle.x, circle.y, vertex[0][0],
vertex[0][1], vertex[3][0], vertex[3][1])
- circle.getRadius() < 0;
if (fla3) {
return true;
}
boolean fla4 = this.getLength(circle.x, circle.y, vertex[1][0],
vertex[1][1], vertex[2][0], vertex[2][1])
- circle.getRadius() < 0;
if (fla4) {
return true;
}
boolean fla5 = this.getLength(circle.x, circle.y, vertex[1][0],
vertex[1][1], vertex[3][0], vertex[3][1])
- circle.getRadius() < 0;
if (fla5) {
return true;
}
boolean fla6 = this.getLength(circle.x, circle.y, vertex[2][0],
vertex[2][1], vertex[3][0], vertex[3][1])
- circle.getRadius() < 0;
if (fla6) {
return true;
}
boolean fla7 = this.contains(circle.x, circle.y);
if (fla7) {
return true;
}
} else if (other instanceof Rectangle) {
Rectangle otherRectangle = (Rectangle) other;
return this.collisionOBB(otherRectangle);
}
} catch (Exception e) {
// 数组下标越界
e.printStackTrace();
return false;
}
return false;
} /**
* OBB矩形碰撞检測
* @param rectangle 矩形2
* @return
*/
private boolean collisionOBB(Rectangle rectangle){
int[][] vertex2=rectangle.vertex;
/*矩形2相邻两条边的向量*/
int wx1=vertex2[0][0]-vertex2[1][0];
int wy1=vertex2[0][1]-vertex2[1][1];
int wx2=vertex2[1][0]-vertex2[2][0];
int wy2=vertex2[1][1]-vertex2[2][1];
/*两个矩形中心点连接向量*/
int centerX=(vertex2[0][0]+vertex2[2][0])/2-(vertex[0][0]+vertex[2][0])/2;
int centerY=(vertex2[0][0]+vertex2[2][0])/2-(vertex[0][0]+vertex[2][0])/2;
/*矩形一第一条边的向量*/
int x11=vertex[0][0]-vertex[1][0];
int y11=vertex[0][1]-vertex[1][1];
/*矩形一在第一条边的投影*/
double le1=Math.sqrt(x11*x11+y11*y11);
/*矩形2相邻两条边在矩形1第一条边上的投影,比如projection211表示第2个矩形的第1条边在矩形1上第1条边的投影*/
double projection2111=this.getProjection(wx1, wy1, x11, y11);
double projection2211=this.getProjection(wx2, wy2, x11, y11);
/*中心点连接向量的投影*/
double centerProjection1=this.getProjection(centerX, centerY, x11, y11);
/*两个矩形投影之和*/
double total=projection2111+projection2211+le1;
/*假设中心点向量投影大于矩形投影之和的一半那肯定没有碰撞*/
if(centerProjection1>total/2){
return false;
} int x12=vertex[1][0]-vertex[2][0];
int y12=vertex[1][1]-vertex[2][1];
double le2=Math.sqrt(x12*x12+y12*y12);
double projection2112=this.getProjection(wx1, wy1, x12, y12);
double projection2212=this.getProjection(wx2, wy2, x12, y12);
double centerProjection2=this.getProjection(centerX, centerY, x12, y12);
if(centerProjection2>(projection2112+projection2212+le2)/2){
return false;
}
/*反过来矩形1在矩形2相邻两条边的投影的一半跟中心点向量的投影大小对照,不一一写凝视了*/
int wx11=vertex[0][0]-vertex[1][0];
int wy11=vertex[0][1]-vertex[1][1];
int wx12=vertex[1][0]-vertex[2][0];
int wy12=vertex[1][1]-vertex[2][1]; int x21=vertex2[1][0]-vertex2[2][0];
int y21=vertex2[1][1]-vertex2[2][1];
double le3=Math.sqrt(x21*x21+y21*y21);
double projection1121=this.getProjection(wx11, wy11, x21, y21);
double projection1221=this.getProjection(wx12, wy12, x21, y21);
double centerProjection3=this.getProjection(centerX, centerY, x21, y21);
if(centerProjection3>(projection1121+projection1221+le3)/2){
return false;
} int x22=vertex2[1][0]-vertex2[2][0];
int y22=vertex2[1][1]-vertex2[2][1];
double le4=Math.sqrt(x22*x22+y22*y22);
double projection1122=this.getProjection(wx11, wy11, x22, y22);
double projection1222=this.getProjection(wx12, wy12, x22, y22);
double centerProjection4=this.getProjection(centerX, centerY, x22, y22);
if(centerProjection4>(projection1122+projection1222+le4)/2){
return false;
}
return true;
}
/**
* 求向量1在向量2的投影
* @param x1 向量1的x坐标
* @param y1 向量1的y坐标
* @param x2 向量2的x坐标
* @param y2 向量2的y坐标
* @return 投影的绝对值
*/
private double getProjection(int x1,int y1,int x2,int y2){
double t=(x1*x2+y1*y2)/(Math.sqrt(x1*x1+y1*y1)*Math.sqrt(x2*x2+y2*y2));
double length=Math.sqrt(x1*x1+y1*y1)*t;
return Math.abs(length);
} /**
* 是否包括某一点
*/
@Override
public boolean contains(int px, int py) {
double l = this.getLength(px, py, vertex[0][0], vertex[0][1],
vertex[1][0], vertex[1][1]);
double l1 = this.getLength(px, py, vertex[1][0], vertex[1][1],
vertex[2][0], vertex[2][1]);
double l2 = this.getLength(px, py, vertex[2][0], vertex[2][1],
vertex[3][0], vertex[3][1]);
double l3 = this.getLength(px, py, vertex[3][0], vertex[3][1],
vertex[0][0], vertex[0][1]);
double total = l1 + l2 + l3 + l;
double width = this.getLength(vertex[0][0], vertex[0][1], vertex[1][0],
vertex[1][1]);
double height = this.getLength(vertex[3][0], vertex[3][1],
vertex[0][0], vertex[0][1]);
return total == (width + height);
} /* 两点间的距离 */
private double getLength(int x1, int y1, int x2, int y2) {
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
} /**
* 点到线段的距离
*
* @param x
* 点x坐标
* @param y
* 点y坐标
* @param x1
* 线段顶点1x坐标
* @param y1
* 线段顶点1y坐标
* @param x2
* 线段顶点2x坐标
* @param y2
* 线段顶点2y坐标
* @return
*/
private double getLength(int x, int y, int x1, int y1, int x2, int y2) {
double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
if (cross <= 0) {
return Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
}
double d2 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
if (cross >= d2) {
return Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
}
double r = cross / d2;
double px = x1 + (x2 - x1) * r;
double py = y1 + (y2 - y1) * r;
return Math.sqrt((x - px) * (x - px) + (py - y) * (py - y)); } public int[][] getVertex() {
return vertex;
} public void setVertex(int[][] vertex) {
this.vertex = vertex;
} @Override
public Rectangle copy() {
return new Rectangle(x, y, vertex);
} @Override
public void moveTo(int px, int py) { int vx = px - this.x;
int vy = py - this.y; int[][] copyOfVertex = new int[this.vertex.length][2];
for (int i = 0; i < this.vertex.length; i++) {
copyOfVertex[i][0] = vx + this.vertex[i][0];
copyOfVertex[i][1] = vy + this.vertex[i][1];
} this.x = px;
this.y = py;
} }
package game; public class Test {
//依据两点能够获取向量
//依据一点能够获得
//依据两点获取 @org.junit.Test
public void test() {
Circle c1=new Circle(0, 0, 71);
Circle c2=new Circle(0, 76, 2);
/*圆是否包括圆*/
System.out.println("圆包括圆"+c1.collision(c2)); /*矩形的4个点*/
int[][] in=new int[4][2];
in[0][0]=0;
in[0][1]=100; in[1][0]=100;
in[1][1]=0; in[2][0]=200;
in[2][1]=100; in[3][0]=100;
in[3][1]=200; Rectangle rectangle1=new Rectangle(100, 100, in);
/*矩形是否包括圆*/
System.out.println("矩形是否包括圆:"+(rectangle1.collision(c1)));
/*圆包括矩形*/
System.out.println("圆包括矩形:"+(c1.collision(rectangle1)));
/*矩形形包括点*/
System.out.println("矩形形包括点:"+rectangle1.contains(55, 49)); /*矩形的4个点*/
int[][] in2=new int[4][2];
in2[0][0]=0;
in2[0][1]=44; in2[1][0]=0;
in2[1][1]=0; in2[2][0]=44;
in2[2][1]=0; in2[3][0]=44;
in2[3][1]=44; Rectangle rectangle2=new Rectangle(24, 24, in2);
long start=System.currentTimeMillis();
rectangle2.collision(rectangle1);
for(int i=0;i<100000;i++){
//c1.collision(c2);
//c1.collision(rectangle1);
rectangle2.collision(rectangle1);
}
long end =System.currentTimeMillis();
System.out.println((end-start));
System.out.println("矩形包括矩形:"+rectangle2.collision(rectangle1));
}
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
游戏碰撞OBB算法(java代码)的更多相关文章
- CCF认证201803-2 碰撞的小球 java代码实现。
问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处.有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒. 当小球到达线 ...
- 排序算法Java代码实现(一)—— 选择排序
以下几篇随笔都是记录的我实现八大排序的代码,主要是贴出代码吧,讲解什么的都没有,主要是为了方便我自己复习,哈哈,如果看不明白,也不要说我坑哦! 本片分为两部分代码: 常用方法封装 排序算法里需要频繁使 ...
- 算法-java代码实现基数排序
基数排序 第11节 基数排序练习题 对于一个int数组,请编写一个基数排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组.保证元素均小于等于2000. 测试样例: [1 ...
- 算法-java代码实现计数排序
计数排序 第10节 计数排序练习题 对于一个int数组,请编写一个计数排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3], ...
- 算法-java代码实现希尔排序
希尔排序 第8节 希尔排序练习题 对于一个int数组,请编写一个希尔排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组.保证元素小于等于2000. 测试样例: [1,2 ...
- 算法-java代码实现堆排序
堆排序 第7节 堆排序练习题 对于一个int数组,请编写一个堆排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3],6 [1,2 ...
- 算法-java代码实现快速排序
快速排序 对于一个int数组,请编写一个快速排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3],6 [1,2,2,3,3,5] ...
- 算法-java代码实现归并排序
归并排序 对于一个int数组,请编写一个归并排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3],6 [1,2,2,3,3,5] ...
- 排序算法Java代码实现(二)—— 冒泡排序
本篇内容: 冒泡排序 冒泡排序 算法思想: 冒泡排序的原理是:从左到右,相邻元素进行比较. 每次比较一轮,就会找到序列中最大的一个或最小的一个.这个数就会从序列的最右边冒出来. 代码实现: /** * ...
随机推荐
- 使用Android简单实现有道电子词典
前言: 毕业设计的内容,仅仅有Java基础.没学过Android. 本着用到什么学什么.花费了10多个晚上完毕毕业设计. 当然,仅仅是简单的实线了电子词典功能,自始至终没有考虑过性能等问题. 本电子词 ...
- 介绍linux下Source Insight强大代码编辑器sublime_text_3
背景 1 一. 运行环境 1 二.安装环境配置 1 三.创建快捷方式 1 四.配置全局环境 2 五.操作界面 3 背景 在windows操作系统系统下,文本代码编辑器众多,各路英雄豪杰争相写了许多强大 ...
- 理解Lambda表达式
1.什么是Lambda表达式 Lambda表达式是一个匿名方法,通常在LINQ中被用来创建委托 简单来说.它是一个没有声明,没有访问修饰符,没有返回值.甚至没有名字的方法. 2.为什么我们需要使用La ...
- NetBeans工具学习之道:NetBeans的(默认)快捷键
没什么好介绍的,是netbeans的快捷键,比較全面.看到好多坛子里还在问eclipse下的这个快捷键怎么netbeans下没有呢.曾经收集的,如今列在以下: 事实上,在当前安装的netbeans的 ...
- Cocos2d-x-lua游戏两个场景互相切换MainScene01切换到MainScene02
/* 场景一lua代码 */ require "MainScene02" local dic_size = CCDirector:sharedDirector():getWinSi ...
- Domain Model(领域模型)
Domain Model(领域模型) 上一篇:<DDD 领域驱动设计-如何 DDD?> 开源地址:https://github.com/yuezhongxin/CNBlogs.Apply. ...
- 为何要fork()两次来避免产生僵尸进程?
为何要fork()两次来避免产生僵尸进程? 当我们只fork()一次后,存在父进程和子进程.这时有两种方法来避免产生僵尸进程: 父进程调用waitpid()等函数来接收子进程退出状态. 父进程先结 ...
- DOM中的动态NodeList与静态NodeList
GitHub版本号: https://github.com/cncounter/translation/blob/master/tiemao_2014/NodeList/NodeList.md 副标题 ...
- Linux scp文件复制
scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 1.scp命令的用处: scp在网络上不同的主机之间复制文件,它使用ssh安全协议传输 ...
- hosts文件导致无法访问网站
前段时间有人反映无论怎么样都无法在自己的电脑上访问法兰克官网,那台电脑的DNS也无法解析,通过查看hosts文件后发现,原来该电脑的hosts文件木马修改过了,屏蔽了相关的域名,删除新增的或者用其他机 ...