Jump to content

Photo

[Resource] Rotate ObjectReference around another ObjectReference

Papyrus Rotation

  • Please log in to reply
9 replies to this topic

#1
Chesko

Chesko
  • Members
  • PipPipPip
  • Diviner
  • Joined: 13-July 10
  • 2634 posts
You may be in a situation where you need to be able to precisely rotate one ObjectReference around another ObjectReference. This proved to be somewhat challenging if you are not familiar with rotation matricies (like me).

This function takes in an akOrigin (the ObjectReference to act as the center) and an akObject (the ObjectReference you want to be rotated). Note that this function returns a float[3] array; it does not perform the actual rotation. It returns a new set of X, Y, Z worldspace coordinates in the [0], [1], and [2] elements of the array that correspond to the coordinates your reference should be located at, once it's been rotated by the degrees you specified in the function call. You will need to create a new array to accept the return of this function. Once you have the coordinates returned by this function, you can call whatever movement function you like at that point (most likely SplineTranslateTo) using whatever parameters you choose.

Enjoy. I've tested it and it works.

float[] function GetPosXYZRotateAroundRef(ObjectReference akOrigin, ObjectReference akObject, float fAngleX, float fAngleY, float fAngleZ)

Rotates a point (coordinates of akObject) offset from the center of rotation (akOrigin) by the supplied degrees fAngleX, fAngleY, fAngleZ, and returns the new desired position of the point.
Spoiler


Example:

float[] myNewPos = new float[3]
myNewPos = GetPosXYZRotateAroundRef(PlayerRef, myCabbage, 25.0, 0.0, 0.0)   ;Find out where myCabbage would be if rotated 25.0 degrees on the X axis around the Player.
myCabbage.SplineTranslateTo(myNewPos[0], myNewPos[1], myNewPos[2], 0.0, 0.0, 0.0, 1.0, 200.0)  ;Move the cabbage to the new rotated position

Edited by Chesko, 07 October 2012 - 01:54 PM.


#2
Chesko

Chesko
  • Members
  • PipPipPip
  • Diviner
  • Joined: 13-July 10
  • 2634 posts
And as usual, the forums jacked up my formatting.

If you'd like to see a more properly formatted version, click here.

Edited by Chesko, 07 October 2012 - 12:30 AM.


#3
seventyfour

seventyfour
  • Members
  • Pip
  • Curate
  • Joined: 16-January 12
  • 667 posts
Thank you so much, I'm going to need to do this soon. The fact that you set this up will probably save a lot of time!

#4
Chesko

Chesko
  • Members
  • PipPipPip
  • Diviner
  • Joined: 13-July 10
  • 2634 posts
No problem. :drag:

#5
Chesko

Chesko
  • Members
  • PipPipPip
  • Diviner
  • Joined: 13-July 10
  • 2634 posts
Minor update. Made a change that inverts the Z angle before computation, to account for the game's interpretation of z-rotation (it's backwards). I can't edit the pastebin post, so make sure you get your code from the post up top. If you want to add the change yourself, just change:


;R * v = Rv, where R = rotation matrix, v = column vector of point [ x y z ], Rv = column vector of point after rotation

to

;R * v = Rv, where R = rotation matrix, v = column vector of point [ x y z ], Rv = column vector of point after rotation

fAngleZ = -(fAngleZ)


Edited by Chesko, 07 October 2012 - 01:56 PM.


#6
JustinOther

JustinOther
  • Members
  • PipPipPipPip
  • Master
  • Joined: 24-September 07
  • 8391 posts
  • Location:Elsweyr "Holding Cell" [CELL:000B9BC6]
Would not...
fNewX = (fVectorX * cos(fAngleZ)) + (fVectorY * sin(-fAngleZ))
...work just as well as...
fNewX = (fVectorX * cos(fAngleZ)) + (fVectorY * sin(-fAngleZ)) + (fVectorZ * 0)
...since '(fVectorZ * 0)' will always equal 0 regardless of fVectorZ's value (and other such instances)?

Forum formatting: You can retain formatting, but you have to turn off the WYSIWYG editor with the light switch looking thingy in the upper left when posting/editing.
Spoiler
Code will lose its indentation if it's copied out of a post, but not if you quote to view it with that switch toggled to off. Also, if you want to kill the pink, find the '/" that starts a string and close it with another '/".

Very cool function! Thanks for sharing, Chesko :)

Edited by JustinOther, 07 October 2012 - 04:51 PM.


#7
seventyfour

seventyfour
  • Members
  • Pip
  • Curate
  • Joined: 16-January 12
  • 667 posts
Another comment (not meant to be a "pun" but okay...) about the code tags... they recognize // as a comment, not ;.

So you can kind of fake the comment highlighting if you do this:

;// I am a comment

Hopefully they fix that eventually. I don't get why they didn't just use // as the comments for papyrus though. I guess they wanted to be different.

Edited by seventyfour, 07 October 2012 - 03:34 PM.


#8
Chesko

Chesko
  • Members
  • PipPipPip
  • Diviner
  • Joined: 13-July 10
  • 2634 posts
JustinOther - yes, there are several parts of the matrix multiplication that could be condensed. I left it verbose such that I could debug it in case something came up, and for algebraic clarity. And I don't think the additional time cost means very much in this case. And thanks for reposting the well-formatted version. (Make sure to add the line that inverses the z-angle!)

seventyfour - Thanks.

Edited by Chesko, 07 October 2012 - 04:43 PM.


#9
SmkViper

SmkViper
  • Bethesda Game Studios
  • Programmer
  • Joined: 04-April 06
  • 350 posts
Small note - you shouldn't have to allocate the array before calling the function, since the function allocates a new one anyway and returns it (the one you allocate before calling the function gets garbage collected).

#10
Chesko

Chesko
  • Members
  • PipPipPip
  • Diviner
  • Joined: 13-July 10
  • 2634 posts
Thanks again, as always, Viper :biggrin: I'll make the change soon.


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users