简体   繁体   English

具有顶点坐标的3D中的Java碰撞检测

[英]Java Collision detection in 3D with Vertex Coordinates

so I've setup all things to presumably create a collision detection, however I'm quite at a loss as to how I would ckeck for collision.. 因此我已经设置了所有东西以大概创建一个碰撞检测,但是我对于如何处理碰撞感到非常困惑。

So from my object I've got a bunch of variables with which I could work with but for a simple BoundingBox I would probably only need these here: 所以从我的对象来看,我有很多可以使用的变量,但是对于一个简单的BoundingBox,我可能只需要这些变量:

System.out.println("width: " + width);
System.out.println("height: " + height);
System.out.println("depth: " + depth);
System.out.println("center[" + xPos+ "/" +yPos+ "/" +zPos+"]");

So what I want is some code that allows me to ckeck if two BoundingBoxes interesct each other, it shouldn't be too hard to come up with a code for this but I'm really struggling with this one, so I would appreciate any help! 所以我想要的是一些代码,如果两个BoundingBoxes相互交叠,可以让我进行检查,为此想出一个代码应该不难,但是我真的为此而苦苦挣扎,所以我将不胜感激!

I will have two aabb's with mentionned properties: 我将有两个带有提到的属性的aabb:

private void AABB_1()
}
// (width,height,depth,xPos,yPos,zPos)
}

private void AABB_2()
}
// (width,height,depth,xPos,yPos,zPos)
}

In the BoundingBox method I want to check for interesction of the two aabb's and set a boolean to either false or true: 在BoundingBox方法中,我想检查两个aabb的相交并将布尔值设置为false或true:

public void BoundingBox()
{
    AABB_1();
    AABB_2();

    boolean intersection;

// check if AABB_1 and AABB_2 intersect each other
// If yes set intersection = true
// If no set intersection = false     
} 

Here in the BoundingBox Method would be the collision detection but as I said I do not reall know how I would ckeck for intersection, has anyone an idea? 在BoundingBox方法中,这里将是碰撞检测,但是正如我说的那样,我并不知道我将如何处理交叉路口,有人知道吗?

Consider 1D first: 首先考虑一维:

//(minX---maxX)
//Non overlap cases:
//a: |----|       
//b:       |----|

//a:       |----|
//b: |----|

public static void intersectOnX(a,b){
  if (b.minX > a.maxX || a.minX > b.maxX)
    return false;//no intersect
  else
    return true;//intersect
}

Repeat this for every extra dimension: 对每个额外的维度重复此步骤:

public static void intersect(a,b){
  if (!intersectOnX(a,b))
    return false;//no intersect

  if (!intersectOnY(a,b))
    return false;//no intersect

  if (!intersectOnZ(a,b))
    return false;//no intersect

  return true;//intersect!
}

Given that you have a box with a center and three extends, you can also write a Box method 假设您有一个具有一个中心和三个扩展的框,则还可以编写一个Box方法

boolean overlaps( Box other ){
    return
        Math.abs(xPos - other.xPos) <= (width + other.width)/2
        &&
        Math.abs(yPos - other.yPos) <= (depth + other.depth)/2
        &&
        Math.abs(zPos - other.zPos) <= (height + other.height)/2;
}

(Check that x,y,z and width, depth, height correlate.) (检查x,y,z和宽度,深度,高度是否相关。)

If you just have some variables storing these 6 + 6 values, you can write the expression as eg 如果只有一些变量存储这6 + 6个值,则可以将表达式编写为例如

Number x1, y1, z1, w1, d1, h1;
Number x2, y2, z2, w2, d2, h2;

boolean overlaps =
    Math.abs(x1 - x2) <= (w1 + w2)/2
    &&
    Math.abs(y1 - y2) <= (d1 + d2)/2
    &&
    Math.abs(z1 - z2) <= (h1 + h2)/2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM