Author Topic: Making an object face a point (solved)  (Read 1788 times)

I'm trying to create a line between point A and B. I have a static shape which is a cube sized 1x1x1 Torque units, which I scale on the Y axis to the distance between A and B. Then, I'm positioning it in the center between A and B. The only thing left is rotating it. How would I go about this?
« Last Edit: April 21, 2013, 04:55:20 AM by Port »

I had the same problem and solved it in this:
Code: [Select]
function createLaserBetweenPoints(%vec1, %vec2, %thickness, %color)
{
%diff = vectorsub(%vec1, %vec2);
%norm = vectornormalize(%diff);
%laser = new StaticShape()
{
datablock = LaserBeamShape;
scale = vectorlen(%diff) * 5 SPC %thickness SPC %thickness;
position = vectoradd(%vec2, vectorscale(%diff, 0.5));
rotation = vectornormalize(vectorcross("1 0 0", %norm)) SPC mradtodeg(macos(vectordot("1 0 0", %norm))) * -1;
};
%laser.setnodecolor("ALL", vectornormalize(%color) SPC 0.5);
return %laser;
}
remove the stuff that you don't need and it should work

I had the same problem and solved it in this:
remove the stuff that you don't need and it should work

I got it working, thanks. Trigun also tried to help a lot (thanks as well) but I ended up using this method instead.

Code: [Select]
function createLine( %a, %b, %size, %color )
{
if ( !strLen( %size ) )
{
%size = 0.05;
}

if ( !strLen( %color ) )
{
%color = "1 1 1 1";
}

%offset = vectorSub( %a, %b );
%normal = vectorNormalize( %offset );

%xyz = vectorNormalize( vectorCross( "1 0 0", %normal ) );
%pow = mRadToDeg( mACos( vectorDot( "1 0 0", %normal ) ) ) * -1;

%obj = new staticShape()
{
datablock = lineShapeData;
scale = vectorLen( %offset ) SPC %size SPC %size;

position = vectorScale( vectorAdd( %a, %b ), 0.5 );
rotation = %xyz SPC %pow;
};

missionCleanup.add( %obj );
%obj.setNodeColor( "ALL", %color );

return %obj;
}

« Last Edit: April 21, 2013, 04:35:44 AM by Port »

I didn't know there was a more simple way of doing this:
vectorToAxis(%Vector);

function eulerToAxis(%euler)
{
   %euler = VectorScale(%euler,$pi / 180);
   %matrix = MatrixCreateFromEuler(%euler);
   return getWords(%matrix,3,6);
}
function vectorToEuler(%vector)
{
   return  0 SPC (mACos(getWord(%vector,2)/vectorDist("0 0 0",%vector))*(180/$pi)+90)%360 SPC
          mATan(getWord(%vector,1),getWord(%vector,0))*(180/$pi);
}
function vectorToAxis(%vector)
{
   return eulerToAxis(vectorToEuler(%vector));
}

......


return 0 SPC (mACos(getWord(%vector,2)/vectorDist("0 0 0",%vector))*(180/$pi)+90)%360 SPC mATan(getWord(%vector,1),getWord(%vector,0))*(180/$pi);

What the hell are you doing there...