Example usage for com.badlogic.gdx.math.collision BoundingBox intersects

List of usage examples for com.badlogic.gdx.math.collision BoundingBox intersects

Introduction

In this page you can find the example usage for com.badlogic.gdx.math.collision BoundingBox intersects.

Prototype

public boolean intersects(BoundingBox b) 

Source Link

Document

Returns whether the given bounding box is intersecting this bounding box (at least one point in).

Usage

From source file:com.quadbits.gdxhelper.actors.ParticleEffectActor.java

License:Apache License

protected boolean isWithinScreenBounds() {
    if (effect == null) {
        return false;
    }/*from   w ww.j av  a2  s .  c o  m*/

    BoundingBox boundingBox = effect.getBoundingBox();
    boundingBox.min.z = 0; // HACK! to make the bounding box valid
    boundingBox.max.z = 1; // HACK! to make the bounding box valid

    // Set stage's viewport coordinates
    float screenWidth = getStage().getViewport().getScreenWidth();
    float screenHeight = getStage().getViewport().getScreenHeight();
    screenBoundingBox.clr().ext(0, 0, 0).ext(screenWidth, screenHeight, 1); // HACK! z = 1 to make bounding box valid

    return boundingBox.intersects(screenBoundingBox);
}

From source file:net.guerra24.voxel.client.world.physics.PhysicsSystem.java

License:Open Source License

public void move(Vector3f dir, BoundingBox currentBox, Vector3f velocity) {

    BoundingBox newBox = currentBox.set(
            new Vector3(currentBox.min.x + dir.x, currentBox.min.y + dir.y, currentBox.min.z + dir.z),
            new Vector3(currentBox.max.x + dir.x, currentBox.max.y + dir.y, currentBox.max.z + dir.z));
    List<BoundingBox> boxes = world.getGlobalBoundingBox(currentBox.ext(newBox));
    Vector3 temp = new Vector3(0, 0, 0);
    Vector3 end = new Vector3(0, 0, 0);
    float precision = (float) dir.length() * 99 + 1;
    boolean colisionX = false, colisionY = false, colisionZ = false;

    for (int i = 1; i < precision; i++) {
        float avance = (i / precision);
        if (!colisionX) {
            temp.set(avance * dir.getX(), end.y, end.z);
            BoundingBox check = currentBox.set(
                    new Vector3(currentBox.min.x + temp.x, currentBox.min.y + temp.y,
                            currentBox.min.z + temp.z),
                    new Vector3(currentBox.max.x + temp.x, currentBox.max.y + temp.y,
                            currentBox.max.z + temp.z));

            for (BoundingBox box : boxes) {
                if (check.intersects(box) || box.intersects(check)) {
                    colisionX = true;//from w  w  w .  j  av a2s .c o  m
                    break;
                }
            }

            if (!colisionX) {
                end.set(temp.x, end.y, end.z);
            }
        }

        if (!colisionY) {
            temp.set(end.x, avance * dir.getY(), end.z);
            BoundingBox check = currentBox.set(
                    new Vector3(currentBox.min.x + temp.x, currentBox.min.y + temp.y,
                            currentBox.min.z + temp.z),
                    new Vector3(currentBox.max.x + temp.x, currentBox.max.y + temp.y,
                            currentBox.max.z + temp.z));

            for (BoundingBox box : boxes) {
                if (check.intersects(box) || box.intersects(check)) {
                    colisionY = true;
                    break;
                }
            }

            if (!colisionY) {
                end.set(end.x, temp.y, end.z);
            }
        }
        if (!colisionZ) {
            temp.set(end.x, end.y, avance * dir.getZ());
            BoundingBox check = currentBox.set(
                    new Vector3(currentBox.min.x + temp.x, currentBox.min.y + temp.y,
                            currentBox.min.z + temp.z),
                    new Vector3(currentBox.max.x + temp.x, currentBox.max.y + temp.y,
                            currentBox.max.z + temp.z));

            for (BoundingBox box : boxes) {
                if (check.intersects(box) || box.intersects(check)) {
                    colisionZ = true;
                    break;
                }
            }

            if (!colisionZ) {
                end.set(end.x, end.y, temp.z);
            }
        }
    }
    if (colisionX)
        velocity.x = 0;
    if (colisionY)
        velocity.y = 0;
    if (colisionZ)
        velocity.z = 0;
}