I have created a 1 sq.ft face along the XY plane using the eye of a camera as its centroid:
model = Sketchup.active_model entities = model.active_entities view = model.active_view cam = view.camera eye = cam.eye target = cam.target pts = [ Geom::Point3d.new(eye - 0.5.feet, eye - 0.5.feet, eye), Geom::Point3d.new(eye + 0.5.feet, eye - 0.5.feet, eye), Geom::Point3d.new(eye + 0.5.feet, eye + 0.5.feet, eye), Geom::Point3d.new(eye - 0.5.feet, eye + 0.5.feet, eye) ] gr = entities.add_group square = gr.entities.add_face ( pts )
I want the square to be perpendicular to the eye-target vector, i.e., rotating the square so the eye-target vector is a normal to its plane. I’ve tried the following geometry equations with no success:
n = [0, 0, 1] # Current normal vector to the square v = [target - eye, target - eye, target - eye] # eye-target vector axis = [v, -v, 0] # Axis of rotation given by vectorial product (v x n) magnitude = Math.sqrt( axis**2 + axis**2 + axis**2 ) # Magnitude of axis u = [axis/magnitude , axis/magnitude , axis/magnitude ] # Normalized axis theta = -Math.acos( v / Math.sqrt(v**2 + v**2 + v**2) ) # Angle of rotation given by scalar product (v · n)
The current normal vector to the square is n = [0,0,1] since it is generated along the XY plane. To calculate the new axis of rotation, a vectorial product (v x n) is needed, and then the axis is normalized. In the same way, I calculate the angle of rotation theta by the scalar product (v · n).
Having the point, the axis, and the angle, I can set the transformation:
tr = Geom::Transformation.rotation( eye, u, theta ) gr.transform!(tr)
But the result is not the expected. Could you help me to localize my mistake? Any other idea to solve the issue will be very welcomed too.