POV-Ray : Newsgroups : povray.advanced-users : Simple one : Re: Simple one Server Time
28 Jul 2024 10:29:00 EDT (-0400)
  Re: Simple one  
From: Sebastian H 
Date: 12 Mar 2006 06:24:59
Message: <4414050b$1@news.povray.org>
Orchid XP v2 wrote:
> OK, this is going to take somebody about 3 seconds to answer I would 
> think...
> 
> I have an object who's surface normal is +Y. I need to rotate it in such 
> a way that its surface normal becomes equal to a user-supplied vector. 
> How do I figure out the necessary angles?

You can rotate the +y vector around an other vector which
not neccessarily must be a basis vector (means x,y,z).

For this we need the angle between the old and the new
vector. It can be calculated with the following expression.

#declare old_vector = <0,1,0>;
#declare new_vector = <1234,1234,1234>;

#declare angle_cos = 
vdot(y,new_vector)/(vlength(old_vector)*vlength(new_vector));

#declare angle=acos(angle_cos);

Note that if old and new vector have a length of 1.0
the division by their length is not necessary in angle_cos.
This is because the dot product between two vector actually returns
vdot(v1,v2) = vlength(v1)*vlength(v2)*cos(angle_between_the_vectors)

Ok, now we have the angle. What now is needed is the axis
to rotate around. It can be determined by the following expression.

#declare rot_axis = vnormalize(vcross(old_vector, new_vector));

Which returns a vector perpendicular to both input vectors.
The vnormalize is required unless the input vectors have
a peculiar relation to each other which in general is not the case.

To rotate the old vector into the new one we now can
do this.

#declare new_vector_rotated = vaxis_rotate(old_vector, rot_axis, angle);

Now new_vector_rotated and new_vector should point into the same
direction. But new_vector_rotated will have the length of old_vector.

If an object must be rotated the macro
Axis_Rotate_Trans(rot_axis, angle) from transforms.inc can be used.

This way it is a bit harder compared to the one from Chris.
The problem with the rotation above is that it rotates
the y-vector into the correct direction but the orientation
of the x- and the z-vector after the rotation are somewhat arbitrary
(of course they're not but we did not specify them).
If you want to transform a whole basis system (e.g x,y,z) to
a new one, which would include specific x- and z-axis alignment
as well, you should consider to take a look at matrices.

Sebastian


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.