Spatial functions and snippets: Difference between revisions

Jump to navigation Jump to search
m (Convert source tags to syntaxhighlight)
Line 90: Line 90:
* fAngleX/Y/Z define the angles for the rotation around the origin.
* fAngleX/Y/Z define the angles for the rotation around the origin.


== Function to find angles from one reference to another (ie angles to face) ==
<syntaxhighlight lang="papyrus">;Skyrim uses right hand coordinate system (X=Eastings, Y=Northings - and up!)
;Skyrim uses left handed XYZ global angles
;Skyrim undergoes ZYX angle calculations (ie in that order)
;Roll not supported!
;Assumes mesh in nif is 'facing' the 'Y' axis
float[] function getAnglesToFaceRef(ObjectReference origin, ObjectReference target)
float deltaX=target.GetPositionX()-origin.GetPositionX()
float deltaY=target.GetPositionY()-origin.GetPositionY()
float deltaZ=target.GetPositionZ()-origin.GetPositionZ()
float localAngleX=atan2(-deltaZ, math.sqrt((deltaX*deltaX)+(deltaY*deltaY))) ; pitch
float localAngleY=0 ; roll - keeping this as zero, other values will break this
float localAngleZ=RPDefault_Math.atan2(deltaX, deltaY) ; yaw
float globalAngleX=localAngleX*Math.Cos(localAngleZ)+localAngleY*Math.Sin(localAngleZ)
float globalAngleY=localAngleY*Math.Cos(localAngleZ)-localAngleX*Math.Sin(localAngleZ)
float globalAngleZ=localAngleZ ; same as local
float[] toReturn=new float[3]
toReturn[0]=globalAngleX
toReturn[1]=globalAngleY
toReturn[2]=globalAngleZ
endfunction
</syntaxhighlight>
== See also ==
== See also ==
*[[Unit|Spatial units]]
*[[Unit|Spatial units]]


[[Category:Papyrus Tutorials]]
[[Category:Papyrus Tutorials]]
2

edits

Navigation menu