Here is the function I wrote that I use in several places in a couple of my mods. It sets two global variables , myXOffset and myYOffset, to the X and Y positions relative to the player that the object needs to be placed such that it is directly in front of them. In this instance it moves the object 100 game units forward and 30 units to one side along the arc; these could have been input parameters but I left them as constants in this case.
Spoiler
function GetOffset() ;Trigonometry time! We need to position the campsite item such that it is dropped in front of the player. int myQuad = 1 float mySideC = 100.0 ;Hypotenuse (distance to move the object forward) float myAngleA = (Game.GetPlayer().GetAngleZ() + 30.0) float myAngleC = 90.0 ;Clamp to an angle less than 90 degrees. if myAngleA > 90.0 && myAngleA <= 180.0 myAngleA -= 90.0 myQuad = 2 elseif myAngleA > 180.0 && myAngleA <= 270.0 myAngleA -= 180.0 myQuad = 3 elseif myAngleA > 270.0 myAngleA -= 270.0 myQuad = 4 endif float mySideA float mySideB ;Determine the length of the legs of the triangle for moving the player. if myQuad == 1 || myQuad == 3 mySideA = math.sin(myAngleA) * mySideC ;Y offset mySideB = math.cos(myAngleA) * mySideC ;X offset else mySideA = math.cos(myAngleA) * mySideC ;Y offset mySideB = math.sin(myAngleA) * mySideC ;X offset endif ;Figure out which direction the offset should travel based on the quadrant. if myQuad == 1 ;Move farther into the 1st quadrant myXOffset = mySideA myYOffset = mySideB elseif myQuad == 2 ;Move farther into the 2nd quadrant myXOffset = mySideA myYOffset = mySideB * -1.0 elseif myQuad == 3 ;Move farther into the 3rd quadrant myXOffset = mySideA * -1.0 myYOffset = mySideB * -1.0 elseif myQuad == 4 ;Move farther into the 4th quadrant myXOffset = mySideA * -1.0 myYOffset = mySideB endifendFunction
It works flawlessly, but I've always thought that it was an inelegant solution to the problem. Has anyone else come up with a way of doing this that is shorter, simpler, sweeter? Thanks.