Game Development Community

Make a staticshape face a certain position

by Richard Price · in Torque Game Engine · 02/20/2012 (7:24 pm) · 3 replies

Hello, first of all, many apologies if this is the wrong section.
Anyways, I'm currently working on an add-on for the game Blockland. I'm trying to simulate some kind of electronic system and I'm stumped on how I could create the wire between two connected parts. Due to me not being the creator of the game, I do not have access to all function, so staticshapes are the only way I can manage this I believe.

First of all, I have to points that I want to have the staticshape go to and from.
http://i933.photobucket.com/albums/ad179/halcynthis/Blockland_00011-1.jpg

Then after that, I spawn the staticshape between the points.
http://i933.photobucket.com/albums/ad179/halcynthis/Blockland_00013-1.jpg

And now, this is were im stuck. I need to rotate the shape to go like this.
http://i933.photobucket.com/albums/ad179/halcynthis/Blockland_00012-1.jpg

I know how to get the distance between the two points and scale the staticshape to the proper size, but I'm clueless on how to get it to connect the two points. Help would be greatly appreciated.

About the author

Recent Threads


#1
02/21/2012 (1:02 pm)
I have found the solution to this problem; If you also need the answer I found it on the following page.
http://www.garagegames.com/community/forums/viewthread/119837
#2
02/21/2012 (3:04 pm)
The Set Forward Vector resource might help. You give it a vector, the shape faces that way. Here's some example code for making a shape face from point A to point B:

%positionA = "0 0 0";
%positionB = "1 1 1";
%vector = VectorSub(%positionB,%positionA);
%object.setForwardVector(%vector);
#3
02/22/2012 (9:03 am)
There are a bunch of "library" functions floating around on the forums that were assembled together in Zod's MG Starter Kit, and someone has posted that script file to the bighole Torque host. Here's an except that will do this in script:
$Pi = 3.14159;
$TwoPi = 6.28319;

// This lil function generates the rotation required for an object at PosOne
// to point at PosTwo (z rot only)
function pointToXYPos(%posOne, %posTwo)
{
   %vec = VectorSub(%posOne, %posTwo);

   // get the angle
   %rotAngleZ = mATan(firstWord(%vec), getWord(%vec, 1));

   // add pi to the angle
   %rotAngleZ += $Pi;

   // make this rotation a proper torque game value,
   // anything more than 240 degrees is negative
   if(%rotAngleZ > 4.18879)
   {
      // the rotation scale is seldom negative,
      // instead make the axis value negative
      %modifier = -1;

      // subtract 2pi from the value, then make sure its positive
      %rotAngleZ = mAbs(%rotAngleZ - $TwoPi);

      //sigh, if only this were all true
   }
   else
      %modifier = 1;

   // assemble the rotation and send it back
   return "0 0" SPC %modifier SPC %rotAngleZ;
}
// And this lil function generates the rotation required for an object at posOne
// to point at PosTwo for X, Y And Z axis.
function pointToPos(%posOne, %posTwo)
{
   // sub the two positions so we get a vector pointing from the origin in the
   // direction we want our object to face
   %vec = VectorSub(%posTwo, %posOne);

   // pull the values out of the vector
   %x = firstWord(%vec);
   %y = getWord(%vec, 1);
   %z = getWord(%vec, 2);

   // this finds the distance from origin to our point
   %len = vectorLen(%vec);

   //---------X-----------------
   // given the rise and length of our vector this will give us the angle in radians
   %rotAngleX = mATan(%z, %len);

   //---------Z-----------------
   // get the angle for the z axis
   %rotAngleZ = mATan(%x, %y);

   // create 2 matrices, one for the z rotation, the other for the x rotation
   %matrix = MatrixCreateFromEuler("0 0" SPC %rotAngleZ * -1);
   %matrix2 = MatrixCreateFromEuler(%rotAngleX SPC "0 0");

   // now multiply them together so we end up with the rotation we want
   %finalMat = MatrixMultiply(%matrix, %matrix2);

   // we're done, send the proper numbers back
   return getWords(%finalMat, 3, 6);
}