Skip to content

Rotate

Orient and rotate

Basic explanations on quaternions

cgwiki/rotations

voxel-pixel/orient

vfxbrain rotations

Make orient from two orthogonal vectors

vex
// tangentu or up = {0, 0, 1},
// usa a polyframe
vector4 orient = quaternion(maketransform(v@tangentu, @N));
p@orient = orient;

Orient from normal, create frame, postrot

vex
//run over points
vector tan_u = cross(v@N, {0,1,0});
vector tan_v = cross(tan_u, v@N);
v@tangentu = tan_u;
v@tangentv = tan_v;
vector4 orient = quaternion(maketransform(v@N, tan_v));
vector4 extrarot = quaternion(angle, {1,0,0});
orient = qmultiply(orient, extrarot);
p@orient = orient;

Rotate a vector around an axis

Rotating Normal

vex
//enforce prototypes
float amount = chf("rot_amount");
vector @P;
vector @N;
vector axis = cross(@N, {0,1,0});
vector normal = normalize(@N);
matrix mat = instance(@P, normal);
rotate(mat, amount ,axis);
@N = @N*mat;

Rotate point orient by angle

Tropical/Recipes/ Recipe: Rotate Around Pivot

input0: points scatter over a plane, points have a normal sampled from the plane the plane has a mountain node, so it is uneven

vex
// run over points
vector right = {1,0,0}; //create a temporary right vector
// second quaterion to rotate the right vector around the axis(normal)
vector axis = @N;
float angle = radians(chf("angle"));
vector4 q2 = quaternion(angle, axis);
right = qrotate(q2, right);//rotate the right vector by quaternion, optional
vector up = cross(@N, right);
//cross will align the rotation Y axis to the normal
vector4 orient = quaternion(maketransform(up, v@N));
p@orient = orient;

Rotate normal with a limit angle

This is for a plane XZ plane points in a COPs SOP

vex
// run over points
#include <math.h>
float rot_angle = radians(chf("angle"));
vector forward = {0,0,1}; // towards camera
vector local_right = cross(forward, v@N);
vector local_up = cross(@N, local_right);
v@local_right = local_right;
v@local_up = local_up;
float dot = dot(@N, forward);
float len = length(@N) * length(forward);
float angle = acos(dot/len);
float max_angle = PI_2 - angle;
float sel_angle = rot_angle;
if (rot_angle > max_angle){
sel_angle = max_angle;
}
vector4 q2 = quaternion(sel_angle, local_right);
@N = qrotate(q2, @N);

Coolkids operator

Rotate a point in place, using a matrix

vex
// run over points
float angle = ch('angle');
matrix3 rotm = ident();
vector axis = {0,1,0};
vector pivot = {0,0,0};
rotate(rotm, angle, axis);
vector4 orient;
vector scale, postrot;
matrix xform;
scale = 1;
postrot = 0;
vector normal = @N;
orient = quaternion(rotm);
xform = instance(pivot, normal, scale, postrot, orient, pivot);
@P *= xform;