Java Code Examples for com.jme3.bounding.BoundingBox#getCenter()

The following examples show how to use com.jme3.bounding.BoundingBox#getCenter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBatchNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void updateBoindPoints(Vector3f[] points) {
    BoundingBox bb = (BoundingBox) batch.getWorldBound();
    float xe = bb.getXExtent();
    float ye = bb.getYExtent();
    float ze = bb.getZExtent();
    float x = bb.getCenter().x;
    float y = bb.getCenter().y;
    float z = bb.getCenter().z;

    points[0].set(new Vector3f(x - xe, y - ye, z - ze));
    points[1].set(new Vector3f(x - xe, y + ye, z - ze));
    points[2].set(new Vector3f(x + xe, y + ye, z - ze));
    points[3].set(new Vector3f(x + xe, y - ye, z - ze));

    points[4].set(new Vector3f(x + xe, y - ye, z + ze));
    points[5].set(new Vector3f(x - xe, y - ye, z + ze));
    points[6].set(new Vector3f(x - xe, y + ye, z + ze));
    points[7].set(new Vector3f(x + xe, y + ye, z + ze));
}
 
Example 2
Source File: OrientedBoxProbeArea.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean intersectsBox(BoundingBox box, TempVars vars) {

    Vector3f axis1 = getScaledAxis(0, vars.vect1);
    Vector3f axis2 = getScaledAxis(1, vars.vect2);
    Vector3f axis3 = getScaledAxis(2, vars.vect3);

    Vector3f tn = vars.vect4;
    Plane p = vars.plane;
    Vector3f c = box.getCenter();

    p.setNormal(0, 0, -1);
    p.setConstant(-(c.z + box.getZExtent()));
    if (!insidePlane(p, axis1, axis2, axis3, tn)) return false;

    p.setNormal(0, 0, 1);
    p.setConstant(c.z - box.getZExtent());
    if (!insidePlane(p, axis1, axis2, axis3, tn)) return false;


    p.setNormal(0, -1, 0);
    p.setConstant(-(c.y + box.getYExtent()));
    if (!insidePlane(p, axis1, axis2, axis3, tn)) return false;

    p.setNormal(0, 1, 0);
    p.setConstant(c.y - box.getYExtent());
    if (!insidePlane(p, axis1, axis2, axis3, tn)) return false;

    p.setNormal(-1, 0, 0);
    p.setConstant(-(c.x + box.getXExtent()));
    if (!insidePlane(p, axis1, axis2, axis3, tn)) return false;

    p.setNormal(1, 0, 0);
    p.setConstant(c.x - box.getXExtent());
    if (!insidePlane(p, axis1, axis2, axis3, tn)) return false;

    return true;

}
 
Example 3
Source File: SpotLight.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean intersectsBox(BoundingBox box, TempVars vars) {
    if (this.spotRange > 0f) {
        // Check spot range first.
        // Sphere v. box collision
        if (!Intersection.intersect(box, position, spotRange)) {
            return false;
        }
    }
    
    Vector3f otherCenter = box.getCenter();
    Vector3f radVect = vars.vect4;
    radVect.set(box.getXExtent(), box.getYExtent(), box.getZExtent());
    float otherRadiusSquared = radVect.lengthSquared();
    float otherRadius = FastMath.sqrt(otherRadiusSquared);
    
    // Check if sphere is within spot angle.
    // Cone v. sphere collision.
    Vector3f E = direction.mult(otherRadius * outerAngleSinRcp, vars.vect1);
    Vector3f U = position.subtract(E, vars.vect2);
    Vector3f D = otherCenter.subtract(U, vars.vect3);

    float dsqr = D.dot(D);
    float e = direction.dot(D);

    if (e > 0f && e * e >= dsqr * outerAngleCosSqr) {
        D = otherCenter.subtract(position, vars.vect3);
        dsqr = D.dot(D);
        e = -direction.dot(D);

        if (e > 0f && e * e >= dsqr * outerAngleSinSqr) {
            return dsqr <= otherRadiusSquared;
        } else {
            return true;
        }
    }
    
    return false;
}
 
