Search in sources :

Example 16 with BoundingVolume

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

the class BoundingBox method intersects.

/**
     * determines if this bounding box intersects with a given oriented bounding
     * box.
     * 
     * @see com.jme.bounding.BoundingVolume#intersectsOrientedBoundingBox(com.jme.bounding.OrientedBoundingBox)
     */
//    public boolean intersectsOrientedBoundingBox(OrientedBoundingBox obb) {
//        return obb.intersectsBoundingBox(this);
//    }
/**
     * determines if this bounding box intersects with a given ray object. If an
     * intersection has occurred, true is returned, otherwise false is returned.
     * 
     * @see BoundingVolume#intersects(com.jme3.math.Ray) 
     */
public boolean intersects(Ray ray) {
    assert Vector3f.isValidVector(center);
    float rhs;
    TempVars vars = TempVars.get();
    Vector3f diff = ray.origin.subtract(getCenter(vars.vect2), vars.vect1);
    final float[] fWdU = vars.fWdU;
    final float[] fAWdU = vars.fAWdU;
    final float[] fDdU = vars.fDdU;
    final float[] fADdU = vars.fADdU;
    final float[] fAWxDdU = vars.fAWxDdU;
    fWdU[0] = ray.getDirection().dot(Vector3f.UNIT_X);
    fAWdU[0] = FastMath.abs(fWdU[0]);
    fDdU[0] = diff.dot(Vector3f.UNIT_X);
    fADdU[0] = FastMath.abs(fDdU[0]);
    if (fADdU[0] > xExtent && fDdU[0] * fWdU[0] >= 0.0) {
        vars.release();
        return false;
    }
    fWdU[1] = ray.getDirection().dot(Vector3f.UNIT_Y);
    fAWdU[1] = FastMath.abs(fWdU[1]);
    fDdU[1] = diff.dot(Vector3f.UNIT_Y);
    fADdU[1] = FastMath.abs(fDdU[1]);
    if (fADdU[1] > yExtent && fDdU[1] * fWdU[1] >= 0.0) {
        vars.release();
        return false;
    }
    fWdU[2] = ray.getDirection().dot(Vector3f.UNIT_Z);
    fAWdU[2] = FastMath.abs(fWdU[2]);
    fDdU[2] = diff.dot(Vector3f.UNIT_Z);
    fADdU[2] = FastMath.abs(fDdU[2]);
    if (fADdU[2] > zExtent && fDdU[2] * fWdU[2] >= 0.0) {
        vars.release();
        return false;
    }
    Vector3f wCrossD = ray.getDirection().cross(diff, vars.vect2);
    fAWxDdU[0] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_X));
    rhs = yExtent * fAWdU[2] + zExtent * fAWdU[1];
    if (fAWxDdU[0] > rhs) {
        vars.release();
        return false;
    }
    fAWxDdU[1] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Y));
    rhs = xExtent * fAWdU[2] + zExtent * fAWdU[0];
    if (fAWxDdU[1] > rhs) {
        vars.release();
        return false;
    }
    fAWxDdU[2] = FastMath.abs(wCrossD.dot(Vector3f.UNIT_Z));
    rhs = xExtent * fAWdU[1] + yExtent * fAWdU[0];
    if (fAWxDdU[2] > rhs) {
        vars.release();
        return false;
    }
    vars.release();
    return true;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 17 with BoundingVolume

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

the class BoundingBox method transform.

