Java 误差与分离轴定理
我已经努力实现SAT很长一段时间了,这是我最后的手段,在对代码进行目视检查之后,我似乎正确地获得了我正在检查的形状的面法线(轴),并且正确地将每个顶点投影到每个轴上,然而,SAT返回false,因为它没有检测到重叠,如果有人能为我指出正确的方向,我将不胜感激 SAT班:(发展)Java 误差与分离轴定理,java,collision-detection,separating-axis-theorem,Java,Collision Detection,Separating Axis Theorem,我已经努力实现SAT很长一段时间了,这是我最后的手段,在对代码进行目视检查之后,我似乎正确地获得了我正在检查的形状的面法线(轴),并且正确地将每个顶点投影到每个轴上,然而,SAT返回false,因为它没有检测到重叠,如果有人能为我指出正确的方向,我将不胜感激 SAT班:(发展) 我不确定它的用法,但可能会失去精确性。试着把x和y改成double或BigDecimal。你的意思是只针对Vector类吗?正如我所听说的,在处理对象位置/坐标时,应该使用float,而仅仅是Vector类。然后,基于矢
我不确定它的用法,但可能会失去精确性。试着把x和y改成double或BigDecimal。你的意思是只针对Vector类吗?正如我所听说的,在处理对象位置/坐标时,应该使用float,而仅仅是Vector类。然后,基于矢量的计算将使用双精度(而不是浮点)。只是一个猜测-不确定它是否有用!我不认为这会解决我的问题,好像这是一个不准确的例子SAT应该在一定程度上仍然有效,但你是对的,我应该使用双打
public void obtainEdges(){
for(int i = 0; i < vertices.length; i++){
Vector2 e1 = vertices[i];
Vector2 e2 = vertices[i + 1 == vertices.length ? 0 : i + 1];
Vector2 edge = e1.subtract(e2);
Vector2 perp = edge.perp(edge);
axis[i] = perp;
}
}
public Vector2 projectAxis(Vector2 axis){
float min = axis.dot(vertices[0]);
float max = min;
for(int i = 1; i < vertices.length; i++){
float proj = axis.dot(vertices[i]);
if(proj < min){
min = proj;
}
if(proj > max){
max = proj;
}
}
proj = new Vector2(min,max);
return proj;
}
public boolean separatingAxisTheorem(){
obtainEdges();
for(DevEntity e : handler.getDevWorld().getDevM().getDevEntities()){
if(e.equals(this)){
return false;
}
Vector2[] axes1 = axis;
Vector2[] axes2 = e.axis;
for(int i = 0; i < axes1.length; i++){
Vector2 p1 = projectAxis(axes1[i]);
Vector2 p2 = e.projectAxis(axes1[i]);
if(!p1.overlap(p2)){
return false;
}
}
for(int i = 0; i < axes2.length; i++){
Vector2 p1 = projectAxis(axes2[i]);
Vector2 p2 = e.projectAxis(axes2[i]);
if(!p1.overlap(p2)){
return false;
}
}
}
return true;
}
public class Vector2 {
public float x, y;
public Vector2(float x, float y){
this.x = x;
this.y = y;
}
public Vector2(Vector2 vec){
this.x = vec.x;
this.y = vec.y;
}
public float dot(Vector2 b){
float d = b.x * x + b.y * y;
return d;
}
public Vector2 normalize(Vector2 vec){
float mag = (float) Math.sqrt(vec.x * vec.x + vec.y * vec.y);
Vector2 b = new Vector2(vec.x/mag, vec.y/mag);
return b;
}
public Vector2 subtract(Vector2 vec){
Vector2 s = new Vector2(x - vec.x, y - vec.y);
return s;
}
public Vector2 subtract(float x, float y){
return new Vector2(this.x - x, this.y - y);
}
public Vector2 perp(Vector2 vec){
Vector2 p = new Vector2(-vec.y, vec.x);
return p;
}
public boolean overlap(Vector2 vec){
if(y > vec.x && vec.y > x){
return true;
}
return false;
}