gpt4 book ai didi

java - 使用投影 vector 的基本 AABB 碰撞

转载 作者:行者123 更新时间:2023-11-30 04:56:02 26 4
gpt4 key购买 nike

过去几天我一直在四处寻找,并研究 vector ,但仍然无法完全理解数学......

我有两个AABB's 。在发生碰撞时,我希望我的方法返回一个 vector ,然后我可以将其添加到位置 vector 以使我的对象回到边界内。

这是我当前的代码:

(位置 vector 为AABB的中心)

public Vector2f collide(Sprite other) {
if(!collideBoolean(other)) return null; //no collision

float xAxis = Math.abs(this.position.x - other.getX()); //distance between centers on x axis
float yAxis = Math.abs(this.position.y - other.getY()); //distance between centers on y axis

//these combined values show the minimum distance apart the objects need to be to not collide
int cw = this.getHalfWidth() + other.getHalfWidth(); //combined width
int ch = this.getHalfHeight() + other.getHalfHeight(); //combined height

float ox = Math.abs(xAxis - cw); //overlap on x
float oy = Math.abs(yAxis - ch); //overlap on y

//direction
Vector2f dir = new Vector2f(this.position);
dir.sub(other.getPosition()); //subtract other.position from this.position
dir.normalise();

return new Vector2f(dir.x * ox, dir.y * oy);
}

(不言自明,但这里也是 collideBoolean(Sprite other) 的代码)

public boolean collideBoolean(Sprite other) {
//code using halfwidths and centers
if(Math.abs(this.position.x - other.getX()) > (this.getHalfWidth() + other.getHalfWidth())) return false;
if(Math.abs(this.position.y - other.getY()) > (this.getHalfHeight() + other.getHalfHeight())) return false;

return true;
}

我当前的代码或多或少有效。但是与“其他”碰撞的(这个)对象被推出并朝向“其他”最近的角落。

我想我已经很接近了。当然,对于不同的人来说,这将是显而易见的事情,但我无法完全弄清楚。任何帮助将不胜感激!

谢谢

编辑:

将其添加到 collide(Sprite other) 方法的末尾是可行的,只是没有我想要的那么整洁。此外,当仅沿一个轴移动到“另一个”主体时,它工作得很好,但如果你以一定角度插入主体,你就会进入形状内部,它会随机地将你抛出去。

这可能只是因为我每步移动了太多像素,但我应该扫描测试我的碰撞

(此代码查看投影 vector 以查看哪个分量更大,然后 0 表示最大分量​​。这意味着我仅沿着最短路径投影出形状)

    ....
//direction
....

Vector2f projection = new Vector2f(dir.x * (ox+1), dir.y * (oy+1));
if(Math.abs(projection.x) > Math.abs(projection.y)) projection.x = 0;
else if(Math.abs(projection.y) > Math.abs(projection.x)) projection.y = 0;

return projection;
}

编辑两个

随着伊什塔尔答案的实现,事情看起来不错。但我发现,如果我将一个小物体与一个宽物体碰撞,它会准确地修复中心附近的碰撞,但当你离开角落附近时,你就会陷入形状中。

像这样:

         _
_______l_l_________
| |
|______OK___________|


_--________________
| -- |
|_____SINKS IN______|

编辑三个

当前碰撞代码:

public class Collision {

/** fix collision based on mass */
public static void collide(Sprite s1, Sprite s2) {
float xAxis = Math.abs(s1.getX() - s2.getX()); //distance between centers
float yAxis = Math.abs(s1.getY() - s2.getY()); //distance between centers

int cw = s1.getHalfWidth() + s2.getHalfWidth(); //combined width
int ch = s1.getHalfHeight() + s2.getHalfHeight(); //combined height

//early exit
if(xAxis > cw) return;
if(yAxis > ch) return;

float ox = Math.abs(xAxis - cw); //overlap on x
float oy = Math.abs(yAxis - ch); //overlap on y

if(s1.getMass() <= s2.getMass())
fixCollision(s1, s2, ox+1, oy+1); //the +1's make you get out of the shape instead of
else //if(s1.getMass() > s2.getMass()) //correcting you onto the edge where you'll be in constant collision
fixCollision(s2, s1, ox+1, oy+1);
}

/**
* Fixes the collision
* @param s1 : this gets pushed out (should be lower mass)
* @param s2 : this stays where it is
* @param ox : the overlap along the x axis
* @param oy : the overlap along the y axis
*/
private static void fixCollision(Sprite s1, Sprite s2, float ox, float oy) {
//direction
Vector2f dir = new Vector2f(s1.getPosition());
dir.sub(s2.getPosition());
dir.normalise();

Vector2f projection = new Vector2f(dir.x * (ox), dir.y * (oy));
if(Math.abs(projection.x) > Math.abs(projection.y)) projection.x = 0;
else if(Math.abs(projection.y) > Math.abs(projection.x)) projection.y = 0;

if(ox > oy) s1.getPosition().add( new Vector2f(0, dir.y * oy) ); //overlap is bigger on x so project on y
else if(ox < oy) s1.getPosition().add( new Vector2f(dir.x * ox, 0)); //overlap is bigger on x so project on x
else s1.getPosition().add( new Vector2f(dir.x * ox, dir.y * oy)); //corner to corner
}

最佳答案

float ox = Math.abs(xAxis - cw); //overlap on x
float oy = Math.abs(yAxis - ch); //overlap on y

//direction
Vector2f dir = new Vector2f(this.position);
dir.sub(other.getPosition()); //subtract other.position from this.position
dir.normalise();

return new Vector2f(dir.x * ox, dir.y * oy);

返回的平移 vector 将使 ox(x 中重叠)和 oy(y 中重叠)都为零。但这不是您需要的。如果 ox 为 0,则 x 方向上没有重叠,因此根本没有重叠。如果 oy 为 0,同上。因此,您需要找到仅使 ox oy 为零的 vector 。

if (ox > oy )
return new Vector3f(0,dir.y*oy);//top-bottom collision
else if (ox < oy )
return new Vector3f(dir.x*ox,0);//left-right collision
else //if(ox == oy)
return new Vector3f(dir.x*ox,dir.y*oy); //corner-corner collision,
//unlikely with float's.

关于java - 使用投影 vector 的基本 AABB 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8515198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com