Search in sources :

Example 6 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class LightFilterTest method testSpotFiltering.

@Test
public void testSpotFiltering() {
    SpotLight sl = new SpotLight(Vector3f.ZERO, Vector3f.UNIT_Z);
    sl.setSpotRange(0);
    geom.addLight(sl);
    // Infinite spot lights are only filtered
    checkFilteredLights(1);
    // if the geometry is outside the infinite cone.
    TempVars vars = TempVars.get();
    try {
        // The spot is not touching the near plane of the camera yet, 
        // should still be culled.
        sl.setSpotRange(1f - FastMath.ZERO_TOLERANCE);
        assert !sl.intersectsFrustum(cam, vars);
        // should be culled from the geometry's PoV
        checkFilteredLights(0);
        // Now it touches the near plane.
        sl.setSpotRange(1f);
        // still culled from the geometry's PoV
        checkFilteredLights(0);
        assert sl.intersectsFrustum(cam, vars);
    } finally {
        vars.release();
    }
    // make it barely reach the geometry
    sl.setSpotRange(9f);
    checkFilteredLights(0);
    // make it reach the geometry (touching its bound)
    sl.setSpotRange(9f + FastMath.ZERO_TOLERANCE);
    checkFilteredLights(1);
    // rotate the cone a bit so it no longer faces the geom
    sl.setDirection(new Vector3f(0.316f, 0, 0.948f).normalizeLocal());
    checkFilteredLights(0);
    // extent the range much farther
    sl.setSpotRange(20);
    checkFilteredLights(0);
    // Create box of size X=10 (double the extent)
    // now, the spot will touch the box.
    geom.setMesh(new Box(5, 1, 1));
    checkFilteredLights(1);
    // ==================================
    // Tests for bounding sphere, with a radius of 1f (in the box geom)
    sl.setPosition(Vector3f.ZERO);
    sl.setDirection(Vector3f.UNIT_Z);
    geom.setLocalTranslation(Vector3f.ZERO);
    geom.setModelBound(new BoundingSphere(1f, Vector3f.ZERO));
    // Infinit spot lights are only filtered
    // if the geometry is outside the infinite cone.
    sl.setSpotRange(0);
    checkFilteredLights(1);
    //the geommetry is outside the infinit cone (cone direction going away from the geom)
    sl.setPosition(Vector3f.UNIT_Z.mult(1 + FastMath.ZERO_TOLERANCE));
    checkFilteredLights(0);
    //place the spote ligth in the corner of the box geom, (in order to test bounding sphere)
    sl.setDirection(new Vector3f(1, 1, 0).normalizeLocal());
    geom.setLocalTranslation(0, 0, 10);
    sl.setPosition(sl.getDirection().mult(-2f).add(geom.getLocalTranslation()));
    // make it barely reach the sphere, incorect with a box
    sl.setSpotRange(1f - FastMath.ZERO_TOLERANCE);
    checkFilteredLights(0);
    // make it reach the sphere
    sl.setSpotRange(1f + FastMath.ZERO_TOLERANCE);
    checkFilteredLights(1);
    // extent the range
    sl.setPosition(Vector3f.ZERO);
    sl.setDirection(Vector3f.UNIT_Z);
    sl.setSpotRange(20);
    checkFilteredLights(1);
    // rotate the cone a bit so it no longer faces the geom
    sl.setDirection(new Vector3f(0, 0.3f, 0.7f).normalizeLocal());
    checkFilteredLights(0);
    // Create sphere of size X=10 (double the radius)
    // now, the spot will touch the sphere.
    geom.setModelBound(new BoundingSphere(5f, Vector3f.ZERO));
    checkFilteredLights(1);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) Vector3f(com.jme3.math.Vector3f) Box(com.jme3.scene.shape.Box) TempVars(com.jme3.util.TempVars) Test(org.junit.Test)

Example 7 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method prePhysicsTick.

/**
     * Used internally, don't call manually
     *
     * @param space
     * @param tpf
     */
