Skip to content

Instantly share code, notes, and snippets.

@kumavis
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumavis/57144f2d72f46d1cac4b to your computer and use it in GitHub Desktop.
Save kumavis/57144f2d72f46d1cac4b to your computer and use it in GitHub Desktop.
// Utility - changes the parent but preserves global position + rotation
function orientUnderParent( target, parent ) {
// calculate new pos
var newPos = new THREE.Vector3()
newPos.setFromMatrixPosition( target.matrixWorld )
parent.worldToLocal( newPos )
target.position = newPos
// calculate new rot
var newRot = new THREE.Quaternion()
newRot.setFromRotationMatrix( target.matrixWorld )
newRot.multiply( parent.quaternion.clone().inverse() )
target.quaternion.copy( newRot )
// attach to parent
parent.add( target )
}
@kumavis
Copy link
Author

kumavis commented Jun 22, 2014

revised:

module.exports = orientUnderParent

// Utility - changes the parent but preserves global position + rotation
function orientUnderParent( target, parent ) {

  // calculate new pos
  var newPos = new THREE.Vector3()
  newPos.setFromMatrixPosition( target.matrixWorld )
  parent.worldToLocal( newPos )
  target.position = newPos

  // calculate new rot
  var newRot = new THREE.Quaternion()
  newRot.setFromRotationMatrix( target.matrixWorld )
  var parentRot = new THREE.Quaternion()
  parentRot.setFromRotationMatrix( parent.matrixWorld )
  newRot.multiply( parentRot.inverse() )
  target.quaternion.copy( newRot )

  // attach to parent
  parent.add( target )

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment