私は過去数日間、ベクトルについて調べたり、検索したりしてきましたが、まだ数学について理解することができません..
私は 2 つの AABBを持っています。衝突時に、メソッドが 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)メソッドの最後に追加すると機能しますが、私が望むほどきれいではありません。また、「他の」ボディに 1 つの軸だけに沿って移動する場合は正常に機能しますが、ボディを斜めに押し込むと、シェイプの内側に入り、ランダムに投げ出されます。
これはおそらく、ステップごとに移動するピクセルが多すぎるためです。ただし、衝突をスイープ テストする必要があります。
(このコードは、投影ベクトルを調べて、どのコンポーネントが大きいかを確認します。次に、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;
}
編集 2
Ishtar の回答の実装により、状況は良好に見えました。しかし、小さなオブジェクトを幅の広いオブジェクトと衝突させた場合、中心付近の衝突は正確に修正されますが、コーナーに近づくと形状に沈み込みます。
このような:
_
_______l_l_________
| |
|______OK___________|
_--________________
| -- |
|_____SINKS IN______|
編集 3
現在の衝突コード:
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
}