public BoundingVolume transform(Matrix4f trans, BoundingVolume store) {
    BoundingBox box;
    if (store == null || store.getType() != Type.AABB) {
        box = new BoundingBox();
    } else {
        box = (BoundingBox) store;
    }
    TempVars vars = TempVars.get();
    float w = trans.multProj(center, box.center);
    box.center.divideLocal(w);
    Matrix3f transMatrix = vars.tempMat3;
    trans.toRotationMatrix(transMatrix);
    // Make the rotation matrix all positive to get the maximum x/y/z extent
    transMatrix.absoluteLocal();
    vars.vect1.set(xExtent, yExtent, zExtent);
    transMatrix.mult(vars.vect1, vars.vect1);
    // Assign the biggest rotations after scales.
    box.xExtent = FastMath.abs(vars.vect1.getX());
    box.yExtent = FastMath.abs(vars.vect1.getY());
    box.zExtent = FastMath.abs(vars.vect1.getZ());
    vars.release();
    return box;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 18 with BoundingVolume

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

the class BoundingSphere method merge.

//    /**
//     * Merges this sphere with the given OBB.
//     *
//     * @param volume
//     *            The OBB to merge.
//     * @return This sphere, after merging.
//     */
//    private BoundingSphere mergeOBB(OrientedBoundingBox volume) {
//        // compute edge points from the obb
//        if (!volume.correctCorners)
//            volume.computeCorners();
//        _mergeBuf.rewind();
//        for (int i = 0; i < 8; i++) {
//            _mergeBuf.put(volume.vectorStore[i].x);
//            _mergeBuf.put(volume.vectorStore[i].y);
//            _mergeBuf.put(volume.vectorStore[i].z);
//        }
//
//        // remember old radius and center
//        float oldRadius = radius;
//        Vector3f oldCenter = _compVect2.set( center );
//
//        // compute new radius and center from obb points
//        computeFromPoints(_mergeBuf);
//        Vector3f newCenter = _compVect3.set( center );
//        float newRadius = radius;
//
//        // restore old center and radius
//        center.set( oldCenter );
//        radius = oldRadius;
//
//        //merge obb points result
//        merge( newRadius, newCenter, this );
//
//        return this;
//    }
private BoundingVolume merge(float temp_radius, Vector3f temp_center, BoundingSphere rVal) {
    TempVars vars = TempVars.get();
    Vector3f diff = temp_center.subtract(center, vars.vect1);
    float lengthSquared = diff.lengthSquared();
    float radiusDiff = temp_radius - radius;
    float fRDiffSqr = radiusDiff * radiusDiff;
    if (fRDiffSqr >= lengthSquared) {
        if (radiusDiff <= 0.0f) {
            vars.release();
            return this;
        }
        Vector3f rCenter = rVal.center;
        if (rCenter == null) {
            rVal.setCenter(rCenter = new Vector3f());
        }
        rCenter.set(temp_center);
        rVal.setRadius(temp_radius);
        vars.release();
        return rVal;
    }
    float length = (float) Math.sqrt(lengthSquared);
    Vector3f rCenter = rVal.center;
    if (rCenter == null) {
        rVal.setCenter(rCenter = new Vector3f());
    }
    if (length > RADIUS_EPSILON) {
        float coeff = (length + radiusDiff) / (2.0f * length);
        rCenter.set(center.addLocal(diff.multLocal(coeff)));
    } else {
        rCenter.set(center);
    }
    rVal.setRadius(0.5f * (length + radius + temp_radius));
    vars.release();
    return rVal;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 19 with BoundingVolume

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

the class Spatial method read.

public void read(JmeImporter im) throws IOException {
    InputCapsule ic = im.getCapsule(this);
    name = ic.readString("name", null);
    worldBound = (BoundingVolume) ic.readSavable("world_bound", null);
    cullHint = ic.readEnum("cull_mode", CullHint.class, CullHint.Inherit);
    batchHint = ic.readEnum("batch_hint", BatchHint.class, BatchHint.Inherit);
    queueBucket = ic.readEnum("queue", RenderQueue.Bucket.class, RenderQueue.Bucket.Inherit);
    shadowMode = ic.readEnum("shadow_mode", ShadowMode.class, ShadowMode.Inherit);
    localTransform = (Transform) ic.readSavable("transform", Transform.IDENTITY);
    localLights = (LightList) ic.readSavable("lights", null);
    localLights.setOwner(this);
    ArrayList<MatParamOverride> localOverridesList = ic.readSavableArrayList("overrides", null);
    if (localOverridesList == null) {
        localOverrides = new SafeArrayList<>(MatParamOverride.class);
    } else {
        localOverrides = new SafeArrayList(MatParamOverride.class, localOverridesList);
    }
    worldOverrides = new SafeArrayList<>(MatParamOverride.class);
    //changed for backward compatibility with j3o files generated before the AnimControl/SkeletonControl split
    //the AnimControl creates the SkeletonControl for old files and add it to the spatial.
    //The SkeletonControl must be the last in the stack so we add the list of all other control before it.
    //When backward compatibility won't be needed anymore this can be replaced by :
    //controls = ic.readSavableArrayList("controlsList", null));
    controls.addAll(0, ic.readSavableArrayList("controlsList", null));
    userData = (HashMap<String, Savable>) ic.readStringSavableMap("user_data", null);
}
Also used : Bucket(com.jme3.renderer.queue.RenderQueue.Bucket) SafeArrayList(com.jme3.util.SafeArrayList) ShadowMode(com.jme3.renderer.queue.RenderQueue.ShadowMode) MatParamOverride(com.jme3.material.MatParamOverride)

Example 20 with BoundingVolume

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

the class Node method updateWorldBound.

@Override
protected void updateWorldBound() {
    super.updateWorldBound();
    // for a node, the world bound is a combination of all it's children
    // bounds
    BoundingVolume resultBound = null;
    for (Spatial child : children.getArray()) {
        // child bound is assumed to be updated
        assert (child.refreshFlags & RF_BOUND) == 0;
        if (resultBound != null) {
            // merge current world bound with child world bound
            resultBound.mergeLocal(child.getWorldBound());
        } else {
            // set world bound to first non-null child world bound
            if (child.getWorldBound() != null) {
                resultBound = child.getWorldBound().clone(this.worldBound);
            }
        }
    }
    this.worldBound = resultBound;
}
Also used : BoundingVolume(com.jme3.bounding.BoundingVolume)

Aggregations

BoundingVolume (com.jme3.bounding.BoundingVolume)18 BoundingBox (com.jme3.bounding.BoundingBox)10 TempVars (com.jme3.util.TempVars)10 BoundingSphere (com.jme3.bounding.BoundingSphere)7 Geometry (com.jme3.scene.Geometry)4 UnsupportedCollisionException (com.jme3.collision.UnsupportedCollisionException)3 Spatial (com.jme3.scene.Spatial)3 CollisionResult (com.jme3.collision.CollisionResult)2 CollisionResults (com.jme3.collision.CollisionResults)2 Vector3f (com.jme3.math.Vector3f)2 InputCapsule (com.jme3.export.InputCapsule)1 MatParamOverride (com.jme3.material.MatParamOverride)1 Matrix4f (com.jme3.math.Matrix4f)1 Camera (com.jme3.renderer.Camera)1 Bucket (com.jme3.renderer.queue.RenderQueue.Bucket)1 ShadowMode (com.jme3.renderer.queue.RenderQueue.ShadowMode)1 Node (com.jme3.scene.Node)1 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)1 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)1 Pointer (com.jme3.scene.plugins.blender.file.Pointer)1