Skip to content

Instantly share code, notes, and snippets.

@nickjs
Last active August 29, 2015 13:57
Show Gist options
  • Save nickjs/9844075 to your computer and use it in GitHub Desktop.
Save nickjs/9844075 to your computer and use it in GitHub Desktop.
parallel transport frames wtf
totalLength = Math.ceil(@model.spline.getLength())
up = LW.UP.clone()
rolledUp = up.clone()
binormal = new THREE.Vector3
bank = lastBank = 0
for i in [0..totalLength]
u = i / totalLength
# the center point of the spline at this u, where the center of the rails should be
position = @model.spline.getPointAt(u)
# forward direction the spline is moving
tangent = @model.spline.getTangentAt(u).normalize()
# side, perpindicular to forward and up
# we calculate it based on the current tangent and the last up vector
binormal.crossVectors(tangent, up).normalize()
# up, perpindicular to forward and binormal
# recalculate a new up based on this tangent
up.crossVectors(binormal, tangent).normalize()
# world absolute amount this particular up vector should be rolled along the tangent, radians
bank = THREE.Math.degToRad(@model.getBankAt(u))
# now we make another up vector, but this time rolled over tangent
rolledUp.copy(up).applyAxisAngle(tangent, bank - lastBank).normalize()
# and now we need to make a new binormal based on the new up
binormal.crossVectors(tangent, rolledUp).normalize()
# store the bank so we can use just the delta next time
lastBank = bank
# finally, we convert everything to a matrix we can multiply to any vector3 to apply this frame to that point
Rframe = new THREE.Matrix4(binormal.x, rolledUp.x, -tangent.x, 0,
binormal.y, rolledUp.y, -tangent.y, 0,
binormal.z, rolledUp.z, -tangent.z, 0
0, 0, 0, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment