com.jme3.bounding.BoundingBox Java Examples

The following examples show how to use com.jme3.bounding.BoundingBox. 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: ShadowUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
    Vector3f temp = new Vector3f();

    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);
    }

    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);
}
 
Example #2
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 #3
Source File: TestObbVsBounds.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void makeBoxWire(BoundingBox box) {
    Vector3f[] points = new Vector3f[8];
    for (int i = 0; i < 8; i++) {
        points[i] = new Vector3f();
    }
    points[0].set(-1, -1, 1);
    points[1].set(-1, 1, 1);
    points[2].set(1, 1, 1);
    points[3].set(1, -1, 1);

    points[4].set(-1, -1, -1);
    points[5].set(-1, 1, -1);
    points[6].set(1, 1, -1);
    points[7].set(1, -1, -1);

    WireFrustum frustumShape = new WireFrustum(points);
    aabbGeom = new Geometry("box", frustumShape);
    aabbGeom.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"));
    aabbGeom.getMaterial().getAdditionalRenderState().setWireframe(true);
    aabbGeom.setLocalTranslation(box.getCenter());
    aabbGeom.setLocalScale(box.getXExtent(), box.getYExtent(), box.getZExtent());
    rootNode.attachChild(aabbGeom);
}
 
Example #4
Source File: TestRayCollision.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args){
    Ray r = new Ray(Vector3f.ZERO, Vector3f.UNIT_X);
    BoundingBox bbox = new BoundingBox(new Vector3f(5, 0, 0), 1, 1, 1);

    CollisionResults res = new CollisionResults();
    bbox.collideWith(r, res);

    System.out.println("Bounding:" +bbox);
    System.out.println("Ray: "+r);

    System.out.println("Num collisions: "+res.size());
    for (int i = 0; i < res.size(); i++){
        System.out.println("--- Collision #"+i+" ---");
        float dist = res.getCollision(i).getDistance();
        Vector3f pt = res.getCollision(i).getContactPoint();
        System.out.println("distance: "+dist);
        System.out.println("point: "+pt);
    }
}
 
Example #5
Source File: BoundingCollisionTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testBoxBoxCollision() {
    BoundingBox box1 = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
    BoundingBox box2 = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
    checkCollision(box1, box2, 1);
    
    // Put it at the very edge - should still intersect.
    box2.setCenter(new Vector3f(2f, 0f, 0f));
    checkCollision(box1, box2, 1);
    
    // Put it a wee bit farther - no intersection expected
    box2.setCenter(new Vector3f(2f + FastMath.ZERO_TOLERANCE, 0, 0));
    checkCollision(box1, box2, 0);
    
    // Check the corners.
    box2.setCenter(new Vector3f(2f, 2f, 2f));
    checkCollision(box1, box2, 1);
    
    box2.setCenter(new Vector3f(2f, 2f, 2f + FastMath.ZERO_TOLERANCE));
    checkCollision(box1, box2, 0);
}
 
Example #6
Source File: TerrainQuad.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Find what terrain patches need normal recalculations and update
 * their normals;
 */
protected void fixNormals(BoundingBox affectedArea) {
    if (children == null)
        return;

    // go through the children and see if they collide with the affectedAreaBBox
    // if they do, then update their normals
    for (int x = children.size(); --x >= 0;) {
        Spatial child = children.get(x);
        if (child instanceof TerrainQuad) {
            if (affectedArea != null && affectedArea.intersects( child.getWorldBound()) )
                ((TerrainQuad) child).fixNormals(affectedArea);
        } else if (child instanceof TerrainPatch) {
            if (affectedArea != null && affectedArea.intersects(child.getWorldBound()) )
                ((TerrainPatch) child).updateNormals(); // recalculate the patch's normals
        }
    }
}
 
