Skip to content

Instantly share code, notes, and snippets.

@MichaelShaw
Created June 12, 2012 14:35
Show Gist options
  • Save MichaelShaw/2917907 to your computer and use it in GitHub Desktop.
Save MichaelShaw/2917907 to your computer and use it in GitHub Desktop.
Basic Skeletal Animation code in Scala
class Skeleton(val name:String) {
val joints = new ArrayBuffer[Joint]()
val jointsByName = new HashMap[String, Joint]() // convenience only
var rootJoint:Option[Joint] = None
}
class Joint {
var name:String = "unnamed joint :-("
val relativeTranslation = new Vector3d() // relative to parent
val absoluteTranslation = new Vector3d() // absolute (relative to model root)
// I use orientations so I can bring skeleton's created/posed in different styles on to
// a common set of orientations (hands by sides), so I can apply one animation to various skeletons.
val relativeOrientation = new Quaternion4d()
var absoluteOrientation = new Quaternion4d()
var children = new ArrayBuffer[Joint](0)
}
class PosedSkeleton(val skeleton:Skeleton) {
val posedJoints = new ArrayBuffer[PosedJoint]()
val posedJointsByName = new HashMap[String, PosedJoint]() // again, convenience only
var rootPosedJoint:Option[PosedJoint] = None
var rootTranslation = new Vector3d() // I use this for offsetting the skeleton from the models' normal position
def computeTransformationMatrices() {
computeTransformationMatrixForJoint(rootPosedJoint.get, Matrix4d.Identity)
}
def computeTransformationMatrixForJoint(posedJoint:PosedJoint, baseTransform: Mat4d) {
val inverseTranslate = Matrix4d.translate(posedJoint.joint.absoluteTranslation * -1)
val orientation = Matrix4d.rotationFor(posedJoint.joint.relativeOrientation))
val rotation = Matrix4d.rotationFor(posedJoint.relativeRotation)
val translate = Matrix4d.translate(posedJoint.joint.absoluteTranslation)
posedJoint.transform = inverseTranslate * rotation * orientation * translate * baseTransform
posedJoint.children.foreach(pj => computeTransformationMatrixForJoint(pj, posedJoint.transform))
}
}
class PosedJoint(val joint:Joint) {
var relativeRotation = new Quaternion4d()
val children = new ArrayBuffer[PosedJoint]()
var transform = new Matrix4d() // used as a cache for the posed joint's full transform matrix (useful for rendering)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment