zoukankan      html  css  js  c++  java
  • 游戏碰撞OBB算法(java代码)


     业务需求
         游戏2D型号有圆形和矩形,推断说白了就是碰撞检测 :
         1.圆形跟圆形是否有相交
         2.圆形跟矩形是否相交 
         3.矩形和矩形是否相交
         
         先明确要实现的原理,才干有思路写代码
         第1个最好推断,推断两个圆中心点的矩形是否小于这两个圆的半径之和
         第2个纠结了我一下,只是也不难先看图圆跟矩形关系有4种情况,例如以下图

     仅仅要推断圆心到矩形4条边的距离都小于圆的半径或者圆心在矩形内则它们相交,还要推断圆心在矩形内是防止出现上面第四张图那样的特殊情况
    圆心到边的距离有例如以下两种情况

    两个箭头表示点到线段的距离,另外一种情况是特殊情况
    第3个推断。有下面几种情况

    我发现有个规律,两个矩形相交必定有一方的顶点在对方的图形内
    推断矩形是否包括某个点这个简单:仅仅要推断点到矩形4条边的距离之和是否等于矩形长与宽的和(高中数学知识)
    只是推断上面这些比不是说实现就完结了,还要考虑性能问题,游戏对性能要求高。并且有对应的算法,AABB,OBB算法
    我仅仅讲OBB碰撞

    方向包围盒(Oriented bounding box),简称OBB。方向包围盒类似于AABB。可是具有方向性、能够旋转。AABB不能旋转。

    如图3所看到的。



    推断两个图形(凸边形)是否相离:肯定有一条直线能让这两个图形在这条直线上的投影是不相交(换个说法:仅仅要有一条直线能把他们分离出来)例如以下图


    推断公式:BE>(BC+DE)=(1/2AC+1/2DF)=1/2(AC+DF)      ---》 BE>1/2(AC+DF) 可推出两个矩形是分离了;
    上图是两个矩形,o1和o2分别为两个矩形的中心,x直线是平行于A1A2的一条直线,你能够把他看做坐标轴x,当中x跟y直线垂直,非常明显有一条直线y把两个矩形分开了所以这两个矩形是不相交的
    由于A1A2平行于x轴,所以矩形1在直线x轴的投影的长度为线段AC, 矩形2在x轴的投影为DF,仅仅要推断AC跟DF不相交,说明肯定有一条直线能把他们分离出来,如图中的y轴
    还有个更加简单的方法,那就是这两个矩形的中心点连线o1o2的投影是否大于 两个矩形在x轴的投影的绝对值的一半的和   o1o2在x轴的投影为BE  仅仅要推断 BE>(BC+DE)=(1/2AC+1/2DF)=1/2(AC+DF) 就说明这两个矩形在x轴(也相当于直线A3A4,A1A2)的投影不相交,x轴不是真正意义上的x轴,我仅仅是让大家看得明了一点。

    由于矩形是有规则的图形,仅仅要分别给他们检測矩形每一条边的垂直线的投影就能够了,两个矩形总共8条边,由于每一个矩形的边都有两两平行,和两两垂直。所以我们仅仅要检測两个矩形到每一个矩形相邻两条边的投影是否相交,仅仅要4次检測中有当中一次两个矩形的投影没有相交就能够推断这两个矩形没有相交,反之就是相交

    由于要用到高中数学知识向量运算,数学公式:
                                                             公式一:
                                      两点 A(x1,y1) 。B(x2,y2)
                                      向量a=AB=(x2-x1,y2-y1) 。

                                     公式二:
                                      向量a=(xa,ya),b=(xb,yb)
                                      cosA=a*b/(|a|*|b|) 
                                                               向量a到B的投影=     |a|*cosA=|a|*( a*b/(|a|*|b|) )=  (a.b)/|b|
                                      a*b=xa*xb+ya*yb;
                                     |b|=sqrt(xb*xb+yb*yb);
                                      投影=(a.b)/|b|=(xa*xb+ya*yb)/sqrt(xb*xb+yb*yb)
                                     注意 :我们须要的投影我们仅仅取正数 
     求矩形在一条直线的投影大小=矩形相邻两条边在直线上的投影的绝对值的和;
    比方上图矩形B1B2B3B4在x投影=|B3B1在x轴投影|+|B1B2在x轴投影|=+|B1B2在x轴投影|+|B2B4在x轴投影|
    上图我就不加画了,画多了花,你们看一下也应该明确,图上的x轴换成直线A1A2或者A3A4是一样的效果。所以大家千万别被这个迷惑,我仅仅是想让大家看明确点

    原理明确了。代码就不是问题了。你学什么语言就用什么语言,由于我学的是java,那我就用java实现
    一下是我的源代码,大家仅仅要看两个矩形碰撞那里就能够了
    我这是以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));
    	}
    }
    




    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    使用 Visual Studio 来调试 dll 文件
    模拟CRC达到反调试的目的
    [CrackMe]一个关于读取文件并进行运算判断的CrackMe
    VT开启前的检测与开启
    一顿操作之后成功在win7 64版本输出VT是否可用
    C#3.0新增功能09 LINQ 基础06 LINQ 查询操作中的类型关系
    C#3.0新增功能09 LINQ 基础05 使用 LINQ 进行数据转换
    C#3.0新增功能09 LINQ 基础04 基本 LINQ 查询操作
    C#3.0新增功能09 LINQ 基础03 LINQ 和泛型类型
    C#3.0新增功能09 LINQ 基础02 LINQ 查询简介
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4916434.html
Copyright © 2011-2022 走看看