Example #7
Source File: BoundingCollisionTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testBoxSphereCollision() {
    BoundingBox box1 = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
    BoundingSphere sphere2 = new BoundingSphere(1, Vector3f.ZERO);
    checkCollision(box1, sphere2, 1);
    
    // Put it at the very edge - for sphere vs. box, it will not intersect
    sphere2.setCenter(new Vector3f(2f, 0f, 0f));
    checkCollision(box1, sphere2, 0);
    
    // Put it a wee bit closer - should intersect.
    sphere2.setCenter(new Vector3f(2f - FastMath.ZERO_TOLERANCE, 0, 0));
    checkCollision(box1, sphere2, 1);
    
    // Test if the algorithm converts the sphere 
    // to a box before testing the collision (incorrect)
    float sqrt3 = FastMath.sqrt(3);
    
    sphere2.setCenter(Vector3f.UNIT_XYZ.mult(2));
    sphere2.setRadius(sqrt3);
    checkCollision(box1, sphere2, 0);
    
    // Make it a wee bit larger.
    sphere2.setRadius(sqrt3 + FastMath.ZERO_TOLERANCE);
    checkCollision(box1, sphere2, 1);
}
 
Example #8
Source File: GeneratedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * This method triangulates the texture. In the result we get a set of small
 * flat textures for each face of the given mesh. This can be later merged
 * into one flat texture.
 * 
 * @param mesh
 *            the mesh we create the texture for
 * @param geometriesOMA
 *            the old memory address of the geometries group that the given
 *            mesh belongs to (required for bounding box calculations)
 * @param coordinatesType
 *            the types of UV coordinates
 * @param blenderContext
 *            the blender context
 * @return triangulated texture
 */
@SuppressWarnings("unchecked")
public TriangulatedTexture triangulate(Mesh mesh, Long geometriesOMA, UVCoordinatesType coordinatesType, BlenderContext blenderContext) {
    List<Geometry> geometries = (List<Geometry>) blenderContext.getLoadedFeature(geometriesOMA, LoadedFeatureDataType.LOADED_FEATURE);

    int[] coordinatesSwappingIndexes = new int[] { ((Number) mTex.getFieldValue("projx")).intValue(), ((Number) mTex.getFieldValue("projy")).intValue(), ((Number) mTex.getFieldValue("projz")).intValue() };
    List<Vector3f> uvs = UVCoordinatesGenerator.generateUVCoordinatesFor3DTexture(mesh, coordinatesType, coordinatesSwappingIndexes, geometries);
    Vector3f[] uvsArray = uvs.toArray(new Vector3f[uvs.size()]);
    BoundingBox boundingBox = UVCoordinatesGenerator.getBoundingBox(geometries);
    Set<TriangleTextureElement> triangleTextureElements = new TreeSet<TriangleTextureElement>(new Comparator<TriangleTextureElement>() {
        public int compare(TriangleTextureElement o1, TriangleTextureElement o2) {
            return o1.faceIndex - o2.faceIndex;
        }
    });
    int[] indices = new int[3];
    for (int i = 0; i < mesh.getTriangleCount(); ++i) {
        mesh.getTriangle(i, indices);
        triangleTextureElements.add(new TriangleTextureElement(i, boundingBox, this, uvsArray, indices, blenderContext));
    }
    return new TriangulatedTexture(triangleTextureElements, blenderContext);
}
 
Example #9
Source File: TerrainQuad.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void read(JmeImporter e) throws IOException {
    super.read(e);
    InputCapsule c = e.getCapsule(this);
    size = c.readInt("size", 0);
    stepScale = (Vector3f) c.readSavable("stepScale", null);
    offset = (Vector2f) c.readSavable("offset", new Vector2f(0,0));
    offsetAmount = c.readFloat("offsetAmount", 0);
    quadrant = c.readInt("quadrant", 0);
    totalSize = c.readInt("totalSize", 0);
    //lodCalculator = (LodCalculator) c.readSavable("lodCalculator", createDefaultLodCalculator());
    //lodCalculatorFactory = (LodCalculatorFactory) c.readSavable("lodCalculatorFactory", null);
    
    if ( !(getParent() instanceof TerrainQuad) ) {
        BoundingBox all = new BoundingBox(getWorldTranslation(), totalSize, totalSize, totalSize);
        affectedAreaBBox = all;
        updateNormals();
    }
}
 
