Java Code Examples for com.jme3.scene.Spatial#getWorldBound()

The following examples show how to use com.jme3.scene.Spatial#getWorldBound() . 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: 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 2
Source File: SceneToolController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void attachBoxSelection(Spatial geom) {
    BoundingVolume bound = geom.getWorldBound();
    if (bound instanceof BoundingBox) {
        BoundingBox bbox = (BoundingBox) bound;
        Vector3f extent = new Vector3f();
        bbox.getExtent(extent);
        WireBox wireBox = new WireBox();
        wireBox.fromBoundingBox(bbox);
        selctionShapeOffset.set(bbox.getCenter()).subtractLocal(geom.getWorldTranslation());
        Geometry selectionGeometry = new Geometry("selection_geometry_sceneviewer", wireBox);
        selectionGeometry.setMaterial(blueMat);
        selectionGeometry.setLocalTransform(geom.getWorldTransform());
        selectionGeometry.setLocalTranslation(bbox.getCenter());
        toolsNode.attachChild(selectionGeometry);
        selectionShape = selectionGeometry;

    }
}
 
Example 3
Source File: TestHoveringTank.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" 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 4
Source File: PointLight.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void computeLastDistance(Spatial owner) {
    if (owner.getWorldBound() != null) {
        BoundingVolume bv = owner.getWorldBound();
        lastDistance = bv.distanceSquaredTo(position);
    } else {
        lastDistance = owner.getWorldTranslation().distanceSquared(position);
    }
}
 
Example 5
Source File: LightProbe.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void computeLastDistance(Spatial owner) {
    if (owner.getWorldBound() != null) {
        BoundingVolume bv = owner.getWorldBound();
        lastDistance = bv.distanceSquaredTo(position);
    } else {
        lastDistance = owner.getWorldTranslation().distanceSquared(position);
    }
}
 
Example 6
Source File: SpotLight.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void computeLastDistance(Spatial owner) {
    if (owner.getWorldBound() != null) {
        BoundingVolume bv = owner.getWorldBound();
        lastDistance = bv.distanceSquaredTo(position);
    } else {
        lastDistance = owner.getWorldTranslation().distanceSquared(position);
    }
}
 
Example 7
Source File: BillboardControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void fixRefreshFlags(){
    // force transforms to update below this node
    spatial.updateGeometricState();
    
    // force world bound to update
    Spatial rootNode = spatial;
    while (rootNode.getParent() != null){
        rootNode = rootNode.getParent();
    }
    rootNode.getWorldBound(); 
}
 
Example 8
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 9
Source File: PointLight.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void computeLastDistance(Spatial owner) {
    if (owner.getWorldBound() != null) {
        BoundingVolume bv = owner.getWorldBound();
        lastDistance = bv.distanceSquaredTo(position);
    } else {
        lastDistance = owner.getWorldTranslation().distanceSquared(position);
    }
}
 
Example 10
Source File: SpotLight.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void computeLastDistance(Spatial owner) {
    if (owner.getWorldBound() != null) {
        BoundingVolume bv = owner.getWorldBound();
        lastDistance = bv.distanceSquaredTo(position);
    } else {
        lastDistance = owner.getWorldTranslation().distanceSquared(position);
    }
}
 
Example 11
Source File: BillboardControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void fixRefreshFlags(){
    // force transforms to update below this node
    spatial.updateGeometricState();
    
    // force world bound to update
    Spatial rootNode = spatial;
    while (rootNode.getParent() != null){
        rootNode = rootNode.getParent();
    }
    rootNode.getWorldBound(); 
}
 
Example 12
Source File: SceneEditTool.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Adjust the scale of the marker so it is relative to the size of the
 * selected spatial. It will have a minimum scale of 2.
 */
private void setAxisMarkerScale(Spatial selected) {
    if (selected != null) {
        if (selected.getWorldBound() instanceof BoundingBox) {
            BoundingBox bbox = (BoundingBox) selected.getWorldBound();
            float smallest = Math.min(Math.min(bbox.getXExtent(), bbox.getYExtent()), bbox.getZExtent());
            float scale = Math.max(1, smallest/2f);
            axisMarker.setLocalScale(new Vector3f(scale,scale,scale));
        }
    } else {
        axisMarker.setLocalScale(new Vector3f(2,2,2));
    }
}
 
Example 13
Source File: SpawnToolControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
/**
 * Spawn models.
 *
 * @param brushRotation the brush rotation.
 * @param contactPoint  the contact point.
 */
@JmeThread
protected void spawn(@NotNull Quaternion brushRotation, @NotNull Vector3f contactPoint) {

    var brushSize = getBrushSize();

    var random = ThreadLocalRandom.current();
    var local = getLocalObjects();
    var spawnPosition = local.nextVector();

    var paintedModel = getPaintedModel();
    var direction = GeomUtils.getDirection(brushRotation, local.nextVector())
            .negateLocal()
            .multLocal(10);

    var sourcePoint = contactPoint.subtract(direction, local.nextVector());
    var ray = local.nextRay();
    ray.setOrigin(sourcePoint);

    var minScale = getMinScale();
    var maxScale = getMaxScale();
    var padding = getPadding();

    var resultPosition = local.nextVector();
    var collisions = local.nextCollisionResults();
    var spawnedCollisions = local.nextCollisionResults();
    var resultScale = local.nextVector();
    var needCalculateScale = !minScale.equals(maxScale);

    var maxCount = (int) Math.max(getBrushPower() / 2F, 1F);
    var spawnedModels = getSpawnedModels();

    for(var count = 0; count < maxCount; count++) {
        for (var attempts = 0; attempts < 10; attempts++, attempts++) {

            collisions.clear();
            spawnedCollisions.clear();

            var x = nextOffset(brushSize, random);
            var y = nextOffset(brushSize, random);
            var z = nextOffset(brushSize, random);

            spawnPosition.set(x, y, z)
                    .addLocal(contactPoint)
                    .subtractLocal(sourcePoint)
                    .normalizeLocal();

            ray.setDirection(spawnPosition);

            paintedModel.collideWith(ray, collisions);

            var closest = collisions.getClosestCollision();
            if (closest == null || contactPoint.distance(closest.getContactPoint()) > brushSize / 2) {
                continue;
            }

            resultPosition.set(closest.getContactPoint())
                    .subtractLocal(paintedModel.getWorldTranslation());

            Spatial clone = examples.get(random.nextInt(0, examples.size())).clone();
            clone.setUserData(KEY_IGNORE_RAY_CAST, Boolean.TRUE);
            clone.setLocalTranslation(resultPosition);

            if (needCalculateScale) {
                clone.setLocalScale(nextScale(minScale, maxScale, resultScale, random));
            } else {
                clone.setLocalScale(minScale);
            }

            clone.updateModelBound();

            var worldBound = clone.getWorldBound();

            if (!Vector3f.ZERO.equals(padding)) {
                worldBound = addPadding(worldBound, padding);
            }

            if (paintedModel.collideWith(worldBound, spawnedCollisions) > 2) {
                continue;
            }

            spawnedModels.add(clone);
            paintedModel.attachChild(clone);
            break;
        }
    }
}