Java Code Examples for com.jme3.math.Transform#setScale()

The following examples show how to use com.jme3.math.Transform#setScale() . 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: DacLinks.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Calculate the physics transform to match the specified skeleton bone.
 *
 * @param bone the skeleton bone to match (not null, unaffected)
 * @param localOffset the location of the body's center (in the bone's local
 * coordinates, not null, unaffected)
 * @param storeResult storage for the result (modified if not null)
 * @return the calculated physics transform (either storeResult or a new
 * transform, not null)
 */
Transform physicsTransform(Joint bone, Vector3f localOffset,
        Transform storeResult) {
    Transform result
            = (storeResult == null) ? new Transform() : storeResult;
    /*
     * Start with the body's transform in the bone's local coordinates.
     */
    result.setTranslation(localOffset);
    result.setRotation(rotateIdentity);
    result.setScale(1f);
    /*
     * Convert to mesh coordinates.
     */
    Transform localToMesh = bone.getModelTransform();
    result.combineWithParent(localToMesh);
    /*
     * Convert to world (physics-space) coordinates.
     */
    Transform meshToWorld = meshTransform(null);
    result.combineWithParent(meshToWorld);

    return result;
}
 
Example 2
Source File: CollisionShapeFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Calculate the correct transform for a collision shape relative to the
 * ancestor for which the shape was generated.
 *
 * @param spat
 * @param parent
 * @return a new instance (not null)
 */
private static Transform getTransform(Spatial spat, Spatial parent) {
    Transform shapeTransform = new Transform();
    Spatial parentNode = spat.getParent() != null ? spat.getParent() : spat;
    Spatial currentSpatial = spat;
    //if we have parents combine their transforms
    while (parentNode != null) {
        if (parent == currentSpatial) {
            //real parent -> only apply scale, not transform
            Transform trans = new Transform();
            trans.setScale(currentSpatial.getLocalScale());
            shapeTransform.combineWithParent(trans);
            parentNode = null;
        } else {
            shapeTransform.combineWithParent(currentSpatial.getLocalTransform());
            parentNode = currentSpatial.getParent();
            currentSpatial = parentNode;
        }
    }
    return shapeTransform;
}
 
Example 3
Source File: CollisionShapeFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * returns the correct transform for a collisionshape in relation
 * to the ancestor for which the collisionshape is generated
 * @param spat
 * @param parent
 * @return
 */
private static Transform getTransform(Spatial spat, Spatial parent) {
    Transform shapeTransform = new Transform();
    Spatial parentNode = spat.getParent() != null ? spat.getParent() : spat;
    Spatial currentSpatial = spat;
    //if we have parents combine their transforms
    while (parentNode != null) {
        if (parent == currentSpatial) {
            //real parent -> only apply scale, not transform
            Transform trans = new Transform();
            trans.setScale(currentSpatial.getLocalScale());
            shapeTransform.combineWithParent(trans);
            parentNode = null;
        } else {
            shapeTransform.combineWithParent(currentSpatial.getLocalTransform());
            parentNode = currentSpatial.getParent();
            currentSpatial = parentNode;
        }
    }
    return shapeTransform;
}
 
Example 4
Source File: CollisionShapeFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * returns the correct transform for a collisionshape in relation
 * to the ancestor for which the collisionshape is generated
 * @param spat
 * @param parent
 * @return
 */
private static Transform getTransform(Spatial spat, Spatial parent) {
    Transform shapeTransform = new Transform();
    Spatial parentNode = spat.getParent() != null ? spat.getParent() : spat;
    Spatial currentSpatial = spat;
    //if we have parents combine their transforms
    while (parentNode != null) {
        if (parent == currentSpatial) {
            //real parent -> only apply scale, not transform
            Transform trans = new Transform();
            trans.setScale(currentSpatial.getLocalScale());
            shapeTransform.combineWithParent(trans);
            parentNode = null;
        } else {
            shapeTransform.combineWithParent(currentSpatial.getLocalTransform());
            parentNode = currentSpatial.getParent();
            currentSpatial = parentNode;
        }
    }
    return shapeTransform;
}
 
Example 5
Source File: PhysicsLink.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Calculate a physics transform for the rigid body (to match the skeleton
 * bone).
 *
 * @param storeResult storage for the result (modified if not null)
 * @return the calculated transform (in physics-space coordinates, either
 * storeResult or a new transform, not null)
 */
public Transform physicsTransform(Transform storeResult) {
    Transform result
            = (storeResult == null) ? new Transform() : storeResult;
    if (isKinematic()) {
        result.set(kpTransform);
    } else {
        rigidBody.getPhysicsLocation(result.getTranslation());
        rigidBody.getPhysicsRotation(result.getRotation());
        result.setScale(rigidBody.getCollisionShape().getScale());
    }

    return result;
}
 
