Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.