Search in sources :

Example 11 with Direction

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

the class TestCameraNode method simpleInitApp.

public void simpleInitApp() {
    // load a teapot model 
    teaGeom = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj");
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
    teaGeom.setMaterial(mat);
    //create a node to attach the geometry and the camera node
    teaNode = new Node("teaNode");
    teaNode.attachChild(teaGeom);
    rootNode.attachChild(teaNode);
    // create a floor
    mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
    Geometry ground = new Geometry("ground", new Quad(50, 50));
    ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
    ground.setLocalTranslation(-25, -1, 25);
    ground.setMaterial(mat);
    rootNode.attachChild(ground);
    //creating the camera Node
    camNode = new CameraNode("CamNode", cam);
    //Setting the direction to Spatial to camera, this means the camera will copy the movements of the Node
    camNode.setControlDir(ControlDirection.SpatialToCamera);
    //attaching the camNode to the teaNode
    teaNode.attachChild(camNode);
    //setting the local translation of the cam node to move it away from the teanNode a bit
    camNode.setLocalTranslation(new Vector3f(-10, 0, 0));
    //setting the camNode to look at the teaNode
    camNode.lookAt(teaNode.getLocalTranslation(), Vector3f.UNIT_Y);
    //disable the default 1st-person flyCam (don't forget this!!)
    flyCam.setEnabled(false);
    registerInput();
}
Also used : Geometry(com.jme3.scene.Geometry) Quad(com.jme3.scene.shape.Quad) Quaternion(com.jme3.math.Quaternion) Node(com.jme3.scene.Node) CameraNode(com.jme3.scene.CameraNode) CameraNode(com.jme3.scene.CameraNode) Vector3f(com.jme3.math.Vector3f) Material(com.jme3.material.Material)

Example 12 with Direction

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

the class TerrainGrid method getCamCell.

/**
     * Get the location in cell-coordinates of the specified location.
     * Cell coordinates are integer corrdinates, usually with y=0, each 
     * representing a cell in the world.
     * For example, moving right in the +X direction:
     * (0,0,0) (1,0,0) (2,0,0), (3,0,0)
     * and then down the -Z direction:
     * (3,0,-1) (3,0,-2) (3,0,-3)
     */
