Search in sources :

Example 6 with BoundingVolume

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

the class LodControl method controlRender.

protected void controlRender(RenderManager rm, ViewPort vp) {
    BoundingVolume bv = spatial.getWorldBound();
    Camera cam = vp.getCamera();
    float atanNH = FastMath.atan(cam.getFrustumNear() * cam.getFrustumTop());
    float ratio = (FastMath.PI / (8f * atanNH));
    float newDistance = bv.distanceTo(vp.getCamera().getLocation()) / ratio;
    int level;
    if (Math.abs(newDistance - lastDistance) <= distTolerance) {
        // we haven't moved relative to the model, send the old measurement back.
        level = lastLevel;
    } else if (lastDistance > newDistance && lastLevel == 0) {
        // we're already at the lowest setting and we just got closer to the model, no need to keep trying.
        level = lastLevel;
    } else if (lastDistance < newDistance && lastLevel == numLevels - 1) {
        // we're already at the highest setting and we just got further from the model, no need to keep trying.
        level = lastLevel;
    } else {
        lastDistance = newDistance;
        // estimate area of polygon via bounding volume
        float area = AreaUtils.calcScreenArea(bv, lastDistance, cam.getWidth());
        float trisToDraw = area * trisPerPixel;
        level = numLevels - 1;
        for (int i = numLevels; --i >= 0; ) {
            if (trisToDraw - numTris[i] < 0) {
                break;
            }
            level = i;
        }
        lastLevel = level;
    }
    spatial.setLodLevel(level);
}
Also used : BoundingVolume(com.jme3.bounding.BoundingVolume) Camera(com.jme3.renderer.Camera)

Example 7 with BoundingVolume

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

the class CubeField method gameLogic.

/**
     * Core Game Logic
     */
private void gameLogic(float tpf) {
    //Subtract difficulty level in accordance to speed every 10 seconds
    if (timer.getTimeInSeconds() >= coreTime2) {
        coreTime2 = timer.getTimeInSeconds() + 10;
        if (difficulty <= lowCap) {
            difficulty = lowCap;
        } else if (difficulty > lowCap) {
            difficulty -= 5;
            diffHelp += 1;
        }
    }
    if (speed < .1f) {
        speed += .000001f * tpf * fpsRate;
    }
    player.move(speed * tpf * fpsRate, 0, 0);
    if (cubeField.size() > difficulty) {
        cubeField.remove(0);
    } else if (cubeField.size() != difficulty) {
        randomizeCube();
    }
    if (cubeField.isEmpty()) {
        requestClose(false);
    } else {
        for (int i = 0; i < cubeField.size(); i++) {
            //better way to check collision
            Geometry playerModel = (Geometry) player.getChild(0);
            Geometry cubeModel = cubeField.get(i);
            cubeModel.updateGeometricState();
            BoundingVolume pVol = playerModel.getWorldBound();
            BoundingVolume vVol = cubeModel.getWorldBound();
            if (pVol.intersects(vVol)) {
                gameLost();
                return;
            }
            //Remove cube if 10 world units behind player
            if (cubeField.get(i).getLocalTranslation().getX() + 10 < player.getLocalTranslation().getX()) {
                cubeField.get(i).removeFromParent();
                cubeField.remove(cubeField.get(i));
            }
        }
    }
    Score += fpsRate * tpf;
    fpsScoreText.setText("Current Score: " + Score);
}
Also used : Geometry(com.jme3.scene.Geometry) BoundingVolume(com.jme3.bounding.BoundingVolume)

Example 8 with BoundingVolume

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

the class TestTriangleCollision method simpleUpdate.

@Override
public void simpleUpdate(float tpf) {
    CollisionResults results = new CollisionResults();
    BoundingVolume bv = geom1.getWorldBound();
    golem.collideWith(bv, results);
    if (results.size() > 0) {
        geom1.getMaterial().setColor("Color", ColorRGBA.Red);
    } else {
        geom1.getMaterial().setColor("Color", ColorRGBA.Blue);
    }
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) BoundingVolume(com.jme3.bounding.BoundingVolume)

Example 9 with BoundingVolume

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

the class CollisionUtil method checkCollisionBase.

private static void checkCollisionBase(Collidable a, Collidable b, int expected) {
    // Test bounding volume methods
    if (a instanceof BoundingVolume && b instanceof BoundingVolume) {
        BoundingVolume bv1 = (BoundingVolume) a;
        BoundingVolume bv2 = (BoundingVolume) b;
        assert bv1.intersects(bv2) == (expected != 0);
    }
    // Test standard collideWith method
    CollisionResults results = new CollisionResults();
    int numCollisions = a.collideWith(b, results);
    assert results.size() == numCollisions;
    assert numCollisions == expected;
    // force the results to be sorted here..
    results.getClosestCollision();
    if (results.size() > 0) {
        assert results.getCollision(0) == results.getClosestCollision();
    }
    if (results.size() == 1) {
        assert results.getClosestCollision() == results.getFarthestCollision();
    }
}
Also used : BoundingVolume(com.jme3.bounding.BoundingVolume)

Example 10 with BoundingVolume

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

the class ArrayModifier method apply.