Example 4
Source File: WireBox.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create a geometry suitable for visualizing the specified bounding box.
 *
 * @param bbox the bounding box (not null)
 * @return a new Geometry instance in world space
 */
public static Geometry makeGeometry(BoundingBox bbox) {
    float xExtent = bbox.getXExtent();
    float yExtent = bbox.getYExtent();
    float zExtent = bbox.getZExtent();
    WireBox mesh = new WireBox(xExtent, yExtent, zExtent);
    Geometry result = new Geometry("bounding box", mesh);

    Vector3f center = bbox.getCenter();
    result.setLocalTranslation(center);

    return result;
}
 
Example 5
Source File: PMDMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BoundingVolume getBound() {
    BoundingBox bb = (BoundingBox)super.getBound();
    BoundingBox bb2 = new BoundingBox(bb.getCenter(), bb.getXExtent()*2, bb.getYExtent()*2,
            bb.getZExtent()*2);
    BoundingBox bb3 = new BoundingBox(bb.getCenter().ZERO,5,5,5);
    return bound;
}
 
Example 6
Source File: UVCoordinatesGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This method returns the bounding sphere of the given mesh.
 * 
 * @param mesh
 *            the mesh
 * @return bounding sphere of the given mesh
 */
/* package */static BoundingSphere getBoundingSphere(Mesh mesh) {
    mesh.updateBound();
    BoundingVolume bv = mesh.getBound();
    if (bv instanceof BoundingBox) {
        BoundingBox bb = (BoundingBox) bv;
        float r = Math.max(bb.getXExtent(), bb.getYExtent());
        r = Math.max(r, bb.getZExtent());
        return new BoundingSphere(r, bb.getCenter());
    } else if (bv instanceof BoundingSphere) {
        return (BoundingSphere) bv;
    } else {
        throw new IllegalStateException("Unknown bounding volume type: " + bv.getClass().getName());
    }
}
 
Example 7
Source File: TriangulatedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This method converts the given point into 3D UV coordinates.
 * 
 * @param boundingBox
 *            the bounding box of the mesh
 * @param point
 *            the point to be transformed
 * @param uvs
 *            the result UV coordinates
 */
private void toTextureUV(BoundingBox boundingBox, Vector3f point, float[] uvs) {
    uvs[0] = (point.x - boundingBox.getCenter().x) / (boundingBox.getXExtent() == 0 ? 1 : boundingBox.getXExtent());
    uvs[1] = (point.y - boundingBox.getCenter().y) / (boundingBox.getYExtent() == 0 ? 1 : boundingBox.getYExtent());
    uvs[2] = (point.z - boundingBox.getCenter().z) / (boundingBox.getZExtent() == 0 ? 1 : boundingBox.getZExtent());
    // UVS cannot go outside <0, 1> range, but since we are generating texture for triangle envelope it might happen that
    // some points of the envelope will exceet the bounding box of the mesh thus generating uvs outside the range
    for (int i = 0; i < 3; ++i) {
        uvs[i] = FastMath.clamp(uvs[i], 0, 1);
    }
}
 
