Java Code Examples for com.jme3.input.InputManager#getCursorPosition()

The following examples show how to use com.jme3.input.InputManager#getCursorPosition() . 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: MoveToolControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
@Override
@JmeThread
public void processTransform() {

    final EditorTransformSupport editorControl = getEditorControl();
    final LocalObjects local = LocalObjects.get();

    final Camera camera = editorControl.getCamera();
    final InputManager inputManager = EditorUtil.getInputManager();
    final Vector2f cursorPosition = inputManager.getCursorPosition();
    final CollisionResults results = local.nextCollisionResults();

    final Vector3f position = camera.getWorldCoordinates(cursorPosition, 0f, local.nextVector());
    final Vector3f direction = camera.getWorldCoordinates(cursorPosition, 1f, local.nextVector())
            .subtractLocal(position)
            .normalizeLocal();

    final Ray ray = local.nextRay();
    ray.setOrigin(position);
    ray.setDirection(direction);

    final Node collisionPlane = getCollisionPlane();
    collisionPlane.collideWith(ray, results);

    final CollisionResult result = results.getClosestCollision();
    final Transform transform = editorControl.getTransformCenter();

    // Complex trigonometry formula based on sin(angle)*distance
    if (result == null || transform == null) {
        return;
    }

    final Node parentNode = getParentNode();

    final Vector3f translation = parentNode.getLocalTranslation();
    final Vector3f contactPoint = result.getContactPoint(); // get a point of collisionPlane

    //set new deltaVector if it's not set
    if (Float.isNaN(editorControl.getTransformDeltaX())) {
        editorControl.setTransformDeltaX(translation.getX() - contactPoint.getX());
        editorControl.setTransformDeltaY(translation.getY() - contactPoint.getY());
        editorControl.setTransformDeltaZ(translation.getZ() - contactPoint.getZ());
    }

    // add delta of the picked place
    contactPoint.addLocal(editorControl.getTransformDeltaX(), editorControl.getTransformDeltaY(),
            editorControl.getTransformDeltaZ());

    final Vector3f difference = contactPoint.subtract(translation, local.nextVector());
    float distanceToContactPoint = translation.distance(contactPoint);

    // Picked vector
    final PickedAxis pickedAxis = editorControl.getPickedAxis();
    final TransformationMode transformationMode = editorControl.getTransformationMode();
    final Vector3f pickedVector = transformationMode.getPickedVector(transform, pickedAxis, camera);
    final Quaternion rotation = parentNode.getLocalRotation();

    // the main formula for constraint axis
    final Vector3f normalizedDifference = local.nextVector(difference).normalizeLocal();

    float angle = normalizedDifference.angleBetween(rotation.mult(pickedVector, local.nextVector())
            .normalizeLocal());

    float distanceVec2 = distanceToContactPoint * FastMath.sin(angle);

    // fix if angle>90 degrees
    Vector3f perpendicularVec = collisionPlane.getLocalRotation()
            .mult(Vector3f.UNIT_X, local.nextVector())
            .multLocal(distanceVec2);

    Vector3f checkVec = contactPoint.add(perpendicularVec, local.nextVector())
            .subtractLocal(contactPoint)
            .normalizeLocal();

    float angleCheck = checkVec.angleBetween(normalizedDifference);

    if (angleCheck < FastMath.HALF_PI) {
        perpendicularVec.negateLocal();
    }

    // find distance to move
    float distanceToMove = contactPoint.addLocal(perpendicularVec).distance(translation);

    // invert value if it's needed for negative movement
    if (angle > FastMath.HALF_PI) {
        distanceToMove = -distanceToMove;
    }

    translateObjects(pickedAxis, notNull(editorControl.getToTransform()), transform, distanceToMove);
}
 
