有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java游戏碰撞检查当矩形从一个位置移动到另一个位置时,如果它在行驶的距离内未命中碰撞

我知道这个问题和其他问题类似,但如果我有一个矩形边界的游戏对象。移动位置如何沿直线检查它是否与中间的任何项目相交

在极端情况下。[x=2,x=1,宽度=1,高度=1]A移动到[x=4,y=1,宽度=1,高度=1]。如果矩形B存在于[3,1,0.5,0.5]处,它将被忽略

我读过关于标量和叉积的书,但如果我读得正确的话,它们是单行的。这是因为Android游戏在低帧速率的慢速设备上开发。我让它掉进物体里。我使用下面的代码检查相交

public boolean testIntersection(GameVector lowerLeftMain, float mainWidth, float            mainHeight, GameVector lowerLeftCollider,
float colliderWidth, float colliderHeight){

    boolean intersect = false;

    if(lowerLeftMain.x < lowerLeftCollider.x + colliderWidth+0.08f && //checks left collision
            lowerLeftMain.x + mainWidth > lowerLeftCollider.x-0.08f && //checks right collision
            lowerLeftMain.y < lowerLeftCollider.y + colliderHeight+0.08f &&//checks top collision
            lowerLeftMain.y + mainHeight > lowerLeftCollider.y-0.08f )//checks bottom collision
        intersect = true;
    return intersect;
}

请有人给我指出正确的方向,我是否应该放弃矩形而专注于光线投射线碰撞样式

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    感谢链接伟大的链接将张贴我的代码,以帮助其他人在未来

    java中的分离轴定理。只是为了测试是否重叠。我之所以选择这个算法,是因为它的效率和潜力,可以看到最小和最大重叠向量

    public GameVector[] getVertices(GameObject obj){
        final GameVector topLeft = new GameVector( obj.mPosition.x-0.06f - (obj.mWidth/2), obj.mPosition.y+0.06f +(obj.mHeight/2) );
        final GameVector topRight = new GameVector(   obj.mPosition.x+0.06f + (obj.mWidth/2),obj.mPosition.y+0.06f +(obj.mHeight/2) );
        final GameVector bottomLeft = new GameVector(  obj.mPosition.x-0.06f - (obj.mWidth/2), obj.mPosition.y-0.06f -(obj.mHeight/2));
        final GameVector bottomRight = new GameVector(  obj.mPosition.x+0.06f + (obj.mWidth/2), obj.mPosition.y-0.06f -(obj.mHeight/2));
    
        //order here matters
        GameVector[] vertices = { topLeft, topRight, bottomRight, bottomLeft }; 
        return vertices;
    }
    
    public GameVector[] getAxis(GameObject shape){
    
        GameVector[] vertices = getVertices(shape);
    
        GameVector[] axes = new GameVector[vertices.length];
        // loop over the vertices
        for (int i = 0; i < vertices.length; i++) {
            // get the current vertex
            GameVector p1 = vertices[i];
            // get the next vertex if i+1 == vertices length set back to vertices [0]
            GameVector p2 = vertices[i + 1 == vertices.length ? 0 : i + 1];
            // subtract the two to get the edge vector
            GameVector edge = p1.subtract(p2.x, p2.y);
            // get either perpendicular vector
            GameVector normal;
            //get the left side normal of the vector due to clock wise positions
            normal = new GameVector(edge.y, -edge.x);//edge.perp();
            axes[i] = normal;
        }
        return axes;
    }
    
    public float dotProduct(GameVector a, GameVector b){
        float dp = a.x*b.x + a.y*b.y;
        return dp;
    }
    
    public class Projection {
    
        private final float min;
        private final float max;
    
        public Projection(float min, float max) {
            this.min = min;
            this.max = max;
        }
    
        public boolean doesOverlap(final Projection other) {
            return !(this.min > other.max || other.min > this.max);
    
        }
    }