Didn’t want to just hand out a solution until you had multiple input, but here is pretty much what I use.
A member of JumpButton Studios helped me understand this, I now use it for everything, I even wrote it in C# and C++ it was that damn useful.
You can use it if you wish, this is the basic version that just parents everything, written in Java :
import com.badlogic.gdx.math.Affine2;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
/**
* Represents the data for the position, scale, angle and origin
*
* @author Stephen Gibson
*/
public class Transform {
public static Quaternion ZERO_ROTATION = new Quaternion();
public static Matrix4 ZERO_TRANSFORM = new Matrix4();
public static Vector3 VECTOR_ZERO = new Vector3();
public static Vector3 VECTOR_ONE = new Vector3(1, 1, 1);
// The transform that is parented to this transform, for relative position,
// scale and rotation
protected Transform parent;
public Vector3 position = new Vector3();
public Vector3 scale = new Vector3(1, 1, 1);
public Quaternion rotation = new Quaternion();
// Scale and rotation matrix
public Matrix4 model = new Matrix4();
public Matrix4 transform = new Matrix4();
/**
* Sets the parent of this transform, this transform won't be updated until the next step
* @param parent
*/
public void setParent(Transform parent){
this.parent = parent;
}
public Matrix4 getParentTransform(){
if(parent != null)
return parent.getTransform();
return ZERO_TRANSFORM;
}
public Quaternion getParentRotation(){
if(parent != null)
return parent.getRotation();
return ZERO_ROTATION;
}
public Vector3 getParentScale(){
if(parent != null)
return parent.getScale();
return VECTOR_ONE;
}
public Matrix4 getModelMatrix(){
model.idt();
model.scl(scale);
model.rotate(rotation);
return model;
}
public Matrix4 getTransform(){
transform.idt();
transform.mul(getParentTransform());
transform.translate(position);
transform.mul(getModelMatrix());
return transform;
}
/**
* Gets the position relative to the parent
* @return
*/
public Vector3 getPosition(){
return position.cpy().mul(getParentTransform());
}
/**
* Gets the absolute position
* @return
*/
public Vector3 getLocalPosition(){
return position;
}
/**
* Get the scale relative to the parent
* @return
*/
public Vector3 getScale(){
Vector3 scl = parent == null ? VECTOR_ONE : parent.getScale();
return scale.cpy().scl(scl.x, scl.y, scl.z);
}
/**
* Get the rotation relative to the parent
* @return
*/
public Quaternion getRotation(){
return rotation.cpy().mul(getParentRotation());
}
public void setPosition(Vector2 position){
this.position.set(position, 1 );
}
public void setScale(Vector2 scale){
this.scale.set(scale, 1);
}
public void setRotation(Quaternion rotation){
this.rotation = rotation;
}
public void setAngleAxis(float x, float y, float z, float degrees){
rotation.setFromAxis(x, y, z, degrees);
}
}
This version here, although not written in Java, allows you to choose what part of the transform to parent. Should be easy to duplicate LINK