Rotate Triangle

Blitz3D Forums/Blitz3D Programming/Rotate Triangle

TeraBit(Posted 2008) [#1]
If anyone a simple answer to this, it would save me some time.

Given an arbitrary triangle with it's centre at 0,0,0 and the face normal as a unit vector, how would I rotate it to flat across the Z axis?


Ross C(Posted 2008) [#2]
Surely some sin/cos would do this? Just rotate each point as you would a point on a circle.


TeraBit(Posted 2008) [#3]
Surely some sin/cos would do this? Just rotate each point as you would a point on a circle.

That makes perfect sense, and I can do that.

What I am looking for is the formula to convert the normal into reliable Pitch / Yaw pair to apply to it.


Ross C(Posted 2008) [#4]
Ah right. Have you tried rotating it normally then tforming the triangles points?


Stevie G(Posted 2008) [#5]
I may have misunderstood you but ...

Place a pivot at the triangles center, aligntovector the pivot on the z-axis using the face normals.

Place another pivot at the first pivots coords but move it forward on the z-axis a few units.

The functions DeltaYaw( pivot1, pivot2 ) and deltapitch( pivot1, pivot2 ) will give you the rotations which are required to rotate the triangle so that it's facing directly forward on the z-axis.


Floyd(Posted 2008) [#6]
You want to rotate a 3-point mesh so the normal points along the z-axis? I think the natural approach would be as follows.

The cross product of the normal vector with "z vector" (0,0,1) is perpendicular to both. The cross product is ( ny, -nx, 0 ). This is the desired axis of rotation.

The angle needed is just the angle between the two vectors. The cosine of this angle is the dot product 0*nx + 0*ny + 1*nz.

Here's a bare outline of the code:

; We already have triangle, a mesh with just three points.
; There is also a normal (nx,ny,nz), with length 1.
; And we have a pivot positioned at (0,0,0).

AlignToVector pivot, ny, -nx, 0, 2	; point y-axis at N crossed with (0,0,1)
TurnEntity pivot, 0, ACos(nz), 0	; pivot has orientation desired for triangle

RotateMesh triangle, EntityPitch(pivot), EntityYaw(pivot), EntityRoll(pivot)	



TeraBit(Posted 2008) [#7]
Thanks all for the replies. I have hacked together a cheesy example code with Floyds bit in. The results are not what I expected. I unweld a sphere, centre all the triangles and want them to align. So I make an entity per triangle and rotate them as per the earlier formula, but This is what I get...




Floyd(Posted 2008) [#8]
Lee,

I out-clevered myself.

The original idea was to set up the pivot as described, parent the triangle and then turn the pivot. But this affects only the triangle entity, not the underlying mesh. Updating the mesh to match the entity orientation seemed messy. Then I got the "improved" idea to use the pivot orientation to rotate the mesh. That failed because

AlignToVector pivot, ny, -nx, 0, 2

points the y-axis the way I want, but other than that the orientation could be anything.

So it is back to the original idea. I think this works:



A few notes about this:

AlignTriangle does the actual work. The face normal has been stored in the first vertex.

The triangle is equilateral for ease of visual inspection. The aligned ( green ) triangle is face-on to the camera and should look equilateral.

In theory the aligned triangle has z=0 for all vertices. In practice this is only approximately true.

Notice the minus sign on -ACos(nz). I initially forgot this. The yaw rotation has to be the opposite way from what I visualize. Those left-handed coordinates bite me in the ass every time!

And finally I really did have a clever idea, a simple way to rotate the tri mesh to match the tri entity.


TeraBit(Posted 2008) [#9]
Hi Floyd,

Thanks for that. I'll have a proper look when I get home. :)

Note: I've found it quite difficult to convert my brain back to B3D after using BlitzMax for a while, which is strange, since this kind of stuff used to come so easily. Been away too long I suppose 8D