Gyo*_*Gyo 2 java collision-detection separating-axis-theorem
因此,我一直在尝试使用分离轴定理在我的游戏项目中使用碰撞检测和响应.我设法发现了碰撞,但是对于我的生活,我无法弄清楚如何应对它.我试图找到最小翻译向量,但我不知道如何使它成为一个实际向量,以便我可以计算如何响应碰撞.我已经阅读了教程后的教程,我在这里看了许多以前问过的问题,但我无法弄清楚如何实现它.我很难理解如何找到和使用MTV,所以如果有人真的可以帮助我理解,也许给我一些例子,这样我就可以在实现中理解它而不仅仅是理论,它会很多感谢,对于给您带来的任何不便,我们深表歉意.我的代码确实成功检测到了碰撞,现在它在下面:
//NOTE: This is within the HitPolygon class, so "this" is referencing itself as a polygon
public Projection project(Vector2D axis){
float min = axis.dot(this.getVertices().get(0));
float max = min;
Vector2D vecMax = new Vector2D(0, 0), vecMin = new Vector2D(0, 0);
for(int i = 1; i < this.getVertices().size(); i++){
float p = axis.dot(this.getVertices().get(i));
if(p < min){
min = p;
vecMin = this.getVertices().get(i);
}
if(p > max){
max = p;
vecMax = this.getVertices().get(i);
}
}
Projection result = new Projection(min, max, vecMin, vecMax, axis);
return result;
}
public boolean contains(Projection p1, Projection p2){
return(p1.min >= p2.min && p1.min <= p2.max) || (p1.max >= p2.min && p1.max <= p2.max);
}
public boolean overlap(Projection a, Projection b){
if(contains(a, b)) return true;
if(contains(b, a)) return true;
return false;
}
public boolean collide(HitPolygon b){
ArrayList<Vector2D> axes1 = this.getAxes(0);
ArrayList<Vector2D> axes2 = b.getAxes(0);
for(int i = 0; i < axes1.size(); i++){
Vector2D axis = axes1.get(i);
Projection p1 = this.project(axis), p2 = b.project(axis);
if(!overlap(p1, p2)) return false;
else{
float start = p1.min > p2.min ? p1.min : p2.min;
float end = p1.max < p2.max ? p1.max : p2.max;
float translation = end - start;
//translation might be the MTV, more or less, but it's not a vector and I don't know how to turn it into one.
}
}
for(int i = 0; i < axes2.size(); i++){
Vector2D axis = axes2.get(i);
Projection p1 = this.project(axis), p2 = b.project(axis);
if(!overlap(p2, p1)) return false;
else{
}
}
System.out.println("collide");
return true;
}
Run Code Online (Sandbox Code Playgroud)
这是简单的投影类:
class Projection{
public Vector2D minVec, maxVec;
public float min, max;
public Vector2D axis = new Vector2D(0, 0);
public Projection(float min, float max, Vector2D minVec, Vector2D maxVec, Vector2D axis){
this.min = min;
this.max = max;
this.minVec = minVec;
this.maxVec = maxVec;
this.axis = axis;
}
public float minDot(){
return axis.dot(minVec);
}
public float maxDot(){
return axis.dot(maxVec);
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要任何额外的代码,我很乐意提供它.原谅我对如何查找和使用MTV缺乏了解,但真正感谢任何帮助.谢谢!
实际上它非常简单.您需要知道所使用的轴的角度以及两种物体在每种情况下碰撞的深度(深度是投影碰撞区域的长度).
使用以下技术将每个数据转换为向量:
var vecX = Math.cos(axisAngle) * deepness;
var vecY = Math.sin(axisAngle) * deepness;
Run Code Online (Sandbox Code Playgroud)
然后,将矢量加在一起:
var sumX = vec1X + vec2X + ...;
var sumY = vec1Y + vec2Y + ...;
Run Code Online (Sandbox Code Playgroud)
在这个你得到一个和矢量,这是你需要使用的位移矢量(因为魔法)
| 归档时间: |
|
| 查看次数: |
1087 次 |
| 最近记录: |