0

I got four corner points of the object which are topLeft, topRight, BottomRight, BottomLeft and are as clockwise. I want a rotation values in X, Y and Z for unity object to rotate based on the rotation of these four points. Actually, these points are coming from python and I want to represent rotation of it on unity object which has Rotation x,y and z

  • 2
    These corner points are expressed in 3D space, or are they 2D points in screen space (or calculated with respect to a camera view)? Are the corners known exactly, or is there some error/jitter we need to smooth out? – DMGregory Feb 07 '24 at 11:18
  • This: https://gamedev.stackexchange.com/a/138035/10408 ? – Theraot Feb 07 '24 at 11:33
  • this is some insanely advanced algebra, I tried writing an answer but I've given up as it was taking hours. you can detect the movement but you need to understand a ton of advanced functions. Theres surely a better way. Why do you need to do this, it seems utterly useless? – Pow Feb 08 '24 at 13:21
  • @DMGregory These three points are coming from realsense using stereoCamera and it is accurate as I have checked it by myself. it is in x y and z. these points are coming based on stereoScopic camera – Yagnik Joshi Feb 12 '24 at 05:29
  • @Pow Exactly, Well, I found a way to actually do it. I chose to take those coordinates and pass it to Unity Script using Socket and then calculate there.

    p.s. this is more easier way to do it. as Python is not pretty complicated for 3d stuff in my opinion.

    – Yagnik Joshi Feb 12 '24 at 05:33
  • I too, also found an equation to do it, but it was very complicated – Pow Feb 12 '24 at 10:26

1 Answers1

0

If the object is a square, you can do this:

// Find perpendicular vectors running sideways...
Vector3 right = TopRight + BottomRight - TopLeft - BottomLeft;

//...and vertically along the object's edges. Vector3 up = TopLeft + TopRight - BottomLeft - BottomRight;

// Use a little vector math to get the remaining // perpendicular axis, forming a basis. Vector3 forward = Vector3.Cross(right, up);

// Extract the orientation of that coordinate basis. Quaternion rotation = Quaternion.LookRotation(forward, up);

Then you can use the quaternion directly (good if you need to interpolate/blend the rotation) or extract rotation.eulerAngles if you need the output as angles (just note these are not as nicely behaved and can have unintuitive and large value discontinuities for small orientation differences, even though they still express the same orientation as the source quaternion)

DMGregory
  • 134,153
  • 22
  • 242
  • 357