Example 8
Source File: BIHNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public final int intersectWhere(Collidable col,
            BoundingBox box,
            Matrix4f worldMatrix,
            BIHTree tree,
            CollisionResults results) {

        TempVars vars = TempVars.get();
        ArrayList<BIHStackData> stack = vars.bihStack;
        stack.clear();

        float[] minExts = {box.getCenter().x - box.getXExtent(),
            box.getCenter().y - box.getYExtent(),
            box.getCenter().z - box.getZExtent()};

        float[] maxExts = {box.getCenter().x + box.getXExtent(),
            box.getCenter().y + box.getYExtent(),
            box.getCenter().z + box.getZExtent()};

        stack.add(new BIHStackData(this, 0, 0));

        Triangle t = new Triangle();
        int cols = 0;

        stackloop:
        while (stack.size() > 0) {
            BIHNode node = stack.remove(stack.size() - 1).node;

            while (node.axis != 3) {
                int a = node.axis;

                float maxExt = maxExts[a];
                float minExt = minExts[a];

                if (node.leftPlane < node.rightPlane) {
                    // means there's a gap in the middle
                    // if the box is in that gap, we stop there
                    if (minExt > node.leftPlane
                            && maxExt < node.rightPlane) {
                        continue stackloop;
                    }
                }

                if (maxExt < node.rightPlane) {
                    node = node.left;
                } else if (minExt > node.leftPlane) {
                    node = node.right;
                } else {
                    stack.add(new BIHStackData(node.right, 0, 0));
                    node = node.left;
                }
//                if (maxExt < node.leftPlane
//                 && maxExt < node.rightPlane){
//                    node = node.left;
//                }else if (minExt > node.leftPlane
//                       && minExt > node.rightPlane){
//                    node = node.right;
//                }else{

//                }
            }

            for (int i = node.leftIndex; i <= node.rightIndex; i++) {
                tree.getTriangle(i, t.get1(), t.get2(), t.get3());
                if (worldMatrix != null) {
                    worldMatrix.mult(t.get1(), t.get1());
                    worldMatrix.mult(t.get2(), t.get2());
                    worldMatrix.mult(t.get3(), t.get3());
                }

                int added = col.collideWith(t, results);

                if (added > 0) {
                    int index = tree.getTriangleIndex(i);
                    int start = results.size() - added;

                    for (int j = start; j < results.size(); j++) {
                        CollisionResult cr = results.getCollisionDirect(j);
                        cr.setTriangleIndex(index);
                    }

                    cols += added;
                }
            }
        }
        vars.release();
        return cols;
    }
 
Example 9
Source File: BIHNode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public final int intersectWhere(Collidable col,
            BoundingBox box,
            Matrix4f worldMatrix,
            BIHTree tree,
            CollisionResults results) {

        TempVars vars = TempVars.get();
        ArrayList<BIHStackData> stack = vars.bihStack;
        stack.clear();

        float[] minExts = {box.getCenter().x - box.getXExtent(),
            box.getCenter().y - box.getYExtent(),
            box.getCenter().z - box.getZExtent()};

        float[] maxExts = {box.getCenter().x + box.getXExtent(),
            box.getCenter().y + box.getYExtent(),
            box.getCenter().z + box.getZExtent()};

        stack.add(new BIHStackData(this, 0, 0));

        Triangle t = new Triangle();
        int cols = 0;

        stackloop:
        while (stack.size() > 0) {
            BIHNode node = stack.remove(stack.size() - 1).node;

            while (node.axis != 3) {
                int a = node.axis;

                float maxExt = maxExts[a];
                float minExt = minExts[a];

                if (node.leftPlane < node.rightPlane) {
                    // means there's a gap in the middle
                    // if the box is in that gap, we stop there
                    if (minExt > node.leftPlane
                            && maxExt < node.rightPlane) {
                        continue stackloop;
                    }
                }

                if (maxExt < node.rightPlane) {
                    node = node.left;
                } else if (minExt > node.leftPlane) {
                    node = node.right;
                } else {
                    stack.add(new BIHStackData(node.right, 0, 0));
                    node = node.left;
                }
//                if (maxExt < node.leftPlane
//                 && maxExt < node.rightPlane){
//                    node = node.left;
//                }else if (minExt > node.leftPlane
//                       && minExt > node.rightPlane){
//                    node = node.right;
//                }else{

//                }
            }

            for (int i = node.leftIndex; i <= node.rightIndex; i++) {
                tree.getTriangle(i, t.get1(), t.get2(), t.get3());
                if (worldMatrix != null) {
                    worldMatrix.mult(t.get1(), t.get1());
                    worldMatrix.mult(t.get2(), t.get2());
                    worldMatrix.mult(t.get3(), t.get3());
                }

                int added = col.collideWith(t, results);

                if (added > 0) {
                    int index = tree.getTriangleIndex(i);
                    int start = results.size() - added;

                    for (int j = start; j < results.size(); j++) {
                        CollisionResult cr = results.getCollisionDirect(j);
                        cr.setTriangleIndex(index);
                    }

                    cols += added;
                }
            }
        }
        vars.release();
        return cols;
    }