Rotate 3d shape in path

68 Views Asked by At

It seems very simple but I am not able to figure it out

let's say I have Plane (yellow) it needs be in border when moved with BLUE direction arrow enter image description here

I have value that needs to be moved for example user moves blue arrow

value to be moved = dotVector ( normalizedDirection , forward vector) * magnitude

Arrow to move object is rotated like so Z axis will be direction of movement , So this value will be in Z axis to move object

I have the center object + I have the radius of the circle ,

How can I Find X Value so it always be in circle ( I don't care about up direction)

Expected result - Yellow plane should follow the path where 3d Sphere are placed

Code to move the Model

 let zDepth = sceneView.projectPoint(selectedAxis.position).z
        let touch3D = SCNVector3(Float(touch2D.x), Float(touch2D.y), zDepth)
        let touchPosition = sceneView.unprojectPoint(touch3D)
        
        let touchDelta = touchPosition - lastTouchPosition!
        let distance = touchDelta.magnitude
        let direction = touchDelta.normalized
        let zTranslation = distance * SCNVector3.dotVector(a: selectedAxis.forward, b: direction)
        
        selectedAxis.localTranslate(by: SCNVector3(0, 0, zTranslation))

Solution :

What I did the vector where to move let's say A

find the angle using arc tan ( A.z, A.x) here y is 0 for all models so

from the answer of the Quarter Lemon

for radius 20

let xnext =  (cos(angle)) * 20 
let yNext = (sin(angle)) * 20

and now it will be move in circle only

enter image description here

enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

Let's say the circle of spheres is in the $xy$ plane. Let the radius of that circle be $r$. Let's also say that we have $N$ spheres, and that their centers are distributed evenly on the circumference of the circle. It would follow that the angular separation between two adjacent spheres is $\phi = \dfrac{2\pi}{N}$. Then assuming one of the centers lies on the $x$-axis, then the coordinates of $i$-th center will be

$ C_i = (r \cos\left( (i-1) \phi \right) , r \sin \left( (i-1) \phi \right) , 0 ) \hspace{20pt} i = 1, 2, ..., N$

Next, we have the yellow plane which passes through the center of the first sphere, and has a normal vector $U$ with an arbitrary orientation. And we have three vectors used for moving the yellow plane, and they are the unit vectors $V_B, V_R, V_G $ (Blue, Red, Green). These vectors can point in any direction as well, as long as they are perpendicular to each other, i.e. $V_G \perp V_B$ and $V_G \perp V_R $ and $V_B \perp V_R $. Define the matrix $R$ as follows

$R = \left[ V_B, V_R, V_G \right] $

Then $R$ is a rotation matrix that specifies the orientation of the various possible movements: Forward/Backward along $V_B$, Left/Right along $V_R$, Up/Down along $ V_G$.

Now suppose we want to move (translate) the yellow plane to the next sphere center, then the translation vector is given by

$ \Delta P = C_{i+1} - C_{i} = r ( \cos (i \phi) - \cos( (i-1) \phi) , \sin (i \phi) - \sin( (i-1) \phi), 0) $

To find the displacement vector $d = (d_B, d_R, d_G)$ we note that

$ \Delta P = R d $

Hence

$d = R^{-1} \Delta P = R^T \Delta P $

And this will give us all of the three required displacements.