Java Code Examples for com.jme3.scene.Spatial#getLocalTranslation()

The following examples show how to use com.jme3.scene.Spatial#getLocalTranslation() . 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: AbstractPhysicsDebugControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Apply the specified location and orientation to the specified spatial.
 *
 * @param worldLocation location vector (in physics-space coordinates, not
 * null, unaffected)
 * @param worldRotation orientation (in physics-space coordinates, not null,
 * unaffected)
 * @param spatial where to apply (may be null)
 */
protected void applyPhysicsTransform(Vector3f worldLocation, Quaternion worldRotation, Spatial spatial) {
    if (spatial != null) {
        Vector3f localLocation = spatial.getLocalTranslation();
        Quaternion localRotationQuat = spatial.getLocalRotation();
        if (spatial.getParent() != null) {
            localLocation.set(worldLocation).subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            localRotationQuat.set(worldRotation);
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);
            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
        } else {
            spatial.setLocalTranslation(worldLocation);
            spatial.setLocalRotation(worldRotation);
        }
    }

}
 
Example 2
Source File: VehicleEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void doCenterSelected(final Spatial selected) {
    final Vector3f location = new Vector3f(selected.getLocalTranslation());
    selected.center();
    Lookup.getDefault().lookup(SceneUndoRedoManager.class).addEdit(this, new AbstractUndoableSceneEdit() {

        @Override
        public void sceneUndo() {
            //undo stuff here
            selected.setLocalTranslation(location);
        }

        @Override
        public void sceneRedo() {
            //redo stuff here
            selected.center();
        }

        @Override
        public void awtRedo() {
        }

        @Override
        public void awtUndo() {
        }
    });
}
 
Example 3
Source File: SceneEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void doMoveSpatial(Spatial selected, Vector3f translation) {
    Vector3f localTranslation = selected.getLocalTranslation();
    Vector3f before = new Vector3f(localTranslation);
    Node parent = selected.getParent();
    if (parent != null) {
        localTranslation.set(translation).subtractLocal(parent.getWorldTranslation());
        localTranslation.divideLocal(parent.getWorldScale());
        //TODO: reuse quaternion..
        new Quaternion().set(parent.getWorldRotation()).inverseLocal().multLocal(localTranslation);
    } else {
        localTranslation.set(translation);
    }
    Vector3f after = new Vector3f(localTranslation);
    selected.setLocalTranslation(localTranslation);
    AbstractSceneExplorerNode selectedSpat = this.selectedSpat;
    moveUndo(selected, before, after, selectedSpat);
}
 
Example 4
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 5
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 6
Source File: SpatialTweens.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 *  Creates a tween that will interpolate the location of the specified target 
 *  from one location to another.  If either location is null then they will 
 *  be substituted with the Spatial's current local translation AT THE TIME OF THIS CALL.
 */
public static Tween move( Spatial target, Vector3f from, Vector3f to, double length ) {
    from = from != null ? from : target.getLocalTranslation();
    to = to != null ? to : target.getLocalTranslation();
    return new MoveSpatial(target, from, to, length);
}
 
Example 7
Source File: TestInstanceNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
    time += tpf;

    if (time > 1f) {
        time = 0f;
        
        for (Spatial instance : instancedNode.getChildren()) {
            if (!(instance instanceof InstancedGeometry)) {
                Geometry geom = (Geometry) instance;
                geom.setMaterial(materials[FastMath.nextRandomInt(0, materials.length - 1)]);

                Mesh mesh; 
                if (FastMath.nextRandomInt(0, 1) == 1) mesh = mesh2;
                else mesh = mesh1;
                geom.setMesh(mesh);
            }
        }
    }
    
    for (Spatial child : instancedNode.getChildren()) {
        if (!(child instanceof InstancedGeometry)) {
            float val = ((Float)child.getUserData("height")).floatValue();
            float dir = ((Float)child.getUserData("dir")).floatValue();

            val += (dir + ((FastMath.nextRandomFloat() * 0.5f) - 0.25f)) * tpf;

            if (val > 1f) {
                val = 1f;
                dir = -dir;
            } else if (val < 0f) {
                val = 0f;
                dir = -dir;
            }

            Vector3f translation = child.getLocalTranslation();
            translation.y = (smoothstep(0, 1, val) * 2.5f) - 1.25f;

            child.setUserData("height", val);
            child.setUserData("dir", dir);

            child.setLocalTranslation(translation);
        }
    }
}
 
Example 8
Source File: SceneEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void doNudgeSpatial(Spatial selected, Vector3f translation) {
    Vector3f before = new Vector3f(selected.getLocalTranslation());
    selected.setLocalTranslation(before.add(translation));
    Vector3f after = new Vector3f(selected.getLocalTranslation());
    nudgeUndo(selected, before, after, selectedSpat);
}
 
Example 9
Source File: SpatialTweens.java    From Lemur with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 *  Creates a tween that will interpolate the location of the specified target 
 *  from one location to another.  If either location is null then they will 
 *  be substituted with the Spatial's current local translation AT THE TIME OF THIS CALL.
 *  This method will use the distance between the two locations as the 
 *  tween length.  This makes it easier to create a sequence of movements
 *  that all move at the same speed.  The overall sequence can always be rescaled
 *  to fit whatever outer time constraints required. 
 */
public static Tween move( Spatial target, Vector3f from, Vector3f to ) {
    from = from != null ? from : target.getLocalTranslation();
    to = to != null ? to : target.getLocalTranslation();
    return new MoveSpatial(target, from, to);
}