public void prePhysicsTick(PhysicsSpace space, float tpf) {
    checkOnGround();
    if (wantToUnDuck && checkCanUnDuck()) {
        setHeightPercent(1);
        wantToUnDuck = false;
        ducked = false;
    }
    TempVars vars = TempVars.get();
    Vector3f currentVelocity = vars.vect2.set(velocity);
    // dampen existing x/z forces
    float existingLeftVelocity = velocity.dot(localLeft);
    float existingForwardVelocity = velocity.dot(localForward);
    Vector3f counter = vars.vect1;
    existingLeftVelocity = existingLeftVelocity * physicsDamping;
    existingForwardVelocity = existingForwardVelocity * physicsDamping;
    counter.set(-existingLeftVelocity, 0, -existingForwardVelocity);
    localForwardRotation.multLocal(counter);
    velocity.addLocal(counter);
    float designatedVelocity = walkDirection.length();
    if (designatedVelocity > 0) {
        Vector3f localWalkDirection = vars.vect1;
        //normalize walkdirection
        localWalkDirection.set(walkDirection).normalizeLocal();
        //check for the existing velocity in the desired direction
        float existingVelocity = velocity.dot(localWalkDirection);
        //calculate the final velocity in the desired direction
        float finalVelocity = designatedVelocity - existingVelocity;
        localWalkDirection.multLocal(finalVelocity);
        //add resulting vector to existing velocity
        velocity.addLocal(localWalkDirection);
    }
    if (currentVelocity.distance(velocity) > FastMath.ZERO_TOLERANCE)
        rigidBody.setLinearVelocity(velocity);
    if (jump) {
        //TODO: precalculate jump force
        Vector3f rotatedJumpForce = vars.vect1;
        rotatedJumpForce.set(jumpForce);
        rigidBody.applyImpulse(localForwardRotation.multLocal(rotatedJumpForce), Vector3f.ZERO);
        jump = false;
    }
    vars.release();
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 8 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method calculateNewForward.

/**
     * This method works similar to Camera.lookAt but where lookAt sets the
     * priority on the direction, this method sets the priority on the up vector
     * so that the result direction vector and rotation is guaranteed to be
     * perpendicular to the up vector.
     *
     * @param rotation The rotation to set the result on or null to create a new
     * Quaternion, this will be set to the new "z-forward" rotation if not null
     * @param direction The direction to base the new look direction on, will be
     * set to the new direction
     * @param worldUpVector The up vector to use, the result direction will be
     * perpendicular to this
     * @return
     */
protected final void calculateNewForward(Quaternion rotation, Vector3f direction, Vector3f worldUpVector) {
    if (direction == null) {
        return;
    }
    TempVars vars = TempVars.get();
    Vector3f newLeft = vars.vect1;
    Vector3f newLeftNegate = vars.vect2;
    newLeft.set(worldUpVector).crossLocal(direction).normalizeLocal();
    if (newLeft.equals(Vector3f.ZERO)) {
        if (direction.x != 0) {
            newLeft.set(direction.y, -direction.x, 0f).normalizeLocal();
        } else {
            newLeft.set(0f, direction.z, -direction.y).normalizeLocal();
        }
        logger.log(Level.INFO, "Zero left for direction {0}, up {1}", new Object[] { direction, worldUpVector });
    }
    newLeftNegate.set(newLeft).negateLocal();
    direction.set(worldUpVector).crossLocal(newLeftNegate).normalizeLocal();
    if (direction.equals(Vector3f.ZERO)) {
        direction.set(Vector3f.UNIT_Z);
        logger.log(Level.INFO, "Zero left for left {0}, up {1}", new Object[] { newLeft, worldUpVector });
    }
    if (rotation != null) {
        rotation.fromAxes(newLeft, worldUpVector, direction);
    }
    vars.release();
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 9 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class PssmShadowRenderer method postQueue.

@SuppressWarnings("fallthrough")
public void postQueue(RenderQueue rq) {
    for (Spatial scene : viewPort.getScenes()) {
        ShadowUtil.getGeometriesInCamFrustum(scene, viewPort.getCamera(), ShadowMode.Receive, lightReceivers);
    }
    Camera viewCam = viewPort.getCamera();
    float zFar = zFarOverride;
    if (zFar == 0) {
        zFar = viewCam.getFrustumFar();
    }
    //We prevent computing the frustum points and splits with zeroed or negative near clip value
    float frustumNear = Math.max(viewCam.getFrustumNear(), 0.001f);
    ShadowUtil.updateFrustumPoints(viewCam, frustumNear, zFar, 1.0f, points);
    //shadowCam.setDirection(direction);
    shadowCam.getRotation().lookAt(direction, shadowCam.getUp());
    shadowCam.update();
    shadowCam.updateViewProjection();
    PssmShadowUtil.updateFrustumSplits(splitsArray, frustumNear, zFar, lambda);
    switch(splitsArray.length) {
        case 5:
            splits.a = splitsArray[4];
        case 4:
            splits.b = splitsArray[3];
        case 3:
            splits.g = splitsArray[2];
        case 2:
        case 1:
            splits.r = splitsArray[1];
            break;
    }
    Renderer r = renderManager.getRenderer();
    renderManager.setForcedMaterial(preshadowMat);
    renderManager.setForcedTechnique("PreShadow");
    for (int i = 0; i < nbSplits; i++) {
        // update frustum points based on current camera and split
        ShadowUtil.updateFrustumPoints(viewCam, splitsArray[i], splitsArray[i + 1], 1.0f, points);
        //Updating shadow cam with curent split frustra
        ShadowUtil.updateShadowCamera(viewPort, lightReceivers, shadowCam, points, splitOccluders, shadowMapSize);
        //saving light view projection matrix for this split            
        lightViewProjectionsMatrices[i].set(shadowCam.getViewProjectionMatrix());
        renderManager.setCamera(shadowCam, false);
        if (debugfrustums) {
            //                    frustrumFromBound(b.casterBB,ColorRGBA.Blue );
            //                    frustrumFromBound(b.receiverBB,ColorRGBA.Green );
            //                    frustrumFromBound(b.splitBB,ColorRGBA.Yellow );
            ((Node) viewPort.getScenes().get(0)).attachChild(createFrustum(points, i));
            ShadowUtil.updateFrustumPoints2(shadowCam, points);
            ((Node) viewPort.getScenes().get(0)).attachChild(createFrustum(points, i));
        }
        r.setFrameBuffer(shadowFB[i]);
        r.clearBuffers(true, true, true);
        // render shadow casters to shadow map
        viewPort.getQueue().renderShadowQueue(splitOccluders, renderManager, shadowCam, true);
    }
    debugfrustums = false;
    //restore setting for future rendering
    r.setFrameBuffer(viewPort.getOutputFrameBuffer());
    renderManager.setForcedMaterial(null);
    renderManager.setForcedTechnique(null);
    renderManager.setCamera(viewCam, false);
}
Also used : Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) Renderer(com.jme3.renderer.Renderer) Camera(com.jme3.renderer.Camera)

Example 10 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class HelloPhysics method makeCannonBall.

/** This method creates one individual physical cannon ball.
   * By defaul, the ball is accelerated and flies
   * from the camera position in the camera direction.*/
public void makeCannonBall() {
    /** Create a cannon ball geometry and attach to scene graph. */
    Geometry ball_geo = new Geometry("cannon ball", sphere);
    ball_geo.setMaterial(stone_mat);
    rootNode.attachChild(ball_geo);
    /** Position the cannon ball  */
    ball_geo.setLocalTranslation(cam.getLocation());
    /** Make the ball physcial with a mass > 0.0f */
    ball_phy = new RigidBodyControl(1f);
    /** Add physical ball to physics space. */
    ball_geo.addControl(ball_phy);
    bulletAppState.getPhysicsSpace().add(ball_phy);
    /** Accelerate the physcial ball to shoot it. */
    ball_phy.setLinearVelocity(cam.getDirection().mult(25));
}
Also used : Geometry(com.jme3.scene.Geometry) RigidBodyControl(com.jme3.bullet.control.RigidBodyControl)

Aggregations

Vector3f (com.jme3.math.Vector3f)26 TempVars (com.jme3.util.TempVars)19 CollisionResult (com.jme3.collision.CollisionResult)6 ColorRGBA (com.jme3.math.ColorRGBA)6 Quaternion (com.jme3.math.Quaternion)6 Geometry (com.jme3.scene.Geometry)4 CollisionResults (com.jme3.collision.CollisionResults)3 DirectionalLight (com.jme3.light.DirectionalLight)3 Ray (com.jme3.math.Ray)3 Vector2f (com.jme3.math.Vector2f)3 Renderer (com.jme3.renderer.Renderer)3 CameraNode (com.jme3.scene.CameraNode)3 Node (com.jme3.scene.Node)3 BoundingSphere (com.jme3.bounding.BoundingSphere)2 VehicleWheel (com.jme3.bullet.objects.VehicleWheel)2 InputCapsule (com.jme3.export.InputCapsule)2 OutputCapsule (com.jme3.export.OutputCapsule)2 AmbientLight (com.jme3.light.AmbientLight)2 Light (com.jme3.light.Light)2 PointLight (com.jme3.light.PointLight)2