Java旋转矩形的碰撞检测?

发布于 2024-11-06 03:20:28 字数 1312 浏览 5 评论 0原文

我正在编写我的第一个 java 游戏,到目前为止:

我已经制作了一个可以使用 WSAD 四处走动的矩形,并且他始终面向鼠标指向的地方。此外,如果您单击,他会向您的鼠标指向的地方发射子弹(并且子弹会旋转以面向该方向)。我还制作了跟随你的敌人,他们旋转面向你的角色。我遇到的问题是,我编写的碰撞检测仅检测对象(角色、敌人和子弹)在旋转之前的碰撞(使用 .intersects())。这意味着绘制时它们身体的某些部分会重叠。

我一直在环顾四周,但没有找到任何我理解或可以适用于我的情况的解决方案。到目前为止,我一直在为每个对象旋转 Graphics2D 网格,因此它们实际上并没有被旋转,只是被绘制出来。有没有办法可以实际旋转它们的形状,然后使用类似 .intersects() 的东西?

任何帮助或建议表示赞赏。

这是我用来查看它是否会通过在 x 轴上移动而发生碰撞的方法:

public boolean detectCollisionX(int id, double xMove, double rectXco, double rectYco, int width, int height)
{
    boolean valid=true;
    //create the shape of the object that is moving.
    Rectangle enemyRectangleX=new Rectangle((int)(rectXco+xMove)-enemySpacing,(int)rectYco-enemySpacing,width+enemySpacing*2,height+enemySpacing*2);
    if (rectXco+xMove<0 || rectXco+xMove>(areaWidth-width))
    {
        valid=false;
    }
    if(enemyNumber>0)
    {
        for (int x=0; x<=enemyNumber; x++)
        {
            if (x!=id)
            {
                //enemies and other collidable objects will be stored in collisionObjects[x] as rectangles.
                if (enemyRectangleX.intersects(collisionObjects[x])==true)
                {
                    valid=false;
                }
            }
        }
    }
    return valid;
}

I am writing my first java game and so far:

I've made a rectangle that can walk around with WSAD, and he always faces where the mouse is pointing. Also if you click, he shoots bullets where your mouse is pointing (and the bullets rotate to face that direction). I've also made enemies which follow you around, and they rotate to face towards your character. The problem i am having is that the collision detection I've written is only detecting the collision of the objects (character, enemies and bullets) before their rotation (using .intersects()). This means that some parts of their bodies overlap when drawn.

I've been looking around, and I haven't found any solutions that I understand or can apply to my situation. I've been rotating my Graphics2D grid for each of the objects so far, so they are not actually being rotated, just drawn out to be. Is there a way I can actually rotate their shapes and then use something like .intersects() ?

Any help or suggestions are appreciated.

Here is what I use to see if it will collide by moving on the x axis:

public boolean detectCollisionX(int id, double xMove, double rectXco, double rectYco, int width, int height)
{
    boolean valid=true;
    //create the shape of the object that is moving.
    Rectangle enemyRectangleX=new Rectangle((int)(rectXco+xMove)-enemySpacing,(int)rectYco-enemySpacing,width+enemySpacing*2,height+enemySpacing*2);
    if (rectXco+xMove<0 || rectXco+xMove>(areaWidth-width))
    {
        valid=false;
    }
    if(enemyNumber>0)
    {
        for (int x=0; x<=enemyNumber; x++)
        {
            if (x!=id)
            {
                //enemies and other collidable objects will be stored in collisionObjects[x] as rectangles.
                if (enemyRectangleX.intersects(collisionObjects[x])==true)
                {
                    valid=false;
                }
            }
        }
    }
    return valid;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀梦 2024-11-13 03:20:28

您可以使用 AffineTransform 类来旋转各种对象,前提是这些对象是 Area 类型。

假设你有两个对象 a 和 b,你可以像这样旋转它们:

  AffineTransform af = new AffineTransform();
  af.rotate(Math.PI/4, ax, ay);//rotate 45 degrees around ax, ay

  AffineTransform bf = new AffineTransform();
  bf.rotate(Math.PI/4, bx, by);//rotate 45 degrees around bx, by

  ra = a.createTransformedArea(af);//ra is the rotated a, a is unchanged
  rb = b.createTransformedArea(bf);//rb is the rotated b, b is unchanged

  if(ra.intersects(rb)){
    //true if intersected after rotation
  }

并且你有原始对象,以防万一这就是你想要的。使用 AffineTransform 可以轻松组合变换、反转变换等。

You can probably use the AffineTransform class to rotate the various objects provided the objects are of type Area.

Assume that you have two objects a and b, you can rotate them like this:

  AffineTransform af = new AffineTransform();
  af.rotate(Math.PI/4, ax, ay);//rotate 45 degrees around ax, ay

  AffineTransform bf = new AffineTransform();
  bf.rotate(Math.PI/4, bx, by);//rotate 45 degrees around bx, by

  ra = a.createTransformedArea(af);//ra is the rotated a, a is unchanged
  rb = b.createTransformedArea(bf);//rb is the rotated b, b is unchanged

  if(ra.intersects(rb)){
    //true if intersected after rotation
  }

and you have the original objects just in case thats what you want. Using the AffineTransform makes it easy to combine transformations, inverse them etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文