git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9220 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
be7a22e3d5
commit
9ddb941daa
@ -1,318 +1,318 @@ |
|||||||
/* |
/* |
||||||
* Copyright (c) 2009-2010 jMonkeyEngine |
* Copyright (c) 2009-2010 jMonkeyEngine |
||||||
* All rights reserved. |
* All rights reserved. |
||||||
* |
* |
||||||
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
||||||
* modification, are permitted provided that the following conditions are |
* modification, are permitted provided that the following conditions are |
||||||
* met: |
* met: |
||||||
* |
* |
||||||
* * Redistributions of source code must retain the above copyright |
* * Redistributions of source code must retain the above copyright |
||||||
* notice, this list of conditions and the following disclaimer. |
* notice, this list of conditions and the following disclaimer. |
||||||
* |
* |
||||||
* * Redistributions in binary form must reproduce the above copyright |
* * Redistributions in binary form must reproduce the above copyright |
||||||
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
||||||
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
||||||
* |
* |
||||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
* may be used to endorse or promote products derived from this software |
* may be used to endorse or promote products derived from this software |
||||||
* without specific prior written permission. |
* without specific prior written permission. |
||||||
* |
* |
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
*/ |
*/ |
||||||
|
|
||||||
package com.jme3.math; |
package com.jme3.math; |
||||||
|
|
||||||
import com.jme3.export.*; |
import com.jme3.export.*; |
||||||
import java.io.IOException; |
import java.io.IOException; |
||||||
|
|
||||||
/** |
/** |
||||||
* Started Date: Jul 16, 2004<br><br> |
* Started Date: Jul 16, 2004<br><br> |
||||||
* Represents a translation, rotation and scale in one object. |
* Represents a translation, rotation and scale in one object. |
||||||
* |
* |
||||||
* @author Jack Lindamood |
* @author Jack Lindamood |
||||||
* @author Joshua Slack |
* @author Joshua Slack |
||||||
*/ |
*/ |
||||||
public final class Transform implements Savable, Cloneable, java.io.Serializable { |
public final class Transform implements Savable, Cloneable, java.io.Serializable { |
||||||
|
|
||||||
static final long serialVersionUID = 1; |
static final long serialVersionUID = 1; |
||||||
|
|
||||||
public static final Transform IDENTITY = new Transform(); |
public static final Transform IDENTITY = new Transform(); |
||||||
|
|
||||||
private Quaternion rot = new Quaternion(); |
private Quaternion rot = new Quaternion(); |
||||||
private Vector3f translation = new Vector3f(); |
private Vector3f translation = new Vector3f(); |
||||||
private Vector3f scale = new Vector3f(1,1,1); |
private Vector3f scale = new Vector3f(1,1,1); |
||||||
|
|
||||||
public Transform(Vector3f translation, Quaternion rot){ |
public Transform(Vector3f translation, Quaternion rot){ |
||||||
this.translation.set(translation); |
this.translation.set(translation); |
||||||
this.rot.set(rot); |
this.rot.set(rot); |
||||||
} |
} |
||||||
|
|
||||||
public Transform(Vector3f translation, Quaternion rot, Vector3f scale){ |
public Transform(Vector3f translation, Quaternion rot, Vector3f scale){ |
||||||
this(translation, rot); |
this(translation, rot); |
||||||
this.scale.set(scale); |
this.scale.set(scale); |
||||||
} |
} |
||||||
|
|
||||||
public Transform(Vector3f translation){ |
public Transform(Vector3f translation){ |
||||||
this(translation, Quaternion.IDENTITY); |
this(translation, Quaternion.IDENTITY); |
||||||
} |
} |
||||||
|
|
||||||
public Transform(Quaternion rot){ |
public Transform(Quaternion rot){ |
||||||
this(Vector3f.ZERO, rot); |
this(Vector3f.ZERO, rot); |
||||||
} |
} |
||||||
|
|
||||||
public Transform(){ |
public Transform(){ |
||||||
this(Vector3f.ZERO, Quaternion.IDENTITY); |
this(Vector3f.ZERO, Quaternion.IDENTITY); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Sets this rotation to the given Quaternion value. |
* Sets this rotation to the given Quaternion value. |
||||||
* @param rot The new rotation for this matrix. |
* @param rot The new rotation for this matrix. |
||||||
* @return this |
* @return this |
||||||
*/ |
*/ |
||||||
public Transform setRotation(Quaternion rot) { |
public Transform setRotation(Quaternion rot) { |
||||||
this.rot.set(rot); |
this.rot.set(rot); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Sets this translation to the given value. |
* Sets this translation to the given value. |
||||||
* @param trans The new translation for this matrix. |
* @param trans The new translation for this matrix. |
||||||
* @return this |
* @return this |
||||||
*/ |
*/ |
||||||
public Transform setTranslation(Vector3f trans) { |
public Transform setTranslation(Vector3f trans) { |
||||||
this.translation.set(trans); |
this.translation.set(trans); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Return the translation vector in this matrix. |
* Return the translation vector in this matrix. |
||||||
* @return translation vector. |
* @return translation vector. |
||||||
*/ |
*/ |
||||||
public Vector3f getTranslation() { |
public Vector3f getTranslation() { |
||||||
return translation; |
return translation; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Sets this scale to the given value. |
* Sets this scale to the given value. |
||||||
* @param scale The new scale for this matrix. |
* @param scale The new scale for this matrix. |
||||||
* @return this |
* @return this |
||||||
*/ |
*/ |
||||||
public Transform setScale(Vector3f scale) { |
public Transform setScale(Vector3f scale) { |
||||||
this.scale.set(scale); |
this.scale.set(scale); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Sets this scale to the given value. |
* Sets this scale to the given value. |
||||||
* @param scale The new scale for this matrix. |
* @param scale The new scale for this matrix. |
||||||
* @return this |
* @return this |
||||||
*/ |
*/ |
||||||
public Transform setScale(float scale) { |
public Transform setScale(float scale) { |
||||||
this.scale.set(scale, scale, scale); |
this.scale.set(scale, scale, scale); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Return the scale vector in this matrix. |
* Return the scale vector in this matrix. |
||||||
* @return scale vector. |
* @return scale vector. |
||||||
*/ |
*/ |
||||||
public Vector3f getScale() { |
public Vector3f getScale() { |
||||||
return scale; |
return scale; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Stores this translation value into the given vector3f. If trans is null, a new vector3f is created to |
* Stores this translation value into the given vector3f. If trans is null, a new vector3f is created to |
||||||
* hold the value. The value, once stored, is returned. |
* hold the value. The value, once stored, is returned. |
||||||
* @param trans The store location for this matrix's translation. |
* @param trans The store location for this matrix's translation. |
||||||
* @return The value of this matrix's translation. |
* @return The value of this matrix's translation. |
||||||
*/ |
*/ |
||||||
public Vector3f getTranslation(Vector3f trans) { |
public Vector3f getTranslation(Vector3f trans) { |
||||||
if (trans==null) trans=new Vector3f(); |
if (trans==null) trans=new Vector3f(); |
||||||
trans.set(this.translation); |
trans.set(this.translation); |
||||||
return trans; |
return trans; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Stores this rotation value into the given Quaternion. If quat is null, a new Quaternion is created to |
* Stores this rotation value into the given Quaternion. If quat is null, a new Quaternion is created to |
||||||
* hold the value. The value, once stored, is returned. |
* hold the value. The value, once stored, is returned. |
||||||
* @param quat The store location for this matrix's rotation. |
* @param quat The store location for this matrix's rotation. |
||||||
* @return The value of this matrix's rotation. |
* @return The value of this matrix's rotation. |
||||||
*/ |
*/ |
||||||
public Quaternion getRotation(Quaternion quat) { |
public Quaternion getRotation(Quaternion quat) { |
||||||
if (quat==null) quat=new Quaternion(); |
if (quat==null) quat=new Quaternion(); |
||||||
quat.set(rot); |
quat.set(rot); |
||||||
return quat; |
return quat; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Return the rotation quaternion in this matrix. |
* Return the rotation quaternion in this matrix. |
||||||
* @return rotation quaternion. |
* @return rotation quaternion. |
||||||
*/ |
*/ |
||||||
public Quaternion getRotation() { |
public Quaternion getRotation() { |
||||||
return rot; |
return rot; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Stores this scale value into the given vector3f. If scale is null, a new vector3f is created to |
* Stores this scale value into the given vector3f. If scale is null, a new vector3f is created to |
||||||
* hold the value. The value, once stored, is returned. |
* hold the value. The value, once stored, is returned. |
||||||
* @param scale The store location for this matrix's scale. |
* @param scale The store location for this matrix's scale. |
||||||
* @return The value of this matrix's scale. |
* @return The value of this matrix's scale. |
||||||
*/ |
*/ |
||||||
public Vector3f getScale(Vector3f scale) { |
public Vector3f getScale(Vector3f scale) { |
||||||
if (scale==null) scale=new Vector3f(); |
if (scale==null) scale=new Vector3f(); |
||||||
scale.set(this.scale); |
scale.set(this.scale); |
||||||
return scale; |
return scale; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Sets this matrix to the interpolation between the first matrix and the second by delta amount. |
* Sets this matrix to the interpolation between the first matrix and the second by delta amount. |
||||||
* @param t1 The begining transform. |
* @param t1 The begining transform. |
||||||
* @param t2 The ending transform. |
* @param t2 The ending transform. |
||||||
* @param delta An amount between 0 and 1 representing how far to interpolate from t1 to t2. |
* @param delta An amount between 0 and 1 representing how far to interpolate from t1 to t2. |
||||||
*/ |
*/ |
||||||
public void interpolateTransforms(Transform t1, Transform t2, float delta) { |
public void interpolateTransforms(Transform t1, Transform t2, float delta) { |
||||||
this.rot.slerp(t1.rot,t2.rot,delta); |
this.rot.slerp(t1.rot,t2.rot,delta); |
||||||
this.translation.interpolate(t1.translation,t2.translation,delta); |
this.translation.interpolate(t1.translation,t2.translation,delta); |
||||||
this.scale.interpolate(t1.scale,t2.scale,delta); |
this.scale.interpolate(t1.scale,t2.scale,delta); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Changes the values of this matrix acording to it's parent. Very similar to the concept of Node/Spatial transforms. |
* Changes the values of this matrix acording to it's parent. Very similar to the concept of Node/Spatial transforms. |
||||||
* @param parent The parent matrix. |
* @param parent The parent matrix. |
||||||
* @return This matrix, after combining. |
* @return This matrix, after combining. |
||||||
*/ |
*/ |
||||||
public Transform combineWithParent(Transform parent) { |
public Transform combineWithParent(Transform parent) { |
||||||
scale.multLocal(parent.scale); |
scale.multLocal(parent.scale); |
||||||
// rot.multLocal(parent.rot);
|
// rot.multLocal(parent.rot);
|
||||||
parent.rot.mult(rot, rot); |
parent.rot.mult(rot, rot); |
||||||
|
|
||||||
// This here, is evil code
|
// This here, is evil code
|
||||||
// parent
|
// parent
|
||||||
// .rot
|
// .rot
|
||||||
// .multLocal(translation)
|
// .multLocal(translation)
|
||||||
// .multLocal(parent.scale)
|
// .multLocal(parent.scale)
|
||||||
// .addLocal(parent.translation);
|
// .addLocal(parent.translation);
|
||||||
|
|
||||||
translation.multLocal(parent.scale); |
translation.multLocal(parent.scale); |
||||||
parent |
parent |
||||||
.rot |
.rot |
||||||
.multLocal(translation) |
.multLocal(translation) |
||||||
.addLocal(parent.translation); |
.addLocal(parent.translation); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Sets this matrix's translation to the given x,y,z values. |
* Sets this matrix's translation to the given x,y,z values. |
||||||
* @param x This matrix's new x translation. |
* @param x This matrix's new x translation. |
||||||
* @param y This matrix's new y translation. |
* @param y This matrix's new y translation. |
||||||
* @param z This matrix's new z translation. |
* @param z This matrix's new z translation. |
||||||
* @return this |
* @return this |
||||||
*/ |
*/ |
||||||
public Transform setTranslation(float x,float y, float z) { |
public Transform setTranslation(float x,float y, float z) { |
||||||
translation.set(x,y,z); |
translation.set(x,y,z); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Sets this matrix's scale to the given x,y,z values. |
* Sets this matrix's scale to the given x,y,z values. |
||||||
* @param x This matrix's new x scale. |
* @param x This matrix's new x scale. |
||||||
* @param y This matrix's new y scale. |
* @param y This matrix's new y scale. |
||||||
* @param z This matrix's new z scale. |
* @param z This matrix's new z scale. |
||||||
* @return this |
* @return this |
||||||
*/ |
*/ |
||||||
public Transform setScale(float x, float y, float z) { |
public Transform setScale(float x, float y, float z) { |
||||||
scale.set(x,y,z); |
scale.set(x,y,z); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
public Vector3f transformVector(final Vector3f in, Vector3f store){ |
public Vector3f transformVector(final Vector3f in, Vector3f store){ |
||||||
if (store == null) |
if (store == null) |
||||||
store = new Vector3f(); |
store = new Vector3f(); |
||||||
|
|
||||||
// multiply with scale first, then rotate, finally translate (cf.
|
// multiply with scale first, then rotate, finally translate (cf.
|
||||||
// Eberly)
|
// Eberly)
|
||||||
return rot.mult(store.set(in).multLocal(scale), store).addLocal(translation); |
return rot.mult(store.set(in).multLocal(scale), store).addLocal(translation); |
||||||
} |
} |
||||||
|
|
||||||
public Vector3f transformInverseVector(final Vector3f in, Vector3f store){ |
public Vector3f transformInverseVector(final Vector3f in, Vector3f store){ |
||||||
if (store == null) |
if (store == null) |
||||||
store = new Vector3f(); |
store = new Vector3f(); |
||||||
|
|
||||||
// The author of this code should look above and take the inverse of that
|
// The author of this code should look above and take the inverse of that
|
||||||
// But for some reason, they didnt ..
|
// But for some reason, they didnt ..
|
||||||
// in.subtract(translation, store).divideLocal(scale);
|
// in.subtract(translation, store).divideLocal(scale);
|
||||||
// rot.inverse().mult(store, store);
|
// rot.inverse().mult(store, store);
|
||||||
|
|
||||||
in.subtract(translation, store); |
in.subtract(translation, store); |
||||||
rot.inverse().mult(store, store); |
rot.inverse().mult(store, store); |
||||||
store.divideLocal(scale); |
store.divideLocal(scale); |
||||||
|
|
||||||
return store; |
return store; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Loads the identity. Equal to translation=1,1,1 scale=0,0,0 rot=0,0,0,1. |
* Loads the identity. Equal to translation=0,0,0 scale=1,1,1 rot=0,0,0,1. |
||||||
*/ |
*/ |
||||||
public void loadIdentity() { |
public void loadIdentity() { |
||||||
translation.set(0,0,0); |
translation.set(0,0,0); |
||||||
scale.set(1,1,1); |
scale.set(1,1,1); |
||||||
rot.set(0,0,0,1); |
rot.set(0,0,0,1); |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public String toString(){ |
public String toString(){ |
||||||
return getClass().getSimpleName() + "[ " + translation.x + ", " + translation.y + ", " + translation.z + "]\n" |
return getClass().getSimpleName() + "[ " + translation.x + ", " + translation.y + ", " + translation.z + "]\n" |
||||||
+ "[ " + rot.x + ", " + rot.y + ", " + rot.z + ", " + rot.w + "]\n" |
+ "[ " + rot.x + ", " + rot.y + ", " + rot.z + ", " + rot.w + "]\n" |
||||||
+ "[ " + scale.x + " , " + scale.y + ", " + scale.z + "]"; |
+ "[ " + scale.x + " , " + scale.y + ", " + scale.z + "]"; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Sets this matrix to be equal to the given matrix. |
* Sets this matrix to be equal to the given matrix. |
||||||
* @param matrixQuat The matrix to be equal to. |
* @param matrixQuat The matrix to be equal to. |
||||||
* @return this |
* @return this |
||||||
*/ |
*/ |
||||||
public Transform set(Transform matrixQuat) { |
public Transform set(Transform matrixQuat) { |
||||||
this.translation.set(matrixQuat.translation); |
this.translation.set(matrixQuat.translation); |
||||||
this.rot.set(matrixQuat.rot); |
this.rot.set(matrixQuat.rot); |
||||||
this.scale.set(matrixQuat.scale); |
this.scale.set(matrixQuat.scale); |
||||||
return this; |
return this; |
||||||
} |
} |
||||||
|
|
||||||
public void write(JmeExporter e) throws IOException { |
public void write(JmeExporter e) throws IOException { |
||||||
OutputCapsule capsule = e.getCapsule(this); |
OutputCapsule capsule = e.getCapsule(this); |
||||||
capsule.write(rot, "rot", new Quaternion()); |
capsule.write(rot, "rot", new Quaternion()); |
||||||
capsule.write(translation, "translation", Vector3f.ZERO); |
capsule.write(translation, "translation", Vector3f.ZERO); |
||||||
capsule.write(scale, "scale", Vector3f.UNIT_XYZ); |
capsule.write(scale, "scale", Vector3f.UNIT_XYZ); |
||||||
} |
} |
||||||
|
|
||||||
public void read(JmeImporter e) throws IOException { |
public void read(JmeImporter e) throws IOException { |
||||||
InputCapsule capsule = e.getCapsule(this); |
InputCapsule capsule = e.getCapsule(this); |
||||||
|
|
||||||
rot = (Quaternion)capsule.readSavable("rot", new Quaternion()); |
rot = (Quaternion)capsule.readSavable("rot", new Quaternion()); |
||||||
translation = (Vector3f)capsule.readSavable("translation", Vector3f.ZERO); |
translation = (Vector3f)capsule.readSavable("translation", Vector3f.ZERO); |
||||||
scale = (Vector3f)capsule.readSavable("scale", Vector3f.UNIT_XYZ); |
scale = (Vector3f)capsule.readSavable("scale", Vector3f.UNIT_XYZ); |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
public Transform clone() { |
public Transform clone() { |
||||||
try { |
try { |
||||||
Transform tq = (Transform) super.clone(); |
Transform tq = (Transform) super.clone(); |
||||||
tq.rot = rot.clone(); |
tq.rot = rot.clone(); |
||||||
tq.scale = scale.clone(); |
tq.scale = scale.clone(); |
||||||
tq.translation = translation.clone(); |
tq.translation = translation.clone(); |
||||||
return tq; |
return tq; |
||||||
} catch (CloneNotSupportedException e) { |
} catch (CloneNotSupportedException e) { |
||||||
throw new AssertionError(); |
throw new AssertionError(); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue