过去几天我一直在四处寻找,并研究过矢量图,但仍然不能完全理解数学...
我有两个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;
}
我当前的代码或多或少有效..但是与“其他”碰撞的(这个)对象被推出并且朝向“其他”的最近角落。
我想我真的很亲密。当然,对于不同的眼睛来说,这将是一件令人眼花缭乱的事情,但我无法解决这个问题。任何帮助将不胜感激!
谢谢,
修改
将它添加到碰撞(Sprite other)方法的末尾,除了不像我想的那样整洁。此外,当沿着一个轴移动到“另一个”身体时,它可以正常工作,但是如果你以一个角度推入身体,你会进入形状并随意抛出它。
这可能只是因为我每步移动太多像素,我应该扫描测试我的碰撞
(此代码查看投影向量以查看哪个组件更大,然后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;
}
编辑两个
随着Ishtar的回答,事情看起来很不错。但是我发现如果我用一个宽物体碰撞一个小物体,它会准确地修复中心附近的碰撞,但当你走近角落时,你会陷入这个形状。
像这样:
_
_______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
}
答案 0 :(得分:1)
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);
返回的平移向量将使ox(在x中重叠)和oy(在y中重叠)为零。但这不是你需要的。如果ox为0,则x方向上没有重叠,因此根本没有重叠。如果oy为0,则表示同意。因此,您需要找到只会使牛或为零的向量。
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.
答案 1 :(得分:1)
要解决“滑动问题”,请执行以下操作:(C#XNA代码)
Vector2 direction = (this.Center - other.Center);
direction.Normalize();
direction.X = (int)Math.Round(direction.X);
direction.Y = (int)Math.Round(direction.Y);
首先,您将获得一个单位向量,其中包含您应该退出的方向。 但是当你靠近角落时,使用它就会变得不可靠。 舍入使最强值为1,另一个为0。
解释为什么要进行舍入:
例如,假设Y轴上的两个对象中心相等,我们向左退出。方向单位矢量是(-1,0)给我们一个可靠的数字来乘以重叠。但随着你越来越接近形状的角落,数字变得更多(-0.88, * )非整数,当相乘时会导致丢失像素并陷入形状。
我希望自那以后做出解释,对这些事情不太好。
顺便说一句,我是OP ^ _ ^ ..感谢所有人的帮助