Q: Rotate primitive in VEX

   12345   5   3
User Avatar
Member
459 posts
Joined: Oct. 2011
Offline
Hi
In VEX, how do i rotate a primitive around one of it's edges? Let's say a have a triangle where none of the edges are aligned with the coordinate system.
How do i set the primitive's rotation axis?

Any help appreciated.

Cheers
Bosak
http://www.racecar.no [www.racecar.no]
User Avatar
Member
1737 posts
Joined: May 2006
Offline
There might be an easier way than this, but this works, and its handy to know how to use these sort of manipulations in other contexts anyway.

Say you want to rotate around the first edge. We can calculate the midpoint of the edge by getting the 2 points that create that edge, adding, then divide by 2. The axis of rotation is given by subtracting one point from the other.

You use the axis with a rotate() call, and use it to build an orient quaternion. You can then create an instance transform using that orient, and the midpoint of the edge as the pivot.

Attachments:
rotate_prims_around_edge_v01.hipnc (272.5 KB)

http://www.tokeru.com/cgwiki [www.tokeru.com]
https://www.patreon.com/mattestela [www.patreon.com]
User Avatar
Member
459 posts
Joined: Oct. 2011
Offline
Thank you very much!

-b
http://www.racecar.no [www.racecar.no]
User Avatar
Member
339 posts
Joined: Aug. 2007
Offline
Ooh, I like that primpoints() function, good method! You can also minimize the complexity of the rotation by directly multing the matrix and applying the pivot yourself:

float angle;
matrix3 rotm;

// first calculate rotation around the axis, convert to orient
angle = ch('angle');
angle += (@primnum/float(@numprim))*ch('waveoffset');

rotm = ident();
rotate(rotm, angle, v@axis);

//move to origin, apply rotation, move back
@P -= v@pivot;
@P *= rotm;
@P += v@pivot;
Jesse Erickson
Fx Animator
WDAS
User Avatar
Member
1737 posts
Joined: May 2006
Offline
Ah nice. I tried (and failed) to read the physicaly based rendering book, some chapter specifically mentioned the move-to-origin/rotate/restore trick as being a terrible idea, and the cool kids rotate in place. I then totally failed to understand the maths behind rotate-in-place, but the sense of shame of doign it the intuitive way stuck with me.

The lesson here is to read less books.
http://www.tokeru.com/cgwiki [www.tokeru.com]
https://www.patreon.com/mattestela [www.patreon.com]
User Avatar
Member
459 posts
Joined: Oct. 2011
Offline
Thanks alot Jesse!
I didn't know about the trick to move back and fourth to the origin. Very nice. I also realised that i don't have to calculate the pivot in the first place , i can just use the prim point 0. Here is what i ended up with. Removed the “ch” for sake of clarity:
float angle = 1;
vector p0,p1;
matrix3 rotm;

i@points = primpoints(0, @primnum);

p0 = point( 0, ‘P’, @points );
p1 = point( 0, ‘P’, @points );

v@pivot = p0;
v@axis = normalize( p0 - p1 );

rotm = ident();
rotate(rotm, angle, v@axis);

@P -= v@pivot;
@P *= rotm;
@P += v@pivot;

-b
http://www.racecar.no [www.racecar.no]
  • Quick Links