Welcome to Fractal Forums

Fractal Software => Help & Support => Topic started by: Kali on December 09, 2012, 06:17:23 AM




Title: Align to normals... what I'm doing wrong?
Post by: Kali on December 09, 2012, 06:17:23 AM
Need some help with this... I'm trying to make an object rotate to match the normal surface of another near object.

What I did is...

being:

pos = position of secondary object (the one to rotate)
normal = DE normal of primary object at pos

rotate object using:

rotation axis = cross(normal,(0,0,1))
rotation angle = acos(dot(normal,(0,0,1)))

So I'm aligning the object's z-axis with the normal.

This worked when I move the object aligned to the xz or yz planes...

This is a test with a simple sphere and a box:


https://dl.dropbox.com/s/zrvszku9nrsifnq/normalign1.swf


(In the example the thin torus is aligned with the xz plane)

But when the object moves outside this planes, there is an undesired rotation around the object's z-axis:


https://dl.dropbox.com/s/r3gb9np1es8upjf/normalign2.swf


I had some ideas of what could be going on but now I'm stuck and my brain hurts :embarrass:

I attached a .frag for more references, you'll see the object moves right when angle1=angle2=0, or angle1=angle2=90 (xz,yz planes)
with angle1=90 and angle2=0 (xy plane) the object always rotates at a constant speed.

Thanks in advance,




Title: Re: Align to normals... what I'm doing wrong?
Post by: asimes on December 09, 2012, 08:45:55 AM
Could it just be that when it reaches that point its target rotation goes from being PI*2 to 0 and so it spins all the way around to get to 0 before increasing again?


Title: Re: Align to normals... what I'm doing wrong?
Post by: Syntopia on December 09, 2012, 12:14:27 PM
Aligning two vectors (such as surface normals) is not enough to determine the orientation of an object. The object will be free to rotate around the aligned vectors.

If you know an orthogonal basis (such as a normal and two tangents) for each object, it should be simple to align the two objects, using a 'change of coordinate' matrix M, where the columns are simply the basis-vectors.

But if you only have the surface normal, then you do not have enough information to determine the orientation - I guess that is what you see in your second video.


Title: Re: Align to normals... what I'm doing wrong?
Post by: cKleinhuis on December 09, 2012, 12:40:28 PM
this is what tickles me all the time, when programming 3d ... seemingly simple rotations can lead to crappy animations due to indeterminism of the alignment, and basically you show an example for the holy grail problem, because this is comparable to what happens when creating a 3d rotation vector out of a 3d point ... the transform is not smooth, meaning you have one "jump" point due to the hairy ball problem ... ;)



Title: Re: Align to normals... what I'm doing wrong?
Post by: fractower on December 09, 2012, 05:44:15 PM

I have found the rotation matrix described in http://en.wikipedia.org/wiki/Rotation_matrix to be free of artefacts.

<br />R = \begin{bmatrix} \cos \theta +u_x^2 \left(1-\cos \theta\right) & u_x u_y \left(1-\cos \theta\right) - u_z \sin \theta & u_x u_z \left(1-\cos \theta\right) + u_y \sin \theta \\ u_y u_x \left(1-\cos \theta\right) + u_z \sin \theta & \cos \theta + u_y^2\left(1-\cos \theta\right) & u_y u_z \left(1-\cos \theta\right) - u_x \sin \theta \\ u_z u_x \left(1-\cos \theta\right) - u_y \sin \theta & u_z u_y \left(1-\cos \theta\right) + u_x \sin \theta & \cos \theta + u_z^2\left(1-\cos \theta\right) \end{bmatrix}


Title: Re: Align to normals... what I'm doing wrong?
Post by: Kali on December 09, 2012, 06:30:41 PM
Adding an aditional rotation around objects z axis, with angle atan(pos.x, pos.y) partially solved the problem... still rotates at some positions but im on the right track (I think)


Title: Re: Align to normals... what I'm doing wrong?
Post by: eiffie on December 11, 2012, 06:21:13 PM
I'm probably going to get this matrix inverted cause I don't have a code example with me but here is how I do it. You know the direction you want to face but you need one more direction like UP. Take an initial guess that up will be about vec3(0.,1.,0.) then build a rotation matrix like this...

mat3 matForward(vec3 fw){
  vec3 up=vec3(0.,1.,0.),rt=normalize(cross(fw,up));
  up=cross(rt,fw);
  return mat3(-rt,up,fw);
}

I should probably check this first lol :) Not sure about -rt and hopefully the order isn't mat3(rt.x,up.x,fw.x..


Title: Re: Align to normals... what I'm doing wrong?
Post by: jehovajah on December 17, 2012, 10:51:37 AM
this is what tickles me all the time, when programming 3d ... seemingly simple rotations can lead to crappy animations due to indeterminism of the alignment, and basically you show an example for the holy grail problem, because this is comparable to what happens when creating a 3d rotation vector out of a 3d point ... the transform is not smooth, meaning you have one "jump" point due to the hairy ball problem ... ;)



