Rotating objects in space
-
The following code rotates an object on its origin by pointing its zaxis towards a point on the model.
sel_trans = sel_comp.transformation sel_origin = sel_trans.origin new_vector = sel_origin.vector_to point_here new_trans = Geom::Transformation.new(sel_origin, new_vector) sel_comp.transformation = new_trans
Problem is that the
new_tran
('s)xaxis
, andyaxis
are arbitrary set byGeom::Transformation.new
. If the object is not symmetrical, the rotation can be unexpected (pointing z is OK). Any suggestions? -
Well, its hard to define what the absolute "right" way is to rotate something in 3d.
I'm guessing what you would like to see is 2 part process. First rotate on the x axis and apply the transformation, then rotate on the y axis. That might give you what seems to be a more logical result.
How to do that is beyond me at the moment - you would have to figure out just how far to rotate on x so that when you rotate on the y the object is poting exactly to the desired target. Maybe its easier to do than I can think of right now. Hopefully your brain will think it through faster than mine.
Chris
-
Sigh.....I was hoping it was just a method I missed. Thanks, guess I'll get back to this one later.
-
Use a .cross method on the zaxis and new_vector to get a vector perpendicular to them - you can then use that as a normal for a rotation transformation using the angle between the zaxis and the new_vector...
What exactly are you trying to do ??
-
Hi Tig, The first Ruby I ever wrote (some time last year) was recently posted here. I did so before realizing that you did "Free Rotate". Still, I would like to learn, and fix my code. I watched the demo video, and didn't open yours assuming that it was to sophisticated for me to understand, and just posted my question here.
-
When dealing with rotations in 3D I think it's a common misconception to consider the "direct" rotation as the only rotation since in 2D there is only ever one rotation to move a vector to point towards another. Say for example you wanted to rotate the vector (1,0,0) to the vector (0,1,0). I think most people would envision the rotation as holding the z-axis fixed the path traced by the vector as being a quarter circle in the x-y plane. However there are infinitely many rotations that will take one vector to the other!
For a simple one, consider looking down at the origin from the point (0.5,0.5,0). From here, you can rotate 180 degrees around the (0.5,0.5,0) vector and it will achieve the same result of moving (1,0,0) to (0,1,0). Another somewhat harder rotation to see is rotating by 120 degrees around the vector (1,1,1) which is equivalent to moving each axis to another axis. In each of these cases the z-axis behaves differently under the rotation so this is where the complication in your original code stems from.
If the behavior your looking for is the direct rotation than the suggestion given by TIG is the correct one, and should provide the expected behavior.
-
Well, better late then never. Not as difficult as I thought. First point, then rotate:
` comp.transformation=Geom::Transformation.new(comp.transformation.origin,point_vector)
comp.transform! Geom::Transformation.new(comp.transformation.origin,comp.transformation.zaxis, (Math::PI*3)/2)`
Thanks everyone.
Advertisement