Example 6
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 7
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 8
Source File: FbxToJmeTrack.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void applyInverse(Vector3f translation, Quaternion rotation, Vector3f scale, Transform inverseBindPose) {
    Transform t = new Transform();
    t.setTranslation(translation);
    t.setRotation(rotation);
    if (scale != null) {
        t.setScale(scale);
    }
    t.combineWithParent(inverseBindPose);
    
    t.getTranslation(translation);
    t.getRotation(rotation);
    if (scale != null) {
        t.getScale(scale);
    }
}
 
Example 9
Source File: ConstraintHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * The method retreives the transform from a feature in a given space.
 * 
 * @param oma
 *            the OMA of the feature (spatial or armature node)
 * @param subtargetName
 *            the feature's subtarget (bone in a case of armature's node)
 * @param space
 *            the space the transform is evaluated to
 * @return thensform of a feature in a given space
 */
public Transform getTransform(Long oma, String subtargetName, Space space) {
    Spatial feature = (Spatial) blenderContext.getLoadedFeature(oma, LoadedFeatureDataType.LOADED_FEATURE);
    boolean isArmature = blenderContext.getMarkerValue(ArmatureHelper.ARMATURE_NODE_MARKER, feature) != null;
    if (isArmature) {
        BoneContext targetBoneContext = blenderContext.getBoneByName(subtargetName);
        Bone bone = targetBoneContext.getBone();

        switch (space) {
            case CONSTRAINT_SPACE_WORLD:
                return new Transform(bone.getModelSpacePosition(), bone.getModelSpaceRotation(), bone.getModelSpaceScale());
            case CONSTRAINT_SPACE_LOCAL:
                Transform localTransform = new Transform(bone.getLocalPosition(), bone.getLocalRotation());
                localTransform.setScale(bone.getLocalScale());
                return localTransform;
            case CONSTRAINT_SPACE_POSE:
                Node nodeWithAnimationControl = blenderContext.getControlledNode(targetBoneContext.getSkeleton());
                Matrix4f m = this.toMatrix(nodeWithAnimationControl.getWorldTransform());
                Matrix4f boneAgainstModifiedNodeMatrix = this.toMatrix(bone.getLocalPosition(), bone.getLocalRotation(), bone.getLocalScale());
                Matrix4f boneWorldMatrix = m.multLocal(boneAgainstModifiedNodeMatrix);

                Matrix4f armatureWorldMatrix = this.toMatrix(feature.getWorldTransform()).invertLocal();
                Matrix4f r2 = armatureWorldMatrix.multLocal(boneWorldMatrix);

                Vector3f loc2 = r2.toTranslationVector();
                Quaternion rot2 = r2.toRotationQuat().normalizeLocal().multLocal(POS_POSE_SPACE_QUATERNION);
                Vector3f scl2 = r2.toScaleVector();

                return new Transform(loc2, rot2, scl2);
            case CONSTRAINT_SPACE_PARLOCAL:
                Matrix4f parentLocalMatrix = Matrix4f.IDENTITY;
                if (bone.getParent() != null) {
                    Bone parent = bone.getParent();
                    parentLocalMatrix = this.toMatrix(parent.getLocalPosition(), parent.getLocalRotation(), parent.getLocalScale());
                } else {
                    // we need to clone it because otherwise we could spoil
                    // the IDENTITY matrix
                    parentLocalMatrix = parentLocalMatrix.clone();
                }
                Matrix4f boneLocalMatrix = this.toMatrix(bone.getLocalPosition(), bone.getLocalRotation(), bone.getLocalScale());
                Matrix4f result = parentLocalMatrix.multLocal(boneLocalMatrix);

                Vector3f loc = result.toTranslationVector();
                Quaternion rot = result.toRotationQuat().normalizeLocal().multLocal(NEG_PARLOC_SPACE_QUATERNION);
                Vector3f scl = result.toScaleVector();
                return new Transform(loc, rot, scl);
            default:
                throw new IllegalStateException("Unknown space type: " + space);
        }
    } else {
        switch (space) {
            case CONSTRAINT_SPACE_LOCAL:
                return feature.getLocalTransform();
            case CONSTRAINT_SPACE_WORLD:
                return feature.getWorldTransform();
            case CONSTRAINT_SPACE_PARLOCAL:
            case CONSTRAINT_SPACE_POSE:
                throw new IllegalStateException("Nodes can have only Local and World spaces applied!");
            default:
                throw new IllegalStateException("Unknown space type: " + space);
        }
    }
}