Game Development Community

dev|Pro Game Development Curriculum

worldToLocalPoint() and localToWorldPoint()

by Orion Elenzil · 10/18/2007 (2:07 pm) · 1 comments

%obj.localToWorldPoint("0 0 0") yields one corner of the local bounding box, and %obj.localToWorldPoint("1 1 1") yields the diagonal, etc, with scale accounted for.

note - these work well for Triggers. i haven't tested them w/ other objects. the line with "bummer.." might only apply to triggers, not sure. i've been meaning to submit these as a resource but haven't because of the weirdness with the "bummer" lines and also not being totally convinced of the corresponding function for transforms.

note - requires vectorConvolve & vectorConvolveInverse, but see below.

// note that this is a POINT, not a vector.
function SceneObject::localToWorldPoint    (%this, %pnt)
{
   %mat = %this.getTransform     ();

   %pnt = setWord(%pnt, 1, getWord(%pnt, 1) - 1);           // bummer about this.
   %pnt = vectorConvolve         (%pnt, %this.getScale());
   %pnt = MatrixMulPoint         (%mat, %pnt);

   return %pnt;
}

// note that this is a POINT, not a vector.
function SceneObject::worldToLocalPoint    (%this, %pnt)
{
   %mat = %this.getWorldTransform();

   %pnt = MatrixMulPoint         (%mat, %pnt);
   %pnt = vectorConvolveInverse  (%pnt, %this.getScale());
   %pnt = setWord(%pnt, 1, getWord(%pnt, 1) + 1);           // bummer about this.

   return %pnt;
}


re vectorConvolve & vectorConvolveInverse,
if you don't have access to the C++ code,
you could implement them in script like this:
function vectorConvolve(%v1, %v2)
{
   %result        = "";
   %delim         = "";
   %numComponents = mMin(getWordCount(%v1, %v2));
   for (%n = 0; %n < %numComponents; %n++)
   {
      %result = %result @ %delim @ (getWord(%v1, %n) * getWord(%v2, %n));
      %delim  = " ";
   }
   
   return %result;
}

function vectorConvolveInverse(%v1, %v2)
{
   %result        = "";
   %delim         = "";
   %numComponents = mMin(getWordCount(%v1, %v2));
   for (%n = 0; %n < %numComponents; %n++)
   {
      %result = %result @ %delim @ (getWord(%v1, %n) / getWord(%v2, %n));
      %delim  = " ";
   }
   
   return %result;
}

#1
10/05/2007 (10:12 am)
hm,
so note this is definitely tweaked for triggers, which have a somewhat wonky coordinate system.
i think for object with a more regular coordinate system (ie, origin at the center instead of corner) this will probably yield surprising output from localToWorldPoint("0 0 0").