简体   繁体   中英

How should I alter my collision detection for a rotating line?

I create a rectangle for my "line", it's a rotating 'laser sight'

public Rectangle getLaserBox() {
    float lOriginX = (leon.getPosition().x + 0.27f);
    float lOriginY = (leon.getPosition().y + 0.7f);
    float lEndX = lOriginX + (float)Math.cos((leonAimLaserSprite.getRotation())/57) * 5f;
    float lEndY = lOriginY + (float)Math.sin((leonAimLaserSprite.getRotation())/57) * 5f;
    Rectangle laserBox = new Rectangle(lOriginX, lOriginY, lEndX, lEndY);
    return laserBox;
}

Then I have a method that checks for overlap of the rectangles and is supposed to shorten the 'laser sight' sprite if overlap is detected. I call the laserCol() method in my render method for now (I know I'm breaking MVC, just trying to get it working), and my laserWidth is applied as the width of the laser sprite.

private float laserWidth;

public void laserCol() {
    Vector2 laserOrigin = new Vector2(leon.getPosition().x + 0.55f, leon.getPosition().y + 0.7f);
    boolean laserIsCol = false;
    for (Tile t : world.getTiles()) {     //pulling in tiles as t, from world method getTiles()
        laserWidth = laserOrigin.dst(t.getPosition().x + 0.1f, t.getPosition().y + 0.7f);
        if (Intersector.overlapRectangles(getLaserBox(), t.getBounds())) {
            laserIsCol = true;
        }
    }
    if (laserIsCol) {
        for (Tile t : world.getTiles()) {     //pulling in tiles as t, from world method getTiles()
            laserWidth = laserOrigin.dst(t.getPosition().x, t.getPosition().y + t.getBounds().y);
        }
    }
    if (!laserIsCol) {
        laserWidth = 8f;
    }
}

But as you can see in the screenshot, the laser does not shorten. I've looked at other examples but can't seem to understand a better way to do this. 在此输入图像描述

So after adding the single object which I called thing in my code, I decided to check my sprite boundingbox I created and it looked like this.

在此输入图像描述

After that, I changed some code, and am trying to use a ray, I've gotten it to work somewhat but it's not as close as I'd like, any suggestions?

public Ray getLaserRay() {
    lOriginX = (leon.getPosition().x + 0.27f);
    lOriginY = (leon.getPosition().y + 0.7f);
    lEndX = lOriginX + (float)Math.cos((leonAimLaserSprite.getRotation())/57) * 10f;
    lEndY = lOriginY + (float)Math.sin((leonAimLaserSprite.getRotation())/57) * 10f;
    laserO = new Vector3(lOriginX, lOriginY, 0);
    laserD = new Vector3(lEndX, lEndY, 0);
    Ray laserRay = new Ray(laserO, laserD);
    return laserRay;
}


private float laserWidth;

public void laserCol() {
    Vector2 laserOrigin = new Vector2(leon.getPosition().x + 0.55f, leon.getPosition().y + 0.7f);
    boolean laserIsCol = false;
        if (Intersector.intersectRayBoundsFast(getLaserRay(), thing.getBB())) {
            laserIsCol = true;
        }
    if (laserIsCol) {
            laserWidth = laserOrigin.dst(thing.getPosition().x, thing.getPosition().y);
        }
    if (!laserIsCol) {
        laserWidth = 10f;
    }
}

在此输入图像描述在此输入图像描述

I ended up using 2 line segments to get the collision detection to work. I made one for the laser sight, and one for my object(enemy), that extended from the lower x point to the top x point. Basically the same code except I used the segment intersector from libgdx.

Anyone have an idea of how to make a bullet or damage happen at laser sight spot?

在此输入图像描述

Without having more information I just can guess, but I assume the problem might be that you're calculating the width using all the tiles in your list/array. Instead, you should just use the tile the laser collides with to calculate the width.

Something like this:

laserWidth = 8f; //default if no collision is found
for (Tile t : world.getTiles()) {     //pulling in tiles as t, from world method getTiles()
    if (Intersector.overlapRectangles(getLaserBox(), t.getBounds())) {
        laserWidth = laserOrigin.dst(t.getPosition().x, t.getPosition().y + t.getBounds().y);
        break;
    }
}

Please note that I just reused your code, I don't know if this is correct or if there are some wrong calculations in it. As I said in my comment, you should debug the calculations and look where they start to get wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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