public Vector3f getCamCell(Vector3f location) {
    Vector3f tile = getTileCell(location);
    Vector3f offsetHalf = new Vector3f(-0.5f, 0, -0.5f);
    Vector3f shifted = tile.subtract(offsetHalf);
    return new Vector3f(FastMath.floor(shifted.x), 0, FastMath.floor(shifted.z));
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 13 with Direction

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

the class Camera method lookAt.

/**
     * <code>lookAt</code> is a convenience method for auto-setting the frame
     * based on a world position the user desires the camera to look at. It
     * repoints the camera towards the given position using the difference
     * between the position and the current camera location as a direction
     * vector and the worldUpVector to compute up and left camera vectors.
     *
     * @param pos           where to look at in terms of world coordinates
     * @param worldUpVector a normalized vector indicating the up direction of the world.
     *                      (typically {0, 1, 0} in jME.)
     */
public void lookAt(Vector3f pos, Vector3f worldUpVector) {
    TempVars vars = TempVars.get();
    Vector3f newDirection = vars.vect1;
    Vector3f newUp = vars.vect2;
    Vector3f newLeft = vars.vect3;
    newDirection.set(pos).subtractLocal(location).normalizeLocal();
    newUp.set(worldUpVector).normalizeLocal();
    if (newUp.equals(Vector3f.ZERO)) {
        newUp.set(Vector3f.UNIT_Y);
    }
    newLeft.set(newUp).crossLocal(newDirection).normalizeLocal();
    if (newLeft.equals(Vector3f.ZERO)) {
        if (newDirection.x != 0) {
            newLeft.set(newDirection.y, -newDirection.x, 0f);
        } else {
            newLeft.set(0f, newDirection.z, -newDirection.y);
        }
    }
    newUp.set(newDirection).crossLocal(newLeft).normalizeLocal();
    this.rotation.fromAxes(newLeft, newUp, newDirection);
    this.rotation.normalizeLocal();
    vars.release();
    onFrameChange();
}
Also used : TempVars(com.jme3.util.TempVars)

Example 14 with Direction

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

the class Camera method onFrameChange.

/**
     * <code>onFrameChange</code> updates the view frame of the camera.
     */
public void onFrameChange() {
    TempVars vars = TempVars.get();
    Vector3f left = getLeft(vars.vect1);
    Vector3f direction = getDirection(vars.vect2);
    Vector3f up = getUp(vars.vect3);
    float dirDotLocation = direction.dot(location);
    // left plane
    Vector3f leftPlaneNormal = worldPlane[LEFT_PLANE].getNormal();
    leftPlaneNormal.x = left.x * coeffLeft[0];
    leftPlaneNormal.y = left.y * coeffLeft[0];
    leftPlaneNormal.z = left.z * coeffLeft[0];
    leftPlaneNormal.addLocal(direction.x * coeffLeft[1], direction.y * coeffLeft[1], direction.z * coeffLeft[1]);
    worldPlane[LEFT_PLANE].setConstant(location.dot(leftPlaneNormal));
    // right plane
    Vector3f rightPlaneNormal = worldPlane[RIGHT_PLANE].getNormal();
    rightPlaneNormal.x = left.x * coeffRight[0];
    rightPlaneNormal.y = left.y * coeffRight[0];
    rightPlaneNormal.z = left.z * coeffRight[0];
    rightPlaneNormal.addLocal(direction.x * coeffRight[1], direction.y * coeffRight[1], direction.z * coeffRight[1]);
    worldPlane[RIGHT_PLANE].setConstant(location.dot(rightPlaneNormal));
    // bottom plane
    Vector3f bottomPlaneNormal = worldPlane[BOTTOM_PLANE].getNormal();
    bottomPlaneNormal.x = up.x * coeffBottom[0];
    bottomPlaneNormal.y = up.y * coeffBottom[0];
    bottomPlaneNormal.z = up.z * coeffBottom[0];
    bottomPlaneNormal.addLocal(direction.x * coeffBottom[1], direction.y * coeffBottom[1], direction.z * coeffBottom[1]);
    worldPlane[BOTTOM_PLANE].setConstant(location.dot(bottomPlaneNormal));
    // top plane
    Vector3f topPlaneNormal = worldPlane[TOP_PLANE].getNormal();
    topPlaneNormal.x = up.x * coeffTop[0];
    topPlaneNormal.y = up.y * coeffTop[0];
    topPlaneNormal.z = up.z * coeffTop[0];
    topPlaneNormal.addLocal(direction.x * coeffTop[1], direction.y * coeffTop[1], direction.z * coeffTop[1]);
    worldPlane[TOP_PLANE].setConstant(location.dot(topPlaneNormal));
    if (isParallelProjection()) {
        worldPlane[LEFT_PLANE].setConstant(worldPlane[LEFT_PLANE].getConstant() + frustumLeft);
        worldPlane[RIGHT_PLANE].setConstant(worldPlane[RIGHT_PLANE].getConstant() - frustumRight);
        worldPlane[TOP_PLANE].setConstant(worldPlane[TOP_PLANE].getConstant() - frustumTop);
        worldPlane[BOTTOM_PLANE].setConstant(worldPlane[BOTTOM_PLANE].getConstant() + frustumBottom);
    }
    // far plane
    worldPlane[FAR_PLANE].setNormal(left);
    worldPlane[FAR_PLANE].setNormal(-direction.x, -direction.y, -direction.z);
    worldPlane[FAR_PLANE].setConstant(-(dirDotLocation + frustumFar));
    // near plane
    worldPlane[NEAR_PLANE].setNormal(direction.x, direction.y, direction.z);
    worldPlane[NEAR_PLANE].setConstant(dirDotLocation + frustumNear);
    viewMatrix.fromFrame(location, direction, up, left);
    vars.release();
    //        viewMatrix.transposeLocal();
    updateViewProjection();
}
Also used : TempVars(com.jme3.util.TempVars)

Example 15 with Direction

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

the class Spatial method lookAt.

/**
     * <code>lookAt</code> is a convenience method for auto-setting the local
     * rotation based on a position in world space and an up vector. It computes the rotation
     * to transform the z-axis to point onto 'position' and the y-axis to 'up'.
     * Unlike {@link Quaternion#lookAt(com.jme3.math.Vector3f, com.jme3.math.Vector3f) }
     * this method takes a world position to look at and not a relative direction.
     *
     * Note : 28/01/2013 this method has been fixed as it was not taking into account the parent rotation.
     * This was resulting in improper rotation when the spatial had rotated parent nodes.
     * This method is intended to work in world space, so no matter what parent graph the
     * spatial has, it will look at the given position in world space.
     *
     * @param position
     *            where to look at in terms of world coordinates
     * @param upVector
     *            a vector indicating the (local) up direction. (typically {0,
     *            1, 0} in jME.)
     */
public void lookAt(Vector3f position, Vector3f upVector) {
    Vector3f worldTranslation = getWorldTranslation();
    TempVars vars = TempVars.get();
    Vector3f compVecA = vars.vect4;
    compVecA.set(position).subtractLocal(worldTranslation);
    getLocalRotation().lookAt(compVecA, upVector);
    if (getParent() != null) {
        Quaternion rot = vars.quat1;
        rot = rot.set(parent.getWorldRotation()).inverseLocal().multLocal(getLocalRotation());
        rot.normalizeLocal();
        setLocalRotation(rot);
    }
    vars.release();
    setTransformRefresh();
}
Also used : TempVars(com.jme3.util.TempVars)

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