Java Code Examples for com.jme3.math.Vector3f#subtractLocal()

The following examples show how to use com.jme3.math.Vector3f#subtractLocal() . 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: TestReverb.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
  time += tpf;

  if (time > nextTime) {
    Vector3f v = new Vector3f();
    v.setX(FastMath.nextRandomFloat());
    v.setY(FastMath.nextRandomFloat());
    v.setZ(FastMath.nextRandomFloat());
    v.multLocal(40, 2, 40);
    v.subtractLocal(20, 1, 20);

    audioSource.setLocalTranslation(v);
    audioSource.playInstance();
    time = 0;
    nextTime = FastMath.nextRandomFloat() * 2 + 0.5f;
  }
}
 
Example 2
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 3
Source File: RagUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Convert a transform from the mesh coordinate system to the local
 * coordinate system of the specified bone.
 *
 * @param parentBone (not null)
 * @param transform the transform to convert (not null, modified)
 */
static void meshToLocal(Joint parentBone, Transform transform) {
    Vector3f location = transform.getTranslation();
    Quaternion orientation = transform.getRotation();
    Vector3f scale = transform.getScale();

    Transform pmx = parentBone.getModelTransform();
    Vector3f pmTranslate = pmx.getTranslation();
    Quaternion pmRotInv = pmx.getRotation().inverse();
    Vector3f pmScale = pmx.getScale();

    location.subtractLocal(pmTranslate);
    location.divideLocal(pmScale);
    pmRotInv.mult(location, location);
    scale.divideLocal(pmScale);
    pmRotInv.mult(orientation, orientation);
}
 
Example 4
Source File: DacLinks.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a jointless BoneLink for the named bone, and add it to the
 * boneLinks map.
 *
 * @param boneName the name of the bone to be linked (not null)
 * @param vertexLocations the set of vertex locations (not null, not empty)
 */
private void createBoneLink(String boneName, VectorSet vertexLocations) {
    Joint bone = findBone(boneName);
    Transform boneToMesh = bone.getModelTransform();
    Transform meshToBone = boneToMesh.invert();
    //logger3.log(Level.INFO, "meshToBone = {0}", meshToBone);
    /*
     * Create the CollisionShape and locate the center of mass.
     */
    CollisionShape shape;
    Vector3f center;
    if (vertexLocations == null || vertexLocations.numVectors() == 0) {
        throw new IllegalStateException("no vertex for " + boneName);
    } else {
        center = vertexLocations.mean(null);
        center.subtractLocal(bone.getModelTransform().getTranslation());
        shape = createShape(meshToBone, center, vertexLocations);
    }

    meshToBone.getTranslation().zero();
    float mass = super.mass(boneName);
    Vector3f offset = meshToBone.transformVector(center, null);
    BoneLink link = new BoneLink(this, bone, shape, mass, offset);
    boneLinks.put(boneName, link);
}
 
Example 5
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 6
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 7
Source File: TestReverb.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
  time += tpf;

  if (time > nextTime) {
    Vector3f v = new Vector3f();
    v.setX(FastMath.nextRandomFloat());
    v.setY(FastMath.nextRandomFloat());
    v.setZ(FastMath.nextRandomFloat());
    v.multLocal(40, 2, 40);
    v.subtractLocal(20, 1, 20);

    audioSource.setLocalTranslation(v);
    audioSource.playInstance();
    time = 0;
    nextTime = FastMath.nextRandomFloat() * 2 + 0.5f;
  }
}
 
Example 8
Source File: RigidBodyMotionState.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
     * If the motion state has been updated, apply the new transform to the
     * specified spatial.
     *
     * @param spatial where to apply the physics transform (not null, modified)
     * @return true if changed
     */
    public boolean applyTransform(Spatial spatial) {
        Vector3f localLocation = spatial.getLocalTranslation();
        Quaternion localRotationQuat = spatial.getLocalRotation();
        boolean physicsLocationDirty = applyTransform(motionStateId, localLocation, localRotationQuat);
        if (!physicsLocationDirty) {
            return false;
        }
        if (!applyPhysicsLocal && spatial.getParent() != null) {
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);

//            localRotationQuat.set(worldRotationQuat);
            tmp_inverseWorldRotation.mult(localRotationQuat, localRotationQuat);

            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
        } else {
            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
//            spatial.setLocalTranslation(worldLocation);
//            spatial.setLocalRotation(worldRotationQuat);
        }
        if (vehicle != null) {
            vehicle.updateWheels();
        }
        return true;
    }
 
Example 9
Source File: TorsoLink.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Calculate the local bone transform to match the physics transform of the
 * rigid body.
 *
 * @param storeResult storage for the result (modified if not null)
 * @return the calculated bone transform (in local coordinates, either
 * storeResult or a new transform, not null)
 */
