Difference between revisions of "User:DavidJCobb/Rotation Library"

Jump to navigation Jump to search
Major fixes
imported>DavidJCobb
m
imported>DavidJCobb
(Major fixes)
Line 8: Line 8:


== Changelog ==
== Changelog ==
;5/5/2015
:Fixed a severe bug stemming from an incorrect attempt at simplifying Chesko's code. The relevant changes may also yield minor performance improvements.
;2/11/2015
;2/11/2015
:Updated it based on my private copy. Fewer cross-script function calls. More options for using the library. Should work and be backward-compatible, if I pasted the right stuff into the right places.
:Updated it based on my private copy. Fewer cross-script function calls. More options for using the library. Should work and be backward-compatible, if I pasted the right stuff into the right places.
Line 348: Line 350:
   qOut[2] = v[2]
   qOut[2] = v[2]
   return qOut
   return qOut
EndFunction
Float[] Function MatrixMultiplyByColumn(Float[] amMatrix, Float[] avColumn) Global
{Multiplies a matrix by a column vector, and returns the resulting column vector.}
  Float[] vResult = new Float[3]
  vResult[0] = amMatrix[0]*avColumn[0] + amMatrix[1]*avColumn[1] + amMatrix[2]*avColumn[2]
  vResult[1] = amMatrix[3]*avColumn[0] + amMatrix[4]*avColumn[1] + amMatrix[5]*avColumn[2]
  vResult[2] = amMatrix[6]*avColumn[0] + amMatrix[7]*avColumn[1] + amMatrix[8]*avColumn[2]
  Return vResult
EndFunction
EndFunction


Line 353: Line 364:
{Given two sets of positions and rotations -- those of a parent object, and those of a child object relative to the parent -- this function returns an array of the form [XPos, YPos, ZPos, XAng, YAng, ZAng]. These are the positions and rotations of the child object relative to the world. In other words, this function exists as an alternative to MoveObjectRelativeToObject, allowing you to move objects however you wish.
{Given two sets of positions and rotations -- those of a parent object, and those of a child object relative to the parent -- this function returns an array of the form [XPos, YPos, ZPos, XAng, YAng, ZAng]. These are the positions and rotations of the child object relative to the world. In other words, this function exists as an alternative to MoveObjectRelativeToObject, allowing you to move objects however you wish.


Position code is based on GetPosXYZRotateAroundRef, a function authored by Chesko that can be found on the Creation Kit wiki.}
Position code was inspired by GetPosXYZRotateAroundRef, a function authored by Chesko that can be found on the Creation Kit wiki.}
  ;
  ; CONSTRUCT POSITION.
   ;
   ;
   ; CONSTRUCT POSITION USING CHESKO'S METHOD:
   ; Child world position = parent rotation as matrix * child parent-relative position
   ;
   ;
   Float[] fOutput = new Float[6]
   Float[] fOutput = new Float[6]
   Float[] fVector = new Float[3]
   Float[] fVector = new Float[3]
   ;
   Float[] mParentRotation = EulerToMatrix(afParentRotation[0], afParentRotation[1], afParentRotation[2])
  ; Apply Z-axis rotation matrix. (Modify the final X- and Y-axis positions based on the Z rotation.)
   Float[] vChildPosition = MatrixMultiplyByColumn(mParentRotation, afOffsetPosition)
  ;
   vChildPosition[0] = vChildPosition[0] + afParentPosition[0]
  fOutput[0] = (afOffsetPosition[0] * Math.cos(-afParentRotation[2])) + (afOffsetPosition[1] * Math.sin( afParentRotation[2]))
   vChildPosition[1] = vChildPosition[1] + afParentPosition[1]
   fOutput[1] = (afOffsetPosition[0] * Math.sin(-afParentRotation[2])) + (afOffsetPosition[1] * Math.cos(-afParentRotation[2]))
   vChildPosition[2] = vChildPosition[2] + afParentPosition[2]
  fOutput[2] = afOffsetPosition[2]
   fOutput[0] = vChildPosition[0]
  ;
   fOutput[1] = vChildPosition[1]
  ; Apply Y-axis rotation matrix. (Modify the final X- and Z-axis positions based on the Y rotation.)
   fOutput[2] = vChildPosition[2]
   ;
  fVector[0] = fOutput[0] ; X
  fVector[2] = fOutput[2] ; Z
  fOutput[0] = (fVector[0] * Math.cos(-afParentRotation[1])) + (fVector[2] * Math.sin(-afParentRotation[1]))
  fOutput[2] = (fVector[0] * Math.sin( afParentRotation[1])) + (fVector[2] * Math.cos(-afParentRotation[1]))
  ;
  ; Apply X-axis rotation matrix. (Modify the final Y- and Z-axis positions based on the X rotation.)
  ;
  fVector[1] = fOutput[1] ; Y
  fVector[2] = fOutput[2] ; Z
   fOutput[1] = (fVector[1] * Math.cos(-afParentRotation[1])) + (fVector[2] * Math.sin( afParentRotation[1]))
   fOutput[2] = (fVector[1] * Math.sin(-afParentRotation[1])) + (fVector[2] * Math.cos(-afParentRotation[1]))
  ;
  ; Finalize coordinates.
  ;
   fOutput[0] = fOutput[0] + afParentPosition[0]
   fOutput[1] = fOutput[1] + afParentPosition[1]
   fOutput[2] = fOutput[2] + afParentPosition[2]
   ;
   ;
   ; CONSTRUCT ROTATION USING THIS LIBRARY:
   ; CONSTRUCT ROTATION USING THIS LIBRARY:
Anonymous user

Navigation menu