Example 2
Source File: RotationToolControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
@Override
@JmeThread
public void processTransform() {

    final LocalObjects local = LocalObjects.get();
    final EditorTransformSupport editorControl = getEditorControl();
    final InputManager inputManager = EditorUtil.getInputManager();
    final Camera camera = editorControl.getCamera();

    final Transform transform = notNull(editorControl.getTransformCenter());

    // cursor position and selected position vectors
    final Vector2f cursorPos = inputManager.getCursorPosition();
    final Vector3f transformOnScreen = camera.getScreenCoordinates(transform.getTranslation());
    final Vector2f selectedCoords = local.nextVector(transformOnScreen.getX(), transformOnScreen.getY());

    //set new deltaVector if it's not set
    if (Float.isNaN(editorControl.getTransformDeltaX())) {
        editorControl.setTransformDeltaX(selectedCoords.getX() - cursorPos.getX());
        editorControl.setTransformDeltaY(selectedCoords.getY() - cursorPos.getY());
    }

    // Picked vector
    final TransformationMode transformationMode = editorControl.getTransformationMode();
    final Vector3f pickedVector = transformationMode.getPickedVector(transform, editorControl.getPickedAxis(), camera);
    final Vector3f deltaVector = local.nextVector(editorControl.getTransformDeltaX(), editorControl.getTransformDeltaY(), 0F);

    // rotate according to angle
    final Vector2f cursorDirection = selectedCoords.subtractLocal(cursorPos).normalizeLocal();
    float angle = cursorDirection.angleBetween(local.nextVector(deltaVector.getX(), deltaVector.getY()));
    angle = FastMath.RAD_TO_DEG * angle * FastMath.DEG_TO_RAD;

    final Node parentNode = getParentNode();
    final Node childNode = getChildNode();

    transformationMode.prepareToRotate(parentNode, childNode, transform, camera);

    final Quaternion rotation = parentNode.getLocalRotation();
    final Vector3f axisToRotate = rotation.mult(pickedVector, local.nextVector());

    float angleCheck = axisToRotate.angleBetween(camera.getDirection(local.nextVector()));

    if (angleCheck > FastMath.HALF_PI) {
        angle = -angle;
    }

    final Quaternion difference = local.nextRotation().fromAngleAxis(angle, pickedVector);
    final Quaternion newRotation = rotation.mult(difference, local.nextRotation());

    parentNode.setLocalRotation(newRotation);

    final Spatial toTransform = notNull(editorControl.getToTransform());
    toTransform.setLocalRotation(childNode.getWorldRotation());

    editorControl.notifyTransformed(toTransform);
}
 
Example 3
Source File: ScaleToolControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
@JmeThread
@Override
public void processTransform() {

    final EditorTransformSupport editorControl = getEditorControl();

    final LocalObjects local = LocalObjects.get();
    final Camera camera = editorControl.getCamera();
    final InputManager inputManager = EditorUtil.getInputManager();
    final Transform transform = notNull(editorControl.getTransformCenter());

    // cursor position and selected position vectors
    final Vector2f cursorPos = inputManager.getCursorPosition();
    final Vector3f transformOnScreen = camera.getScreenCoordinates(transform.getTranslation(), local.nextVector());
    final Vector2f selectedCoords = local.nextVector(transformOnScreen.getX(), transformOnScreen.getY());

    // set new deltaVector if it's not set (scale tool stores position of a cursor)
    if (Float.isNaN(editorControl.getTransformDeltaX())) {
        editorControl.setTransformDeltaX(cursorPos.getX());
        editorControl.setTransformDeltaY(cursorPos.getY());
    }

    final Node parentNode = getParentNode();
    final Node childNode = getChildNode();

    // Picked vector
    final Spatial toTransform = notNull(editorControl.getToTransform());
    final TransformationMode transformationMode = editorControl.getTransformationMode();
    transformationMode.prepareToScale(parentNode, childNode, transform, camera);

    // scale according to distance
    final Vector3f deltaVector = local.nextVector(editorControl.getTransformDeltaX(), editorControl.getTransformDeltaY(), 0F);
    final Vector2f delta2d = local.nextVector(deltaVector.getX(), deltaVector.getY());
    final Vector3f baseScale = local.nextVector(transform.getScale()); // default scale
    final Vector3f pickedVector = local.nextVector(transformationMode.getScaleAxis(transform, editorControl.getPickedAxis(), camera));
    pickedVector.setX(abs(pickedVector.getX()));
    pickedVector.setY(abs(pickedVector.getY()));
    pickedVector.setZ(abs(pickedVector.getZ()));

    if (Config.DEV_TRANSFORMS_DEBUG) {
        System.out.println("Base scale " + baseScale + ", pickedVector " + pickedVector);
    }

    // scale object
    float disCursor = cursorPos.distance(selectedCoords);
    float disDelta = delta2d.distance(selectedCoords);
    float scaleValue = (float) (cursorPos.distance(delta2d) * 0.01f * Math.sqrt(baseScale.length()));

    if (disCursor > disDelta) {
        baseScale.addLocal(pickedVector.mult(scaleValue, local.nextVector()));
    } else {
        scaleValue = Math.min(scaleValue, 0.999f); // remove negateve values
        baseScale.subtractLocal(pickedVector.mult(scaleValue, local.nextVector()));
    }

    parentNode.setLocalScale(baseScale);

    if (Config.DEV_TRANSFORMS_DEBUG) {
        System.out.println("New scale " + baseScale + ", result world " + childNode.getWorldScale());
    }

    parentNode.setLocalScale(baseScale);
    toTransform.setLocalScale(childNode.getWorldScale());

    editorControl.notifyTransformed(toTransform);
}