@Override
public void apply(Node node, BlenderContext blenderContext) {
    if (invalid) {
        LOGGER.log(Level.WARNING, "Array modifier is invalid! Cannot be applied to: {0}", node.getName());
    } else {
        TemporalMesh temporalMesh = this.getTemporalMesh(node);
        if (temporalMesh != null) {
            LOGGER.log(Level.FINE, "Applying array modifier to: {0}", temporalMesh);
            if (offset == null) {
                // the node will be repeated several times in the same place
                offset = new float[] { 0.0f, 0.0f, 0.0f };
            }
            if (scale == null) {
                // the node will be repeated several times in the same place
                scale = new float[] { 0.0f, 0.0f, 0.0f };
            } else {
                // getting bounding box
                temporalMesh.updateModelBound();
                BoundingVolume boundingVolume = temporalMesh.getWorldBound();
                if (boundingVolume instanceof BoundingBox) {
                    scale[0] *= ((BoundingBox) boundingVolume).getXExtent() * 2.0f;
                    scale[1] *= ((BoundingBox) boundingVolume).getYExtent() * 2.0f;
                    scale[2] *= ((BoundingBox) boundingVolume).getZExtent() * 2.0f;
                } else if (boundingVolume instanceof BoundingSphere) {
                    float radius = ((BoundingSphere) boundingVolume).getRadius();
                    scale[0] *= radius * 2.0f;
                    scale[1] *= radius * 2.0f;
                    scale[2] *= radius * 2.0f;
                } else {
                    throw new IllegalStateException("Unknown bounding volume type: " + boundingVolume.getClass().getName());
                }
            }
            // adding object's offset
            float[] objectOffset = new float[] { 0.0f, 0.0f, 0.0f };
            if (pOffsetObject != null && pOffsetObject.isNotNull()) {
                FileBlockHeader offsetObjectBlock = blenderContext.getFileBlock(pOffsetObject.getOldMemoryAddress());
                ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class);
                try {
                    // we take the structure in case the object was not yet loaded
                    Structure offsetStructure = offsetObjectBlock.getStructure(blenderContext);
                    Vector3f translation = objectHelper.getTransformation(offsetStructure, blenderContext).getTranslation();
                    objectOffset[0] = translation.x;
                    objectOffset[1] = translation.y;
                    objectOffset[2] = translation.z;
                } catch (BlenderFileException e) {
                    LOGGER.log(Level.WARNING, "Problems in blender file structure! Object offset cannot be applied! The problem: {0}", e.getMessage());
                }
            }
            // getting start and end caps
            MeshHelper meshHelper = blenderContext.getHelper(MeshHelper.class);
            TemporalMesh[] caps = new TemporalMesh[] { null, null };
            Pointer[] pCaps = new Pointer[] { pStartCap, pEndCap };
            for (int i = 0; i < pCaps.length; ++i) {
                if (pCaps[i].isNotNull()) {
                    FileBlockHeader capBlock = blenderContext.getFileBlock(pCaps[i].getOldMemoryAddress());
                    try {
                        // we take the structure in case the object was not yet loaded
                        Structure capStructure = capBlock.getStructure(blenderContext);
                        Pointer pMesh = (Pointer) capStructure.getFieldValue("data");
                        List<Structure> meshesArray = pMesh.fetchData();
                        caps[i] = meshHelper.toTemporalMesh(meshesArray.get(0), blenderContext);
                    } catch (BlenderFileException e) {
                        LOGGER.log(Level.WARNING, "Problems in blender file structure! Cap object cannot be applied! The problem: {0}", e.getMessage());
                    }
                }
            }
            Vector3f translationVector = new Vector3f(offset[0] + scale[0] + objectOffset[0], offset[1] + scale[1] + objectOffset[1], offset[2] + scale[2] + objectOffset[2]);
            if (blenderContext.getBlenderKey().isFixUpAxis()) {
                float y = translationVector.y;
                translationVector.y = translationVector.z;
                translationVector.z = y == 0 ? 0 : -y;
            }
            // getting/calculating repeats amount
            int count = 0;
            if (fittype == 0) {
                // Fixed count
                count = this.count - 1;
            } else if (fittype == 1) {
                // Fixed length
                float length = this.length;
                if (translationVector.length() > 0.0f) {
                    count = (int) (length / translationVector.length()) - 1;
                }
            } else if (fittype == 2) {
                // Fit curve
                throw new IllegalStateException("Fit curve should be transformed to Fixed Length array type!");
            } else {
                throw new IllegalStateException("Unknown fit type: " + fittype);
            }
            // adding translated nodes and caps
            Vector3f totalTranslation = new Vector3f(translationVector);
            if (count > 0) {
                TemporalMesh originalMesh = temporalMesh.clone();
                for (int i = 0; i < count; ++i) {
                    TemporalMesh clone = originalMesh.clone();
                    for (Vector3f v : clone.getVertices()) {
                        v.addLocal(totalTranslation);
                    }
                    temporalMesh.append(clone);
                    totalTranslation.addLocal(translationVector);
                }
            }
            if (caps[0] != null) {
                translationVector.multLocal(-1);
                TemporalMesh capsClone = caps[0].clone();
                for (Vector3f v : capsClone.getVertices()) {
                    v.addLocal(translationVector);
                }
                temporalMesh.append(capsClone);
            }
            if (caps[1] != null) {
                TemporalMesh capsClone = caps[1].clone();
                for (Vector3f v : capsClone.getVertices()) {
                    v.addLocal(totalTranslation);
                }
                temporalMesh.append(capsClone);
            }
        } else {
            LOGGER.log(Level.WARNING, "Cannot find temporal mesh for node: {0}. The modifier will NOT be applied!", node);
        }
    }
}
Also used : ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) BoundingSphere(com.jme3.bounding.BoundingSphere) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) Pointer(com.jme3.scene.plugins.blender.file.Pointer) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) BoundingVolume(com.jme3.bounding.BoundingVolume) Structure(com.jme3.scene.plugins.blender.file.Structure) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper)

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