It tickles me too. We do not know 3d space like we think we do, and our mathematical models are only worked out for certain boundary conditions. Our computers are fantastic and can now show us where we are going wrong in our modeling. The behaviour reminds me of the inverse aspect of the spherical reference frame, that is for every point there is also an inverse point. so when you model  sphere for large r you also get a sphere for inverse r. You probably just have to splice the section where this happens out in your formulation.

Have you ever wondered why hose pipes and electrical cables get twisted up? Paul Dirac gave an explanation of this which essentially says that we should work with 4π not 2π to get things straightened out, so maybe look at your angular ratios too.

Trochoids, man. Everything in 3d space is Trochoids! lol!


Title: Re: Align to normals... what I'm doing wrong?
Post by: Kali on December 17, 2012, 05:51:32 PM
I couldn't resolve this yet, thanks for your comments anyway.

@eiffie: didn't work, or I'm doing it wrong (most likely  ;D)


Title: Re: Align to normals... what I'm doing wrong?
Post by: fractower on December 17, 2012, 06:43:49 PM

Try starting with the normal that points toward the origin (which you already have) and the direction you want to object to go. The axis of rotation is just the cross product R = cross(direction, normal).

For example if you want to orbit in the XZ plain then start with position = (0,0,-1), normal = (0,0,1) and direction =(1,0,0).

The axis of rotation is R = cross(direction, normal) = (0,-1,0).

Even if the direction is not orthogonal to the normal the resulting motion will be.


Title: Re: Align to normals... what I'm doing wrong?
Post by: Syntopia on December 17, 2012, 09:22:04 PM
The rotation matrix from Wikipedia that fractower refers to, is the same as the 'rotationMatrix3' function in Fragmentarium.

Eiffies solution is the same as I suggested (change-of-coordinate matrix), but you still need to have more than one than one axis to align two objects.

As fractower suggests, if you know the trajectory in advance (or can calculate force and acceleration), you may use this to define a coordinate system following the object - this is a called a Frenet frame: http://en.wikipedia.org/wiki/Frenet%E2%80%93Serret_formulas

You could also derive a velocity vector based on the last position of your object, but that is not really possible in Fragmentarium, since you cannot access the last value of a uniform variable.


Title: Re: Align to normals... what I'm doing wrong?
Post by: cKleinhuis on December 18, 2012, 12:18:53 AM
hey kali and others
isnt this the hairy ball theorem problem ?
http://en.wikipedia.org/wiki/Hairy_ball_theorem

this bugged me often enough, but you already have the solution:

what syntopia meant was:

you need to define your object with a 3 component normal on each vertice :D


Title: Re: Align to normals... what I'm doing wrong?
Post by: cKleinhuis on December 18, 2012, 12:21:19 AM
and what you are seeing there is basically the interpolated transform over the non-continous parts of your function...

to get you another view on this

what you are visualisating is the tranform from a cartesian coordinate to a polar representation

this operation is used in the triplex algebra

fractalforums.com members rumour that this could be the reason for the "whipped cream" in the standard bulb ....  :whistle2:


Title: Re: Align to normals... what I'm doing wrong?
Post by: eiffie on December 18, 2012, 09:00:37 PM
OK Kali here is what you want (see attached). This brought up an interesting question about apparent wobble. I think I'll make a video to show what I mean since talking in 3d is challenging.


Title: Re: Align to normals... what I'm doing wrong?
Post by: Syntopia on December 18, 2012, 09:44:49 PM
Ah. now I can understand what you where asking about, Kali. You just wanted to attach the box rigidly to the ring. I thought you wanted to place the box on an arbitrary DE-object. Sorry for all the confusion :-)


Title: Re: Align to normals... what I'm doing wrong?
Post by: Kali on December 19, 2012, 05:51:10 AM
@eiffie:  :thanks1: now I understand how it works, and it works perfect  :beer: - Now I have to design a DE-estimated land-rover for an exploration mission to a KIFS fractal  ;D

@Syntopia: No, you were right, the ring was just to show the orbit I'm moving the object, and how with orbit aligned to some planes it worked fine and in other arbitrary angles there was the undesired rotation problem, just happened that with a DE sphere the normal also aligns with the ring, but the idea is to use on arbitrary DE objects indeed:

https://dl.dropbox.com/s/xijy4pu0xbl00xc/alinormal_001.swf

Here I'm using a normal a bit far from the surface, to make the movement smoother, and getting rid of little details... on a more complex fractal surface the object moves drastically because I'm taking only one normal and it varies a lot. Now the challenge is to make a nice vehicle reacting as real as possible when it moves over the surface.. but that's another story, will see later how it can be done...

many thanks for your help and comments.

 


Title: Re: Align to normals... what I'm doing wrong?
Post by: eiffie on December 19, 2012, 06:52:50 PM
Glad it makes sense because my video is really confusing :)
http://www.youtube.com/watch?v=lXLQMF8V9oc (http://www.youtube.com/watch?v=lXLQMF8V9oc)