缩放旋转边界框

时间:2013-05-28 17:20:39

标签: windows-phone-7 xna-4.0

我最近开始使用XNA开发Windows Phone游戏。我有问题,因为你可能已经猜到了碰撞检测。在查找了可以实现的所有类型的教程后,我决定进行基本的矩形碰撞检测。我有一个旋转精灵和一个方法,每次在Update()方法中计算边界框,所以我知道它的边界框在哪里然后我只是检查框的所有行与另一个精灵的所有行之间的交集框。但是因为我的盒子看起来是正方形的并且我的旋转精灵的纹理是矩形的,我想缩放边界框以便它更接近纹理的大小。这是我计算旋转边界框角落的原因:

        double baseAngle = Math.Atan(this.Height / this.Width);
        double len = Math.Sqrt(this.Height * this.Height / 4 + this.Width * this.Width / 4);

        Vector2 tr = new Vector2((float)(Math.Sin(baseAngle + this.Rotation) * len) + this.Position.X, (float)(Math.Cos(baseAngle + this.Rotation) * len) + this.Position.Y);
        Vector2 tl = new Vector2((float)(Math.Sin(Math.PI - baseAngle + this.Rotation) * len) + this.Position.X, (float)(Math.Cos(Math.PI - baseAngle + this.Rotation) * len) + this.Position.Y);
        Vector2 bl = new Vector2((float)(Math.Sin(Math.PI + baseAngle + this.Rotation) * len) + this.Position.X, (float)(Math.Cos(Math.PI + baseAngle + this.Rotation) * len) + this.Position.Y);
        Vector2 br = new Vector2((float)(Math.Sin(2 * Math.PI - baseAngle + this.Rotation) * len) + this.Position.X, (float)(Math.Cos(2 * Math.PI - baseAngle + this.Rotation) * len) + this.Position.Y);`

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

当你缩放时,它只显示更大的高度和高度相同。边界框与原始边框相同。尝试多层高度和宽度以及计算边界框的比例数。

你不能旋转边界框,你必须使用matrix.class,但你总是可以使用圆形碰撞。

圈子碰撞

int circlesColliding(int x1, int y1, int radius1, int x2, int y2, int radius2) {
    //compare the distance to combined radii
    int dx = x2 - x1;
    int dy = y2 - y1;
    int radii = radius1 + radius2;
    if ((dx * dx) + (dy * dy) < radii * radii) {
        return true;
    } else {
        return false;
    }
}
相关问题