1
\$\begingroup\$

I'm creating a procedural aiming animation system in UE5 as a learning experiment.

In order to position the hand for aiming down sights, I'm using a forward vector from player camera to project out a point centered in the camera POV. I then account for the offset between the rear weapon sight socket attached to the weapon, and the hand bone to create the IK target for the hand shown as a pink circle. I believe I've done that correctly.

The real challenge I'm facing is with rotation. I'm struggling to procedurally rotate the hand bone in such a way that aligns front sight, rear sight, and camera along the green line shown in the pictures. The green line starts from center camera and ends at the rear sight.

I believe there's a way to construct a quaternion from two vectors using the dot product to rotate the hand bone and align the sights? The different spaces (world, hand, and rear weapon sight) are also adding to my confusion. Any input is greatly appreciated.ADS View 1 ADS View 2

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

construct a quaternion from two vectors

Yes it's quite easy if you understand what the quaternion elements are:

Quat QuatFromVectors(Vec3 from, Vec3 to)
{
    from = normalize(from);
    to = normalize(to);
    vec3 cross = crossProduct(from, to); 
    // cross product is the axis aroud which to rotate scaled by the sin of the angle
    float dot = dotProduct(from, to); 
    // dot product is the cos of the angle
    Quat temp = Quat(cross, dot+1); 
    //nlerp(identity, temp, 0.5) to get the half angle
    return normalize(temp);
}

Unreal 5 has this function already.

The from and to vectors should be in world space. That will let you apply it as the last rotation on the wrist.

However this might result in weird roll of the gun but you can adjust that by rotating around the look direction.

\$\endgroup\$
2
  • \$\begingroup\$ As for the inputs for the UE5 function, what vectors should I use for start and end? I have the hand bone, the socket attached to the hand that the weapon actually attaches to, and the rear sight socket on the weapon. \$\endgroup\$ Commented Sep 3 at 19:23
  • \$\begingroup\$ Thinking about that more, I believe the start vector would be the rear sight forward vector, and the end be the camera forward vector. Convert the resulting quat to a rotator and then pass that to transform bone. \$\endgroup\$ Commented Sep 3 at 19:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.