private Transform localBoneTransform(Transform storeResult) {
    Transform result
            = (storeResult == null) ? new Transform() : storeResult;
    Vector3f location = result.getTranslation();
    Quaternion orientation = result.getRotation();
    Vector3f scale = result.getScale();
    /*
     * Start with the rigid body's transform in physics/world coordinates.
     */
    PhysicsRigidBody body = getRigidBody();
    body.getPhysicsLocation(result.getTranslation());
    body.getPhysicsRotation(result.getRotation());
    result.setScale(body.getCollisionShape().getScale());
    /*
     * Convert to mesh coordinates.
     */
    Transform worldToMesh = getControl().meshTransform(null).invert();
    result.combineWithParent(worldToMesh);
    /*
     * Subtract the body's local offset, rotated and scaled.
     */
    Vector3f meshOffset = localOffset(null);
    meshOffset.multLocal(scale);
    orientation.mult(meshOffset, meshOffset);
    location.subtractLocal(meshOffset);

    return result;
}
 
Example 10
Source File: BoneLink.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Calculate the local bone transform to match the physics transform of the
 * rigid body.
 *
 * @param storeResult storage for the result (modified if not null)
 * @return the calculated bone transform (in local coordinates, either
 * storeResult or a new transform, not null)
 */
private Transform localBoneTransform(Transform storeResult) {
    Transform result
            = (storeResult == null) ? new Transform() : storeResult;
    Vector3f location = result.getTranslation();
    Quaternion orientation = result.getRotation();
    Vector3f scale = result.getScale();
    /*
     * Start with the rigid body's transform in physics/world coordinates.
     */
    PhysicsRigidBody body = getRigidBody();
    body.getPhysicsLocation(result.getTranslation());
    body.getPhysicsRotation(result.getRotation());
    result.setScale(body.getCollisionShape().getScale());
    /*
     * Convert to mesh coordinates.
     */
    Transform worldToMesh = getControl().meshTransform(null).invert();
    result.combineWithParent(worldToMesh);
    /*
     * Convert to the bone's local coordinate system by factoring out the
     * parent bone's transform.
     */
    Joint parentBone = getBone().getParent();
    RagUtils.meshToLocal(parentBone, result);
    /*
     * Subtract the body's local offset, rotated and scaled.
     */
    Vector3f parentOffset = localOffset(null);
    parentOffset.multLocal(scale);
    orientation.mult(parentOffset, parentOffset);
    location.subtractLocal(parentOffset);

    return result;
}
 
Example 11
Source File: WaterUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void updateReflectionCam(Camera reflectionCam, Plane plane, Camera sceneCam){
    
    TempVars vars = TempVars.get();
     //Temp vects for reflection cam orientation calculation
    Vector3f sceneTarget =  vars.vect1;
    Vector3f  reflectDirection =  vars.vect2;
    Vector3f  reflectUp =  vars.vect3;
    Vector3f  reflectLeft = vars.vect4;
    Vector3f  camLoc = vars.vect5;
    camLoc = plane.reflect(sceneCam.getLocation(), camLoc);
    reflectionCam.setLocation(camLoc);
    reflectionCam.setFrustum(sceneCam.getFrustumNear(),
            sceneCam.getFrustumFar(),
            sceneCam.getFrustumLeft(),
            sceneCam.getFrustumRight(),
            sceneCam.getFrustumTop(),
            sceneCam.getFrustumBottom());
    reflectionCam.setParallelProjection(sceneCam.isParallelProjection());

    sceneTarget.set(sceneCam.getLocation()).addLocal(sceneCam.getDirection(vars.vect6));
    reflectDirection = plane.reflect(sceneTarget, reflectDirection);
    reflectDirection.subtractLocal(camLoc);

    sceneTarget.set(sceneCam.getLocation()).subtractLocal(sceneCam.getUp(vars.vect6));
    reflectUp = plane.reflect(sceneTarget, reflectUp);
    reflectUp.subtractLocal(camLoc);

    sceneTarget.set(sceneCam.getLocation()).addLocal(sceneCam.getLeft(vars.vect6));
    reflectLeft = plane.reflect(sceneTarget, reflectLeft);
    reflectLeft.subtractLocal(camLoc);

    reflectionCam.setAxes(reflectLeft, reflectUp, reflectDirection);

    vars.release();
}
 
Example 12
Source File: RigidBodyMotionState.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
     * applies the current transform to the given jme Node if the location has been updated on the physics side
     * @param spatial
     */
    public synchronized boolean applyTransform(Spatial spatial) {
        Vector3f localLocation = spatial.getLocalTranslation();
        Quaternion localRotationQuat = spatial.getLocalRotation();
        boolean physicsLocationDirty = applyTransform(motionStateId, localLocation, localRotationQuat);
        if (!physicsLocationDirty) {
            return false;
        }
        if (!applyPhysicsLocal && spatial.getParent() != null) {
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);

//            localRotationQuat.set(worldRotationQuat);
            tmp_inverseWorldRotation.mult(localRotationQuat, localRotationQuat);

            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
        } else {
            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
//            spatial.setLocalTranslation(worldLocation);
//            spatial.setLocalRotation(worldRotationQuat);
        }
        if (vehicle != null) {
            vehicle.updateWheels();
        }
        return true;
    }
 
Example 13
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);
}