13.2 C
New York
Friday, May 23, 2025

Two -point alignment transformations


I’m attempting to find out the transformation steps (place, scale, rotation) essential to use to an object for 2 factors, regionally positioned in that object, align with two world house positions.

The configuration is at the moment:

  • E is a prefabrication with two reworked kids (a, b)
  • C and D transformations are additionally inside a prefabrication (F)
  • We’ve got a worldwide place and rotation that can instantize
  • Earlier than any occasion, calculate the transformation that should happen to finish up within the appropriate place.

The answer I’ve at the moment is shut, however the rotation by no means appears to align completely, affecting the positioning.

public static (Vector3, Quaternion, Vector3) GetChildPosLeg(
    Rework topReference, // A
    Rework bottomReference, // B
    Rework topTarget, // C
    Rework bottomTarget, // D
    Vector3 scale // Scale of root instantiated object
    )
{
    // World topTarget
    Vector3 topTargetPos = (Vector3.Scale(topTarget.place, scale));
    Vector3 bottomTargetPos = (Vector3.Scale(bottomTarget.place, scale));

    // Scale
    float refDist = Vector3.Distance(topReference.place, bottomReference.place);
    float targetDist = Vector3.Distance(topTargetPos, bottomTargetPos);

    float scaleRatio = targetDist / refDist;
    Vector3 finalScale = new Vector3(0.5f, 1f, 0.5f) * scaleRatio;

    // Rotation
    Vector3 targetDir = topTarget.place - bottomTarget.place;
    Vector3 refDir =  bottomReference.localPosition - topReference.localPosition;

    Quaternion objectRotation = Quaternion.FromToRotation(refDir, targetDir);

    // Place
    Vector3 childNodePos = Vector3.Scale(topReference.localPosition, finalScale);
    childNodePos = objectRotation * childNodePos;
    Vector3 finalPosition = topTargetPos + childNodePos;

    return (finalPosition, objectRotation, finalScale);
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles