Search Unity

Quaternion.LookRotation with just euler angles

Discussion in 'Scripting' started by DragonCoder, Jan 15, 2022.

  1. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    Hello community,

    am converting a swarming\flocking algorithm to highly optimized burst-code. Optimization is important here since it's performed on thousands of particles.

    What I'm struggling with is, optimizing this code where pr is a particle struct (not using jobs right away before I haven't converted the code to use the Mathematics library since this makes debugging easier):

    Code (CSharp):
    1. Quaternion rotationCur = Quaternion.LookRotation(towardsVec);
    2. pr.rotation3D = rotationCur.eulerAngles;
    It seems highly inefficient to use Quaternions here only to convert back to eulers.

    Especially since when using "quaternion" from Mathematics (which is highly recommended when using the burst compiler, right?), I am forced to compute the euler angles in code, since there is no implementation in the library (there's a whole thread where the devs argue that's not something needed q_q).

    Does someone know how to compute what LookRotation(vec, up) does, directly into euler angles?

    Also on a sidenote,
    why is this despite Quat->quat conversion:
    Code (CSharp):
    1. quaternion rotationCur = Quaternion.LookRotation(towardsVec);
    WAY faster than this quat-only solution (at least without burst compilation about 3 times faster)?
    Code (CSharp):
    1. quaternion rotationCur = quaternion.LookRotation(towardsVec, math.up());
    Huge thanks in advance!
     
    Last edited: Jan 15, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    There was a DOTS flocking demo some years back, I remember seeing it in a GDC video.

    See if you can perhaps find the code for that and see what they did to get the boids pointed properly.