5 Replies Latest reply on May 1, 2007 9:25 AM by duckets

# Slope and Rotation from face

Hi.
I'm trying to get the slope and rotation from a face that the user clicks on.
I created the attached code which was fine untill I realised that it gives the wrong results if the selected face has a combination of slope and rotation.

Trevor

• ###### 1. Re: Slope and Rotation from face
This is what I always use:

tiltAngle = vector(0,0,1).anglebetween(faceNormal)
tiltVector = faceNormal.perpendicularto(vector(0,0,1))

and then to align an object to that normal:

myObject.transform.rotation = vector(0,0,0)
myObject.transform.rotate(myObject.transform.position,tiltVector,-tiltAngle)

hope this helps,

- Ben
• ###### 2. Re: Slope and Rotation from face
Hi Ben,
Your use of perpendicularto() function has possibly helped with a separate problem I 'm trying to solve.

However, I don't think it actually helps me for this problem.
I'm not trying to align an object to a face. I'm trying to get the individual rotation and slope components from a vector. These values are then used for analysis.
Here's an example:
If I pick a face and its normal is
vector( 0.0000, -1.0000, 0.0000 )
I can get its rotation
put faceNormal.angleBetween(vector(0,1,0))
-- 180.0000

If I change the slope of the face so that its normal is
vector( 0.0000, -0.9461, 0.3240 )
when I get the rotation of the face normal, I get
put faceNormal.angleBetween(vector(0,1,0))
-- 161.0954
but I should still be getting 180.000 as the rotation is still the same.

Hope I'm confusing things with this explanation...

Regards,
Trevor

• ###### 3. Re: Slope and Rotation from face
No, you shouldn't be getting 180. Your face normal is initially pointing directly along the negative Y axis. Then you get the angle between that and 0,1,0 (directly along the positive Y axis). The angle between the two is 180.

Then you've tilted the face slightly, so the normal is slightly off the negative Y axis, and then you're measuring the angle between(0,1,0) and your new face normal, which is no longer 180 degrees in the opposite direction. It's slightly less.

The code I supplied gets the 'tilt vector' (i.e. the 'slope') and 'tile angle', from a face normal compared with a given axis (in my code, z+).

If this isn't what you want, you'll have to describe more clearly what you want - perhaps describe what you want to do with the values once you have them?

- Ben

• ###### 4. Re: Slope and Rotation from face
To demonstrate my problem, I've created an image :
one face of a box is selected and displayed in red.
It has a rotation of 180 and a slope 90.
If I adjust its slope by 10 degrees, I end up with a slope of 80 but also a rotation of 170 but you can clearly see it still has the same rotation.

I'm using these values to pass to an external analysis program which then works out the solar and fabric gain of a building.

Cheers,
Trevor
• ###### 5. Re: Slope and Rotation from face
Imagine a line protruding from that red face, perpendicular to that face. In the top image, the face is vertical, so the line protruding from that face (which, incidentally, is the 'normal' of that face). would be pointing in exactly the opposite direction to your Y axis. Your Y axis is identified by the green axis line. (RGB = XYZ, for the axis markers). Hence, 180 degrees retured by the angleBetween function.

In the lower image, the face is tilted a little, which means the line protruding from the face would be angled upwards a little. Now, if you measure the angle between that line and the green Y axis line, it is less than 180 degrees, because you have rotated it around a little.

I've done a bit of drawing on your image, to illustrate what's happening:

http://www.robotduck.com/misc/angleBetween.jpg

- Ben