Jump to content
Sign in to follow this  
Gabor64738

Rotate a cube to align with a 3D line

Recommended Posts

I am trying to rotate a 3D object (StrokeCube) to align its Y axis with a line segment defined by two points (two Spheres). Is there an easy way to do this using say CreateLookAtRH, or similar? The Help and https://docwiki.embarcadero.com/ does not describe this and similar methods and I cannot find any example code. Having searched and read for a few days/nights, I've gone down the scary path of rotation matrices etc.

 

This is the process I have so far:

  • Create Vector1 as the AbsoluteUp (TVector3D) of the StrokeCube.
  • Create Vector2 that runs from Sphere1 to Sphere2 (subtracting them), also TVector3D.
  • Normalise the two vectors.
  • Create RotationAxis by calculating their cross product (RotationAxis perpendicular to both).
  • Normalise RotationAxis.
  • Calculate Angle between Vector1 and Vector2 as arccos of their dot product divided by (Vector1Length*Vector2Length).
  • R := mat.CreateRotation() with the normalised RotationAxis and the Angle.
  • Getting the X Y Z rotations for the StrokeCube out of R does not give correct results. I've tried many variants, e.g. https://learnopencv.com/rotation-matrix-to-euler-angles/ and https://uk.mathworks.com/matlabcentral/answers/493771-euler-3d-rotation-between-two-vectors.

 

My questions:

  • Is there an easy way to align a 3D object (StrokeCube) to a line defined by two points?
  • If not, then is my processing correct so far?
  • What matches the X Y Z rotations of an FMX 3D object? One of the Euler sequences, Quaternions, or something else? How do I extract the necessary rotations from the rotation matrix?

 

Thanks for your help.

 

Update: I've tried these but they don't rotate the Segment (StrokeCube):

Segment.AbsoluteMatrix.CreateRotation(Vector3DP1P2n, Angle);
Segment.AbsoluteMatrix.CreateLookAtRH(TPoint3D.Create(M1.Position.X,M1.Position.Y,M1.Position.Z), TPoint3D.Create(M2.Position.X,M2.Position.Y,M2.Position.Z), TPoint3D.Create(0,0,0));
Segment.LocalMatrix.CreateRotation(Vector3DP1P2n, Angle);
Segment.LocalMatrix.CreateLookAtRH(TPoint3D.Create(M1.Position.X, M1.Position.Y, M1.Position.Z), TPoint3D.Create(M2.Position.X,M2.Position.Y,M2.Position.Z), TPoint3D.Create(0,0,0));

 

Edited by Gabor64738

Share this post


Link to post

The CreateXX are class functions. Therefore they don't change LocalMatrix itself, but return a new matrix instead.

 

I'm not sure if I understand you right, but it might be sufficient to set the RotationAngle of the StrokeCube according to the vector V between those two spheres. The Y value should get the angle (in degrees) in the X/Z plane, while the Z value should get the inclination angle to that plane (not tested).

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×