Java Code Examples for com.jme3.math.Quaternion#lookAt()

The following examples show how to use com.jme3.math.Quaternion#lookAt() . 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: CharacterControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void update(float tpf) {
    if (enabled && spatial != null) {
        Quaternion localRotationQuat = spatial.getLocalRotation();
        Vector3f localLocation = spatial.getLocalTranslation();
        if (!applyLocal && spatial.getParent() != null) {
            getPhysicsLocation(localLocation);
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            spatial.setLocalTranslation(localLocation);

            if (useViewDirection) {
                localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
                spatial.setLocalRotation(localRotationQuat);
            }
        } else {
            spatial.setLocalTranslation(getPhysicsLocation());
            localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
            spatial.setLocalRotation(localRotationQuat);
        }
    }
}
 
Example 2
Source File: CharacterControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void update(float tpf) {
    if (enabled && spatial != null) {
        Quaternion localRotationQuat = spatial.getLocalRotation();
        Vector3f localLocation = spatial.getLocalTranslation();
        if (!applyLocal && spatial.getParent() != null) {
            getPhysicsLocation(localLocation);
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            spatial.setLocalTranslation(localLocation);

            if (useViewDirection) {
                localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
                spatial.setLocalRotation(localRotationQuat);
            }
        } else {
            spatial.setLocalTranslation(getPhysicsLocation());
            localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
            spatial.setLocalRotation(localRotationQuat);
        }
    }
}
 
Example 3
Source File: CharacterControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void update(float tpf) {
    if (enabled && spatial != null) {
        Quaternion localRotationQuat = spatial.getLocalRotation();
        Vector3f localLocation = spatial.getLocalTranslation();
        if (!applyLocal && spatial.getParent() != null) {
            getPhysicsLocation(localLocation);
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            spatial.setLocalTranslation(localLocation);

            if (useViewDirection) {
                localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
                spatial.setLocalRotation(localRotationQuat);
            }
        } else {
            spatial.setLocalTranslation(getPhysicsLocation());
            localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
            spatial.setLocalRotation(localRotationQuat);
        }
    }
}
 
Example 4
Source File: EditorAudioNode.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Synchronize this node with audio node.
 */
@JmeThread
public void sync() {

    final AudioNode audioNode = getAudioNode();
    if (audioNode == null) return;

    final LocalObjects local = LocalObjects.get();
    final Quaternion rotation = local.nextRotation();
    rotation.lookAt(audioNode.getDirection(), camera.getUp(local.nextVector()));

    final Node editedNode = getEditedNode();
    editedNode.setLocalRotation(rotation);
    editedNode.setLocalTranslation(audioNode.getLocalTranslation());
}
 
Example 5
Source File: TestMousePick.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
    public void simpleUpdate(float tpf){
        Vector3f origin    = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
        Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
        direction.subtractLocal(origin).normalizeLocal();

        Ray ray = new Ray(origin, direction);
        CollisionResults results = new CollisionResults();
        shootables.collideWith(ray, results);
//        System.out.println("----- Collisions? " + results.size() + "-----");
//        for (int i = 0; i < results.size(); i++) {
//            // For each hit, we know distance, impact point, name of geometry.
//            float dist = results.getCollision(i).getDistance();
//            Vector3f pt = results.getCollision(i).getWorldContactPoint();
//            String hit = results.getCollision(i).getGeometry().getName();
//            System.out.println("* Collision #" + i);
//            System.out.println("  You shot " + hit + " at " + pt + ", " + dist + " wu away.");
//        }
        if (results.size() > 0) {
            CollisionResult closest = results.getClosestCollision();
            mark.setLocalTranslation(closest.getContactPoint());

            Quaternion q = new Quaternion();
            q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y);
            mark.setLocalRotation(q);

            rootNode.attachChild(mark);
        } else {
            rootNode.detachChild(mark);
        }
    }
 
Example 6
Source File: MotionEvent.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void computeTargetDirection() {
    switch (directionType) {
        case Path:
            Quaternion q = new Quaternion();
            q.lookAt(direction, upVector);
            spatial.setLocalRotation(q);
            break;
        case LookAt:
            if (lookAt != null) {
                spatial.lookAt(lookAt, upVector);
            }
            break;
        case PathAndRotation:
            if (rotation != null) {
                Quaternion q2 = new Quaternion();
                q2.lookAt(direction, upVector);
                q2.multLocal(rotation);
                spatial.setLocalRotation(q2);
            }
            break;
        case Rotation:
            if (rotation != null) {
                spatial.setLocalRotation(rotation);
            }
            break;
        case None:
            break;
        default:
            break;
    }
}
 
Example 7
Source File: TestMousePick.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
    public void simpleUpdate(float tpf){
        Vector3f origin    = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
        Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
        direction.subtractLocal(origin).normalizeLocal();

        Ray ray = new Ray(origin, direction);
        CollisionResults results = new CollisionResults();
        shootables.collideWith(ray, results);
//        System.out.println("----- Collisions? " + results.size() + "-----");
//        for (int i = 0; i < results.size(); i++) {
//            // For each hit, we know distance, impact point, name of geometry.
//            float dist = results.getCollision(i).getDistance();
//            Vector3f pt = results.getCollision(i).getWorldContactPoint();
//            String hit = results.getCollision(i).getGeometry().getName();
//            System.out.println("* Collision #" + i);
//            System.out.println("  You shot " + hit + " at " + pt + ", " + dist + " wu away.");
//        }
        if (results.size() > 0) {
            CollisionResult closest = results.getClosestCollision();
            mark.setLocalTranslation(closest.getContactPoint());

            Quaternion q = new Quaternion();
            q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y);
            mark.setLocalRotation(q);

            rootNode.attachChild(mark);
        } else {
            rootNode.detachChild(mark);
        }
    }
 
Example 8
Source File: MotionTrack.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void computeTargetDirection() {
    switch (directionType) {
        case Path:
            Quaternion q = new Quaternion();
            q.lookAt(direction, Vector3f.UNIT_Y);
            spatial.setLocalRotation(q);
            break;
        case LookAt:
            if (lookAt != null) {
                spatial.lookAt(lookAt, upVector);
            }
            break;
        case PathAndRotation:
            if (rotation != null) {
                Quaternion q2 = new Quaternion();
                q2.lookAt(direction, Vector3f.UNIT_Y);
                q2.multLocal(rotation);
                spatial.setLocalRotation(q2);
            }
            break;
        case Rotation:
            if (rotation != null) {
                spatial.setLocalRotation(rotation);
            }
            break;
        case None:
            break;
        default:
            break;
    }
}
 
Example 9
Source File: SimpleMoveBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Method for rotating agent in direction of velocity of agent.
 *
 * @param tpf time per frame
 */
public void rotateAgent(float tpf) {
    Quaternion q = new Quaternion();
    q.lookAt(moveDirection, new Vector3f(0, 1, 0));
    agent.getLocalRotation().slerp(q, agent.getRotationSpeed() * tpf);
}
 
Example 10
Source File: AbstractSteeringBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Method for rotating agent in direction of velocity of agent.
 *
 * @param tpf time per frame
 */
protected void rotateAgent(float tpf) {
    Quaternion q = new Quaternion();
    q.lookAt(velocity, new Vector3f(0, 1, 0));
    agent.getLocalRotation().slerp(q, agent.getRotationSpeed() * tpf);
}
 
Example 11
Source File: TestBetterCharacter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void simpleInitApp() {
    //setup keyboard mapping
    setupKeys();

    // activate physics
    bulletAppState = new BulletAppState() {
        @Override
        public void prePhysicsTick(PhysicsSpace space, float tpf) {
            // Apply radial gravity near the planet, downward gravity elsewhere.
            checkPlanetGravity();
        }
    };
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);

    // init a physics test scene
    PhysicsTestHelper.createPhysicsTestWorldSoccer(rootNode, assetManager, bulletAppState.getPhysicsSpace());
    PhysicsTestHelper.createBallShooter(this, rootNode, bulletAppState.getPhysicsSpace());
    setupPlanet();

    // Create a node for the character model
    characterNode = new Node("character node");
    characterNode.setLocalTranslation(new Vector3f(4, 5, 2));

    // Add a character control to the node so we can add other things and
    // control the model rotation
    physicsCharacter = new BetterCharacterControl(0.3f, 2.5f, 8f);
    characterNode.addControl(physicsCharacter);
    getPhysicsSpace().add(physicsCharacter);

    // Load model, attach to character node
    Node model = (Node) assetManager.loadModel("Models/Jaime/Jaime.j3o");
    model.setLocalScale(1.50f);
    characterNode.attachChild(model);

    // Add character node to the rootNode
    rootNode.attachChild(characterNode);

    // Set forward camera node that follows the character, only used when
    // view is "locked"
    camNode = new CameraNode("CamNode", cam);
    camNode.setControlDir(ControlDirection.SpatialToCamera);
    camNode.setLocalTranslation(new Vector3f(0, 2, -6));
    Quaternion quat = new Quaternion();
    // These coordinates are local, the camNode is attached to the character node!
    quat.lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
    camNode.setLocalRotation(quat);
    characterNode.attachChild(camNode);
    // Disable by default, can be enabled via keyboard shortcut
    camNode.setEnabled(false);
}