Scripting : Align along normal

Date : Mon, 31 Oct 2005 13:34:14 +0100
To : XSI(at)Softimage.COM
From : guillaume laforge <guillaume.laforge.3d(at)gmail.com>
Subject : Scripting : Align along normal
Hi list :-)
 
I'm learning XSIMath and I can't find how to align an object along the POLYGON normal ? My script seems to work but it doesn't align perfectly along polygon normal :-/. As I'm new to those things I'm wondering if I miss something in the 3d math logic or if in XSI the polygon normal is a special case (compared to normals on points ).
 
Here is my script :
 

//AlignAlongNormal.js
//Select one polygon, run the script and pick an object.
//-----------------------------------------------------

//Get the selected polygon
var polySel = Selection.item(0).subcomponent.componentcollection;
var oPolygonFace = polySel(0);
//Get the Polygon Normal
var oPolygonNode = oPolygonFace.Nodes(0);
var >
//Get the Polygon points
1 );
//-----------------------------------------
//Find the Position.
var oPos = XSIMath.CreateVector3();
for ( var i = 0 ; i < oNborsPoints.count ; i++ )
{
oPos.x = oPos.x+oNborsPoints(i).Position.x;
oPos.y = oPos.y+oNborsPoints(i).Position.y;
oPos.z = oPos.z +oNborsPoints(i).Position.z;
}
oPos.x = oPos.x/oNborsPoints.count;
oPos.y = oPos.y/oNborsPoints.count;
oPos.z = oPos.z /oNborsPoints.count; 

//-------------Align the picked object------------------
//Pick session
var oPicked = PickObject ("Please pick an object") ;
var buttonChoice = oPicked.Value( "ButtonPressed" ) ;
var MyObj = oPicked.item(2);

//Rotation
MyObj.kinematics.global.transform = RotationFromPoly(oNormal) ;

//Position
MyObj.Kinematics.Global.Parameters("posx").value = oPos.x;
MyObj.Kinematics.Global.Parameters("posy").value = oPos.y;
MyObj.Kinematics.Global.Parameters("posz").value = oPos.z;

//**********From Ray-T Function**********
function RotationFromPoly(polyNormal)
{
var upVec = XSIMath.CreateVector3();
upVec.set(0, 1, 0);
var axis = XSIMath.CreateVector3();
axis.cross(upVec, polyNormal);
var angle = Math.acos(upVec.dot(polyNormal));
var rot = XSIMath.CreateRotation();
rot.SetFromAxisAngle(axis, angle);
var transfo = XSIMath.CreateTransform();
transfo.SetRotation(rot);

return transfo;
}

Any advice from experienced user is welcome !

Cheers,

Guillaume Laforge
CG artist | Jr td :)

 


Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.