Snap normal to nearest 45 degree angle in vex?

   2048   1   1
User Avatar
Member
6 posts
Joined: July 2018
Offline
Hey I'm pretty new to houdini and am trying to figure out what I think is probably a pretty basic setup.

If I have pointA and pointB and I calculate the normal from pointA to pointB as

vector posA = point(0, “P”, pointA);
vector posB = point(0, “P”, pointB);

vector dir = normalize(posA - posB);

this gives me the normal. how could I then snap that normal to the nearest 45 degree angle in x,y,z?

I hope what I'm asking makes sense. thanks
User Avatar
Member
4500 posts
Joined: Feb. 2012
Offline
Hi,

You can do something like this:

float getAngleBetweenVectors(vector v0; vector v1)
{
    vector v0n = normalize(v0);
    vector v1n = normalize(v1);
    return atan2(length(cross(v0n,v1n)), dot(v0n, v1n));
}

vector axes[]={{1,0,0},{0,1,0},{0,0,1}};

float ax = getAngleBetweenVectors(@N, axes[0]);
float ay = getAngleBetweenVectors(@N, axes[1]);
float az = getAngleBetweenVectors(@N, axes[2]);

float angles[] = array(ax, ay, az);

@N = axes[argsort(angles)[0]];
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com]

youtube.com/@pragmaticvfx | patreon.com/animatrix | animatrix2k7.gumroad.com
  • Quick Links