Example #10
Source File: PickEventSession.java    From Lemur with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 *  Returns the min and max world z values for the specified
 *  spatial. 
 */
protected float[] getZBounds( Spatial s ) {
    BoundingVolume bv = s.getWorldBound();
    if( bv == null ) {
        // JME returns null for empty nodes
        return new float[] {0, 1}; 
    }
    Vector3f center = bv.getCenter();
    if( bv instanceof BoundingBox ) {
        BoundingBox bb = (BoundingBox)bv;
        return new float[] { center.z - bb.getZExtent(), center.z + bb.getZExtent() };
    } else if( bv instanceof BoundingSphere ) {
        BoundingSphere bs = (BoundingSphere)bv;
        return new float[] { center.z - bs.getRadius(), center.z + bs.getRadius() };
    } else {
        throw new UnsupportedOperationException("Bounding volume type not supported for:" + bv);
    }        
}
 
Example #11
Source File: BIHTree.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private BoundingBox createBox(int l, int r) {
    TempVars vars = TempVars.get();

    Vector3f min = vars.vect1.set(new Vector3f(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
    Vector3f max = vars.vect2.set(new Vector3f(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));

    Vector3f v1 = vars.vect3,
            v2 = vars.vect4,
            v3 = vars.vect5;

    for (int i = l; i <= r; i++) {
        getTriangle(i, v1, v2, v3);
        BoundingBox.checkMinMax(min, max, v1);
        BoundingBox.checkMinMax(min, max, v2);
        BoundingBox.checkMinMax(min, max, v3);
    }

    BoundingBox bbox = new BoundingBox(min, max);
    vars.release();
    return bbox;
}
 
Example #12
Source File: ShadowUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public OccludersExtractor(Matrix4f vpm, int cc, BoundingBox sBB, BoundingBox cBB, GeometryList sOCC, TempVars v) {
    viewProjMatrix = vpm; 
    casterCount = cc;
    splitBB = sBB;
    casterBB = cBB;
    splitOccluders = sOCC;
    vars = v;
}
 
Example #13
Source File: ParticleEmitter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Instantly emits all the particles possible to be emitted. Any particles
 * which are currently inactive will be spawned immediately.
 */
public void emitAllParticles() {
    // Force world transform to update
    this.getWorldTransform();

    TempVars vars = TempVars.get();

    BoundingBox bbox = (BoundingBox) this.getMesh().getBound();

    Vector3f min = vars.vect1;
    Vector3f max = vars.vect2;

    bbox.getMin(min);
    bbox.getMax(max);

    if (!Vector3f.isValidVector(min)) {
        min.set(Vector3f.POSITIVE_INFINITY);
    }
    if (!Vector3f.isValidVector(max)) {
        max.set(Vector3f.NEGATIVE_INFINITY);
    }

    while (emitParticle(min, max) != null);

    bbox.setMinMax(min, max);
    this.setBoundRefresh();

    vars.release();
}
 
Example #14
Source File: ShadowUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Computes the bounds of multiple bounding volumes
 * @param bv
 * @return 
 */
public static BoundingBox computeUnionBound(List<BoundingVolume> bv) {
    BoundingBox bbox = new BoundingBox();
    for (int i = 0; i < bv.size(); i++) {
        BoundingVolume vol = bv.get(i);
        bbox.mergeLocal(vol);
    }
    return bbox;
}
 
Example #15
Source File: TerrainPatch.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This most definitely is not optimized.
 */
private int collideWithBoundingBox(BoundingBox bbox, CollisionResults results) {
    
    // test the four corners, for cases where the bbox dimensions are less than the terrain grid size, which is probably most of the time
    Vector3f topLeft = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x-bbox.getXExtent(), 0, bbox.getCenter().z-bbox.getZExtent()));
    Vector3f topRight = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x+bbox.getXExtent(), 0, bbox.getCenter().z-bbox.getZExtent()));
    Vector3f bottomLeft = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x-bbox.getXExtent(), 0, bbox.getCenter().z+bbox.getZExtent()));
    Vector3f bottomRight = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x+bbox.getXExtent(), 0, bbox.getCenter().z+bbox.getZExtent()));

    Triangle t = getTriangle(topLeft.x, topLeft.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(topRight.x, topRight.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(bottomLeft.x, bottomLeft.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    t = getTriangle(bottomRight.x, bottomRight.z);
    if (t != null && bbox.collideWith(t, results) > 0)
        return 1;
    
    // box is larger than the points on the terrain, so test against the points
    for (float z=topLeft.z; z<bottomLeft.z; z+=1) {
        for (float x=topLeft.x; x<topRight.x; x+=1) {
            
            if (x < 0 || z < 0 || x >= size || z >= size)
                continue;
            t = getTriangle(x,z);
            if (t != null && bbox.collideWith(t, results) > 0)
                return 1;
        }
    }

    return 0;
}
 
Example #16
Source File: ShapeBoundsTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testBounds(Geometry geometry) {
    BoundingVolume bv = geometry.getWorldBound();

    if (bv instanceof BoundingBox) {
        BoundingBox bb = (BoundingBox) bv;
        Assert.assertTrue(bb.getXExtent() > 0 && bb.getYExtent() > 0 && bb.getZExtent() > 0);
    } else if (bv instanceof BoundingSphere) {
        BoundingSphere bs = (BoundingSphere) bv;
        Assert.assertTrue(bs.getRadius() > 1f);
    }

    testVertices(geometry);
}
 
Example #17
Source File: UVProjectionGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Flat projection for 2D textures.
 * 
 * @param mesh
 *            mesh that is to be projected
 * @param bb
 *            the bounding box for projecting
 * @return UV coordinates after the projection
 */
public static float[] flatProjection(float[] positions, BoundingBox bb) {
    Vector3f min = bb.getMin(null);
    float[] ext = new float[] { bb.getXExtent() * 2.0f, bb.getZExtent() * 2.0f };
    float[] uvCoordinates = new float[positions.length / 3 * 2];
    for (int i = 0, j = 0; i < positions.length; i += 3, j += 2) {
        uvCoordinates[j] = (positions[i] - min.x) / ext[0];
        // skip the Y-coordinate
        uvCoordinates[j + 1] = (positions[i + 2] - min.z) / ext[1];
    }
    return uvCoordinates;
}
 
Example #18
Source File: TestHoveringTank.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void makeMissile() {
    Vector3f pos = spaceCraft.getWorldTranslation().clone();
    Quaternion rot = spaceCraft.getWorldRotation();
    Vector3f dir = rot.getRotationColumn(2);

    Spatial missile = assetManager.loadModel("Models/SpaceCraft/Rocket.mesh.xml");
    missile.scale(0.5f);
    missile.rotate(0, FastMath.PI, 0);
    missile.updateGeometricState();

    BoundingBox box = (BoundingBox) missile.getWorldBound();
    final Vector3f extent = box.getExtent(null);

    BoxCollisionShape boxShape = new BoxCollisionShape(extent);

    missile.setName("Missile");
    missile.rotate(rot);
    missile.setLocalTranslation(pos.addLocal(0, extent.y * 4.5f, 0));
    missile.setLocalRotation(hoverControl.getPhysicsRotation());
    missile.setShadowMode(ShadowMode.Cast);
    RigidBodyControl control = new BombControl(assetManager, boxShape, 20);
    control.setLinearVelocity(dir.mult(100));
    control.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_03);
    missile.addControl(control);


    rootNode.attachChild(missile);
    getPhysicsSpace().add(missile);
}
 
Example #19
Source File: ShadowUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Compute bounds of a geomList
 * @param list
 * @param transform
 * @return 
 */
public static BoundingBox computeUnionBound(GeometryList list, Transform transform) {
    BoundingBox bbox = new BoundingBox();
    for (int i = 0; i < list.size(); i++) {
        BoundingVolume vol = list.get(i).getWorldBound();
        BoundingVolume newVol = vol.transform(transform);
        //Nehon : prevent NaN and infinity values to screw the final bounding box
        if (newVol.getCenter().x != Float.NaN && newVol.getCenter().x != Float.POSITIVE_INFINITY && newVol.getCenter().x != Float.NEGATIVE_INFINITY) {
            bbox.mergeLocal(newVol);
        }
    }
    return bbox;
}
 
Example #20
Source File: ShadowUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Compute bounds of a geomList
 * @param list
 * @param mat
 * @return 
 */
public static BoundingBox computeUnionBound(GeometryList list, Matrix4f mat) {
    BoundingBox bbox = new BoundingBox();
    BoundingVolume store = null;
    for (int i = 0; i < list.size(); i++) {
        BoundingVolume vol = list.get(i).getWorldBound();
        store = vol.clone().transform(mat, null);
        //Nehon : prevent NaN and infinity values to screw the final bounding box
        if (store.getCenter().x != Float.NaN && store.getCenter().x != Float.POSITIVE_INFINITY && store.getCenter().x != Float.NEGATIVE_INFINITY) {
            bbox.mergeLocal(store);
        }
    }
    return bbox;
}
 
Example #21
Source File: Camera.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Camera clone() {
    try {
        Camera cam = (Camera) super.clone();
        cam.viewportChanged = true;
        cam.planeState = 0;

        cam.worldPlane = new Plane[MAX_WORLD_PLANES];
        for (int i = 0; i < worldPlane.length; i++) {
            cam.worldPlane[i] = worldPlane[i].clone();
        }

        cam.coeffLeft = new float[2];
        cam.coeffRight = new float[2];
        cam.coeffBottom = new float[2];
        cam.coeffTop = new float[2];

        cam.location = location.clone();
        cam.rotation = rotation.clone();

        if (projectionMatrixOverride != null) {
            cam.projectionMatrixOverride = projectionMatrixOverride.clone();
        }

        cam.viewMatrix = viewMatrix.clone();
        cam.projectionMatrix = projectionMatrix.clone();
        cam.viewProjectionMatrix = viewProjectionMatrix.clone();
        cam.guiBounding = (BoundingBox) guiBounding.clone();

        cam.update();

        return cam;
    } catch (CloneNotSupportedException ex) {
        throw new AssertionError();
    }
}
 
Example #22
Source File: FastOctnode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static void findChildBound(BoundingBox bbox, int side){
    float extent = bbox.getXExtent() * 0.5f;
    bbox.getCenter().set(bbox.getCenter().x + extent * Octnode.extentMult[side].x,
                         bbox.getCenter().y + extent * Octnode.extentMult[side].y,
                         bbox.getCenter().z + extent * Octnode.extentMult[side].z);
    bbox.setXExtent(extent);
    bbox.setYExtent(extent);
    bbox.setZExtent(extent);
}
 
Example #23
Source File: ShadowUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Compute bounds of a geomList
 * @param list
 * @param mat
 * @return a new instance
 */
public static BoundingBox computeUnionBound(GeometryList list, Matrix4f mat) {
    BoundingBox bbox = new BoundingBox();
    TempVars tempv = TempVars.get();
    for (int i = 0; i < list.size(); i++) {
        BoundingVolume vol = list.get(i).getWorldBound();
        BoundingVolume store = vol.transform(mat, tempv.bbox);
        //Nehon : prevent NaN and infinity values to screw the final bounding box
        if (!Float.isNaN(store.getCenter().x) && !Float.isInfinite(store.getCenter().x)) {
            bbox.mergeLocal(store);
        }
    }
    tempv.release();
    return bbox;
}
 
Example #24
Source File: AreaUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static float calcScreenArea(BoundingBox bound, float distance, float screenWidth) {
    // Calc as if we are a BoundingSphere for now...
    float radiusSquare = bound.getXExtent() * bound.getXExtent()
                       + bound.getYExtent() * bound.getYExtent()
                       + bound.getZExtent() * bound.getZExtent();
    return ((radiusSquare * screenWidth * screenWidth) / (distance * distance * 4)) * FastMath.PI;
}
 
Example #25
Source File: ShadowUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Compute bounds of a geomList
 * @param list
 * @param transform
 * @return a new instance
 */
public static BoundingBox computeUnionBound(GeometryList list, Transform transform) {
    BoundingBox bbox = new BoundingBox();
    TempVars tempv = TempVars.get();
    for (int i = 0; i < list.size(); i++) {
        BoundingVolume vol = list.get(i).getWorldBound();
        BoundingVolume newVol = vol.transform(transform, tempv.bbox);
        //Nehon : prevent NaN and infinity values to screw the final bounding box
        if (!Float.isNaN(newVol.getCenter().x) && !Float.isInfinite(newVol.getCenter().x)) {
            bbox.mergeLocal(newVol);
        }
    }
    tempv.release();
    return bbox;
}
 
Example #26
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 #27
Source File: ShadowUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Compute bounds from an array of points
 * @param pts
 * @param transform
 * @return 
 */
public static BoundingBox computeBoundForPoints(Vector3f[] pts, Transform transform) {
    Vector3f min = new Vector3f(Vector3f.POSITIVE_INFINITY);
    Vector3f max = new Vector3f(Vector3f.NEGATIVE_INFINITY);
    Vector3f temp = new Vector3f();
    for (int i = 0; i < pts.length; i++) {
        transform.transformVector(pts[i], temp);

        min.minLocal(temp);
        max.maxLocal(temp);
    }
    Vector3f center = min.add(max).multLocal(0.5f);
    Vector3f extent = max.subtract(min).multLocal(0.5f);
    return new BoundingBox(center, extent.x, extent.y, extent.z);
}
 
Example #28
Source File: AreaUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static float calcScreenArea(BoundingBox bound, float distance, float screenWidth) {
    // Calc as if we are a BoundingSphere for now...
    float radiusSquare = bound.getXExtent() * bound.getXExtent()
                       + bound.getYExtent() * bound.getYExtent()
                       + bound.getZExtent() * bound.getZExtent();
    return ((radiusSquare * screenWidth * screenWidth) / (distance * distance * 4)) * FastMath.PI;
}
 
Example #29
Source File: ParticleEmitter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Instantly emits available particles, up to num.
 */
public void emitParticles(int num) {
    // Force world transform to update
    this.getWorldTransform();

    TempVars vars = TempVars.get();

    BoundingBox bbox = (BoundingBox) this.getMesh().getBound();

    Vector3f min = vars.vect1;
    Vector3f max = vars.vect2;

    bbox.getMin(min);
    bbox.getMax(max);

    if (!Vector3f.isValidVector(min)) {
        min.set(Vector3f.POSITIVE_INFINITY);
    }
    if (!Vector3f.isValidVector(max)) {
        max.set(Vector3f.NEGATIVE_INFINITY);
    }

    for(int i=0;i<num;i++) {
        if( emitParticle(min, max) == null ) break;
    }

    bbox.setMinMax(min, max);
    this.setBoundRefresh();

    vars.release();
}
 
Example #30
Source File: PssmShadowUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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());
}