简体   繁体   English

Java碰撞检测旋转矩形?

[英]Java collision detection for rotated rectangles?

I am writing my first java game and so far: 到目前为止,我正在写我的第一个Java游戏:

I've made a rectangle that can walk around with WSAD, and he always faces where the mouse is pointing. 我已经制作了一个可以与WSAD一起走动的矩形,他总是面对鼠标指向的位置。 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()). 我遇到的问题是,我编写的碰撞检测仅在旋转对象(字符,敌人和子弹)(使用.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. 到目前为止,我一直在为每个对象旋转我的Graphics2D网格,因此实际上并没有旋转它们,只是绘制出来了。 Is there a way I can actually rotate their shapes and then use something like .intersects() ? 有没有一种方法可以让我实际旋转它们的形状,然后使用类似.intersects()的方法?

Any help or suggestions are appreciated. 任何帮助或建议,表示赞赏。

Here is what I use to see if it will collide by moving on the x axis: 这是我用来查看是否会因在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;
}

You can probably use the AffineTransform class to rotate the various objects provided the objects are of type Area. 您可以使用AffineTransform类旋转各种对象,前提是这些对象的类型为Area。

Assume that you have two objects a and b, you can rotate them like this: 假设您有两个对象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
  }

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. 使用AffineTransform可以轻松组合转换,逆转换等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM