Search code examples
mathrotationpositionunreal-engine4unreal-blueprint

(Unreal 4.8.1) How do you calculate the x y and z of the player controller when rotating around a point a certain distance away


I have 4 arrows on my interface. One arrow points in one direction: up, down, left, right. When you select an arrow I want the player controller to rotate in that direction around a point while facing toward that point. I have kind of gotten lost in the logic of transforms, vectors and rotators (pitch, yaw, roll). Can anyone please help me out with this logic problem? I am working in blueprint.


Solution

  • As you are mixing the transformations in a non-commutative way, it's better to repeat the calculation every time you need it.

    Requires 5 stages, a distance and two angles. A picture to clarify:

    enter image description here

    1. Translate your player in the +ve Z-direction by the distance you want it to be away from the point
    2. Make it face the -ve Z-direction (0, 0, -1)
    3. Rotate it around the x-axis by θ (in a clockwise sense when you look down the axis) - matrix P
    4. Rotate it around the z-axis by ϕ (likewise) - matrix Q
    5. Translate it by the displacement from the origin to the point (so, the point expressed as its position vector) - matrix T

    The matrices you need for P and Q: https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations

    I assume you know the translation matrix :)

    Keep track of each angle and change them when the arrows keys are pressed. Also the final matrix you need is TPQ (multiply in this order); re-calculate it every time you need it - don't worry about the processing power needed.