Search in sources :

Example 41 with BoundingBox

use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.

the class BIHNode method intersectWhere.

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) {
                // 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;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Triangle(com.jme3.math.Triangle) TempVars(com.jme3.util.TempVars)

Example 42 with BoundingBox

use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.

the class BIHTree method construct.

public void construct() {
    BoundingBox sceneBbox = createBox(0, numTris - 1);
    root = createNode(0, numTris - 1, sceneBbox, 0);
}
Also used : BoundingBox(com.jme3.bounding.BoundingBox)

Example 43 with BoundingBox

use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.

the class BIHTree method collideWithBoundingVolume.

private int collideWithBoundingVolume(BoundingVolume bv, Matrix4f worldMatrix, CollisionResults results) {
    BoundingBox bbox;
    if (bv instanceof BoundingSphere) {
        BoundingSphere sphere = (BoundingSphere) bv;
        bbox = new BoundingBox(bv.getCenter().clone(), sphere.getRadius(), sphere.getRadius(), sphere.getRadius());
    } else if (bv instanceof BoundingBox) {
        bbox = new BoundingBox((BoundingBox) bv);
    } else {
        throw new UnsupportedCollisionException("BoundingVolume:" + bv);
    }
    bbox.transform(worldMatrix.invert(), bbox);
    return root.intersectWhere(bv, bbox, worldMatrix, this, results);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) BoundingBox(com.jme3.bounding.BoundingBox) UnsupportedCollisionException(com.jme3.collision.UnsupportedCollisionException)

Example 44 with BoundingBox

use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.

the class PssmShadowUtil method computeZFar.

/**
     * Compute the Zfar in the model vieuw to adjust the Zfar distance for the splits calculation
     */
public static float computeZFar(GeometryList occ, GeometryList recv, Camera cam) {
    Matrix4f mat = cam.getViewMatrix();
    BoundingBox bbOcc = ShadowUtil.computeUnionBound(occ, mat);
    BoundingBox bbRecv = ShadowUtil.computeUnionBound(recv, mat);
    return min(max(bbOcc.getZExtent() - bbOcc.getCenter().z, bbRecv.getZExtent() - bbRecv.getCenter().z), cam.getFrustumFar());
}
Also used : Matrix4f(com.jme3.math.Matrix4f) BoundingBox(com.jme3.bounding.BoundingBox)

Example 45 with BoundingBox

use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.

the class ShadowUtil method computeBoundForPoints.

/**
     * Compute bounds from an array of points
     * @param pts
     * @param mat
     * @return
     */
public static BoundingBox computeBoundForPoints(Vector3f[] pts, Matrix4f mat) {
    Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
    Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
    TempVars vars = TempVars.get();
    Vector3f temp = vars.vect1;
    for (int i = 0; i < pts.length; i++) {
        float w = mat.multProj(pts[i], temp);
        temp.x /= w;
        temp.y /= w;
        // Why was this commented out?
        temp.z /= w;
        min.minLocal(temp);
        max.maxLocal(temp);
    }
    vars.release();
    Vector3f center = min.add(max).multLocal(0.5f);
    Vector3f extent = max.subtract(min).multLocal(0.5f);
    //Nehon 08/18/2010 : Added an offset to the extend to avoid banding artifacts when the frustum are aligned
    return new BoundingBox(center, extent.x + 2.0f, extent.y + 2.0f, extent.z + 2.5f);
}
Also used : Vector3f(com.jme3.math.Vector3f) BoundingBox(com.jme3.bounding.BoundingBox) TempVars(com.jme3.util.TempVars)

Aggregations

BoundingBox (com.jme3.bounding.BoundingBox)42 Vector3f (com.jme3.math.Vector3f)35 Geometry (com.jme3.scene.Geometry)14 TempVars (com.jme3.util.TempVars)14 BoundingVolume (com.jme3.bounding.BoundingVolume)10 BoundingSphere (com.jme3.bounding.BoundingSphere)8 Material (com.jme3.material.Material)7 Vector2f (com.jme3.math.Vector2f)4 Texture (com.jme3.texture.Texture)4 Test (org.junit.Test)4 DirectionalLight (com.jme3.light.DirectionalLight)3 Matrix4f (com.jme3.math.Matrix4f)3 Ray (com.jme3.math.Ray)3 Node (com.jme3.scene.Node)3 Spatial (com.jme3.scene.Spatial)3 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)3 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)3 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)3 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)3 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)3