Search in sources :

Example 1 with BoundingBox

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

the class BoundingBox method transform.

/**
     * <code>transform</code> modifies the center of the box to reflect the
     * change made via a rotation, translation and scale.
     * 
     * @param trans 
     *            the transform to apply
     * @param store
     *            box to store result in
     */
public BoundingVolume transform(Transform trans, BoundingVolume store) {
    BoundingBox box;
    if (store == null || store.getType() != Type.AABB) {
        box = new BoundingBox();
    } else {
        box = (BoundingBox) store;
    }
    center.mult(trans.getScale(), box.center);
    trans.getRotation().mult(box.center, box.center);
    box.center.addLocal(trans.getTranslation());
    TempVars vars = TempVars.get();
    Matrix3f transMatrix = vars.tempMat3;
    transMatrix.set(trans.getRotation());
    // Make the rotation matrix all positive to get the maximum x/y/z extent
    transMatrix.absoluteLocal();
    Vector3f scale = trans.getScale();
    vars.vect1.set(xExtent * FastMath.abs(scale.x), yExtent * FastMath.abs(scale.y), zExtent * FastMath.abs(scale.z));
    transMatrix.mult(vars.vect1, vars.vect2);
    // Assign the biggest rotations after scales.
    box.xExtent = FastMath.abs(vars.vect2.getX());
    box.yExtent = FastMath.abs(vars.vect2.getY());
    box.zExtent = FastMath.abs(vars.vect2.getZ());
    vars.release();
    return box;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 2 with BoundingBox

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

the class Intersection method intersect.

//    private boolean axisTest(float a, float b, float fa, float fb, Vector3f v0, Vector3f v1, )
//    private boolean axisTestX01(float a, float b, float fa, float fb,
//                             Vector3f center, Vector3f ext,
//                             Vector3f v1, Vector3f v2, Vector3f v3){
//	float p0 = a * v0.y - b * v0.z;
//	float p2 = a * v2.y - b * v2.z;
//        if(p0 < p2){
//            min = p0;
//            max = p2;
//        } else {
//            min = p2;
//            max = p0;
//        }
//	float rad = fa * boxhalfsize.y + fb * boxhalfsize.z;
//	if(min > rad || max < -rad)
//            return false;
//    }
public static boolean intersect(BoundingBox bbox, Vector3f v1, Vector3f v2, Vector3f v3) {
    //  use separating axis theorem to test overlap between triangle and box
    //  need to test for overlap in these directions:
    //  1) the {x,y,z}-directions (actually, since we use the AABB of the triangle
    //     we do not even need to test these)
    //  2) normal of the triangle
    //  3) crossproduct(edge from tri, {x,y,z}-directin)
    //       this gives 3x3=9 more tests
    TempVars vars = TempVars.get();
    Vector3f tmp0 = vars.vect1, tmp1 = vars.vect2, tmp2 = vars.vect3;
    Vector3f e0 = vars.vect4, e1 = vars.vect5, e2 = vars.vect6;
    Vector3f center = bbox.getCenter();
    Vector3f extent = bbox.getExtent(null);
    //   float min,max,p0,p1,p2,rad,fex,fey,fez;
    //   float normal[3]
    // This is the fastest branch on Sun
    // move everything so that the boxcenter is in (0,0,0)
    v1.subtract(center, tmp0);
    v2.subtract(center, tmp1);
    v3.subtract(center, tmp2);
    // compute triangle edges
    // tri edge 0
    tmp1.subtract(tmp0, e0);
    // tri edge 1
    tmp2.subtract(tmp1, e1);
    // tri edge 2
    tmp0.subtract(tmp2, e2);
    // Bullet 3:
    //  test the 9 tests first (this was faster)
    float min, max;
    float p0, p1, p2, rad;
    float fex = FastMath.abs(e0.x);
    float fey = FastMath.abs(e0.y);
    float fez = FastMath.abs(e0.z);
    //AXISTEST_X01(e0[Z], e0[Y], fez, fey);
    p0 = e0.z * tmp0.y - e0.y * tmp0.z;
    p2 = e0.z * tmp2.y - e0.y * tmp2.z;
    min = min(p0, p2);
    max = max(p0, p2);
    rad = fez * extent.y + fey * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //   AXISTEST_Y02(e0[Z], e0[X], fez, fex);
    p0 = -e0.z * tmp0.x + e0.x * tmp0.z;
    p2 = -e0.z * tmp2.x + e0.x * tmp2.z;
    min = min(p0, p2);
    max = max(p0, p2);
    rad = fez * extent.x + fex * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    // AXISTEST_Z12(e0[Y], e0[X], fey, fex);
    p1 = e0.y * tmp1.x - e0.x * tmp1.y;
    p2 = e0.y * tmp2.x - e0.x * tmp2.y;
    min = min(p1, p2);
    max = max(p1, p2);
    rad = fey * extent.x + fex * extent.y;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    fex = FastMath.abs(e1.x);
    fey = FastMath.abs(e1.y);
    fez = FastMath.abs(e1.z);
    //        AXISTEST_X01(e1[Z], e1[Y], fez, fey);
    p0 = e1.z * tmp0.y - e1.y * tmp0.z;
    p2 = e1.z * tmp2.y - e1.y * tmp2.z;
    min = min(p0, p2);
    max = max(p0, p2);
    rad = fez * extent.y + fey * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //   AXISTEST_Y02(e1[Z], e1[X], fez, fex);
    p0 = -e1.z * tmp0.x + e1.x * tmp0.z;
    p2 = -e1.z * tmp2.x + e1.x * tmp2.z;
    min = min(p0, p2);
    max = max(p0, p2);
    rad = fez * extent.x + fex * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    // AXISTEST_Z0(e1[Y], e1[X], fey, fex);
    p0 = e1.y * tmp0.x - e1.x * tmp0.y;
    p1 = e1.y * tmp1.x - e1.x * tmp1.y;
    min = min(p0, p1);
    max = max(p0, p1);
    rad = fey * extent.x + fex * extent.y;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //
    fex = FastMath.abs(e2.x);
    fey = FastMath.abs(e2.y);
    fez = FastMath.abs(e2.z);
    // AXISTEST_X2(e2[Z], e2[Y], fez, fey);
    p0 = e2.z * tmp0.y - e2.y * tmp0.z;
    p1 = e2.z * tmp1.y - e2.y * tmp1.z;
    min = min(p0, p1);
    max = max(p0, p1);
    rad = fez * extent.y + fey * extent.z;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    // AXISTEST_Y1(e2[Z], e2[X], fez, fex);
    p0 = -e2.z * tmp0.x + e2.x * tmp0.z;
    p1 = -e2.z * tmp1.x + e2.x * tmp1.z;
    min = min(p0, p1);
    max = max(p0, p1);
    rad = fez * extent.x + fex * extent.y;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //   AXISTEST_Z12(e2[Y], e2[X], fey, fex);
    p1 = e2.y * tmp1.x - e2.x * tmp1.y;
    p2 = e2.y * tmp2.x - e2.x * tmp2.y;
    min = min(p1, p2);
    max = max(p1, p2);
    rad = fey * extent.x + fex * extent.y;
    if (min > rad || max < -rad) {
        vars.release();
        return false;
    }
    //  Bullet 1:
    //  first test overlap in the {x,y,z}-directions
    //  find min, max of the triangle each direction, and test for overlap in
    //  that direction -- this is equivalent to testing a minimal AABB around
    //  the triangle against the AABB
    Vector3f minMax = vars.vect7;
    // test in X-direction
    findMinMax(tmp0.x, tmp1.x, tmp2.x, minMax);
    if (minMax.x > extent.x || minMax.y < -extent.x) {
        vars.release();
        return false;
    }
    // test in Y-direction
    findMinMax(tmp0.y, tmp1.y, tmp2.y, minMax);
    if (minMax.x > extent.y || minMax.y < -extent.y) {
        vars.release();
        return false;
    }
    // test in Z-direction
    findMinMax(tmp0.z, tmp1.z, tmp2.z, minMax);
    if (minMax.x > extent.z || minMax.y < -extent.z) {
        vars.release();
        return false;
    }
    //       // Bullet 2:
    //       //  test if the box intersects the plane of the triangle
    //       //  compute plane equation of triangle: normal * x + d = 0
    //        Vector3f normal = new Vector3f();
    //        e0.cross(e1, normal);
    Plane p = vars.plane;
    p.setPlanePoints(v1, v2, v3);
    if (bbox.whichSide(p) == Plane.Side.Negative) {
        vars.release();
        return false;
    }
    //
    //        if(!planeBoxOverlap(normal,v0,boxhalfsize)) return false;
    vars.release();
    return true;
/* box and triangle overlaps */
}
Also used : Plane(com.jme3.math.Plane) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 3 with BoundingBox

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

the class BoundingCollisionTest method testBoxRayCollision.

@Test
public void testBoxRayCollision() {
    BoundingBox box = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
    Ray ray = new Ray(Vector3f.ZERO, Vector3f.UNIT_Z);
    // XXX: seems incorrect, ray inside box should only generate
    // one result...
    checkCollision(box, ray, 2);
    ray.setOrigin(new Vector3f(0, 0, -5));
    checkCollision(box, ray, 2);
    // XXX: is this right? the ray origin is on the box's side..
    ray.setOrigin(new Vector3f(0, 0, 2f));
    checkCollision(box, ray, 0);
    ray.setOrigin(new Vector3f(0, 0, -2f));
    checkCollision(box, ray, 2);
    // parallel to the edge, touching the side
    ray.setOrigin(new Vector3f(0, 1f, -2f));
    checkCollision(box, ray, 2);
    // still parallel, but not touching the side
    ray.setOrigin(new Vector3f(0, 1f + FastMath.ZERO_TOLERANCE, -2f));
    checkCollision(box, ray, 0);
}
Also used : BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray) Test(org.junit.Test)

Example 4 with BoundingBox

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

the class BoundingCollisionTest method testBoxSphereCollision.

@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);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) Test(org.junit.Test)

Example 5 with BoundingBox

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

the class GeneratedTexture method triangulate.

/**
     * 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
     */
public TriangulatedTexture triangulate(Mesh mesh, Long geometriesOMA, UVCoordinatesType coordinatesType, BlenderContext blenderContext) {
    TemporalMesh geometries = (TemporalMesh) blenderContext.getLoadedFeature(geometriesOMA, LoadedDataType.TEMPORAL_MESH);
    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);
}
Also used : TriangleTextureElement(com.jme3.scene.plugins.blender.textures.TriangulatedTexture.TriangleTextureElement) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) TreeSet(java.util.TreeSet) Vector3f(com.jme3.math.Vector3f) BoundingBox(com.jme3.bounding.BoundingBox)

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