Quad rotation around its surface normal?

BlitzMax Forums/MiniB3D Module/Quad rotation around its surface normal?

Krischan(Posted 2014) [#1]
I have a very simple question. I just want to rotate a quad around its surface normal, independent of its current rotation angles.

Currently, I use a second entity as a temporary child and rotate the quad in the loop according to its ex-child's rotation, this is working but not very smart ;-) Perhaps you have a better idea how to achieve this?

Here is a simple code example to play with:


Horizontal Arrows = rotate around the axis only
Vertical Arrows = rotate free around


Kryzon(Posted 2014) [#2]
Krischan,
If you're certain that the quadrilateral is planar, you can rotate it by using two auxiliary vectors: one from the centre of the quad to its top-right vertex ("right" vector) and another from the centre to the top-left vertex ("up" vector), transforming them with Sine and Cosine.

See this as reference:
http://www.blitzbasic.com/Community/posts.php?topic=102120


Krischan(Posted 2014) [#3]
Huh excellent link - I'll have to check this (missed it somehow), reminds me of my own asteroids field and will be useful in my current project :-)

In my example, the quadrilateral is planar but I'm no expert at vectors - any simple example to play with?


Kryzon(Posted 2014) [#4]
I'm afraid I just have that code example.

A way to look at the logic behind it is that you have a position in 3D space for the quadrilateral mesh, and a normal for the 3D plane where the vertices of the quadrilateral are placed - in your case, this is the "surface normal."

So you have this plane, which may be rotated in any way (the direction of the surface normal).
Using the 3D position of the vertices you calculate the "top-left" and "top-right" offset vectors (which are contained in the plane). These offset vectors go from the 3D position of the mesh to the top-left and top-right vertices, respectively.

If you add or subtract these offset vectors in a specific order from the 3D position of the mesh, you retrieve back the original position of the vertices.
But then you use Sine and Cosine to transform these offset vectors by a rotation angle. When you do this, you are discovering relative vectors to these, that when added or subtracted from the 3D position of the mesh will place the vertices in positions dislocated from their original positions.
In fact, the vertices will be placed anywhere on a circle around the mesh depending on the rotation angle you use, with the circle being aligned to the plane.

You are rotating the vertices around the 3D position of the mesh while containing them in the plane described by the normal of the mesh. Or as in my example, the plane described by the normal of the camera view (for a particle system).


Kryzon(Posted 2014) [#5]
The following is untested, but is the direction that I would go for what you want:




Krischan(Posted 2014) [#6]
I don't get it running, sorry. I think my pivot solution is not very smart but it is much simpler (and I guess - faster: look at all the calculations you're making!). And it works.

But thanks anyway.


Kryzon(Posted 2014) [#7]
I had my doubts whether a simpler, manual method would be faster than using a pivot for handling particle rotations, so in that thread above I compared them both.
Though there are considerable calculations, like you observed, the manual method is slightly faster because even though we don't see it, transforming an entity such as a pivot involves even more calculations done by the engine (from transforming the entity matrix).
But I verified this by profiling the results of course, and if you're interested, that thread comes with an example code for testing.

With a single pivot there shouldn't be much difference, so you're indeed with the saner approach, but if you happen to need that sort of normal-aligned rotation for plenty more quads, you could try giving the manual method another look.