Author Topic: Rotate a point around another point  (Read 1530 times)

I'm trying to make %pos rotate based on %col's rotation. I took the angle code from I think greek2me's forum post.

%curAim = %col.getEyeVector();
   %curX = getWord(%curAim,0);
   %curY = getWord(%curAim,1);
   %curZ = getWord(%curAim,2);
   %theta = mRadToDeg(mATan(%curY,%curX));
   if(%curX > 0 && %curY > 0)
      %angle = %theta;
   else if(%curX < 0 && %curY > 0)
      %angle = 180 - %theta;
   else if(%curX < 0 && %curY < 0)
      %angle = %theta + 180;
   else if(%curX > 0 && %curY < 0)
      %angle = 360 - %theta;

   talk(%angle);

   %lastAngle = %col.lastAngle;
   if(%lastAngle !$= "") {
      %col.lastAngle = %angle;
      %diff = %lastangle - %angle;

      %s = mSin(%diff);
      %c = mCos(%diff);

      %px = getWord(%pos, 0);
      %py = getWord(%pos, 1);

      %ox = getWord(%col.getHackPosition(),0);
      %oy = getWord(%col.getHackPosition(),1);

      %nx = %c * (%px - %ox) - %s * (%py - %oy) + %ox;
      %ny = %s * (%px - %ox) + %c * (%py - %oy) + %oy;

      %pos = %nx SPC %ny SPC getWord(%pos,2);
      %obj.GrappleRopePos = %pos;
   }
   else {
      %col.lastAngle = %angle;
   }


right now it just spazzes out.

function vectorRotateEuler(%vec, %euler) {
   return vectorRotateAxis(%vec, eulerToAxis(%euler));
}

function vectorRotateAxis(%vec, %axis) { //Epic function found online. Credits to Blocki <3. Id also like to thank Zeblote for finding this for me <3
   %u["x"] = getword(%axis,0);
   %u["y"] = getword(%axis,1);
   %u["z"] = getword(%axis,2);

   %angl = getword(%axis,3) * -1;
   %cos = mcos(%angl);
   %sin = msin(%angl);

   %a[1,1] = %cos + (%u["x"] * %u["x"] * (1 - %cos));
   %a[1,2] = (%u["x"] * %u["y"] * (1 - %cos)) - (%u["z"] * %sin);
   %a[1,3] = (%u["x"] * %u["z"] * (1 - %cos)) + (%u["y"] * %sin);

   %a[2,1] = (%u["y"] * %u["x"] * (1 - %cos)) + (%u["z"] * %sin);
   %a[2,2] = %cos + (%u["y"] * %u["y"] * (1 - %cos));
   %a[2,3] = (%u["y"] * %u["z"] * (1 - %cos)) - (%u["x"] * %sin);

   %a[3,1] = (%u["z"] * %u["x"] * (1 - %cos)) - (%u["y"] * %sin);
   %a[3,2] = (%u["z"] * %u["y"] * (1 - %cos)) + (%u["x"] * %sin);
   %a[3,3] = %cos + (%u["z"] * %u["z"] * (1 - %cos));

   %x = getWord(%vec, 0);
   %y = getWord(%vec, 1);
   %z = getWord(%vec, 2);

   %newx = (%a[1,1] * %x) + (%a[1,2] * %y) + (%a[1,3] * %z);
   %newy = (%a[2,1] * %x) + (%a[2,2] * %y) + (%a[2,3] * %z);
   %newz = (%a[3,1] * %x) + (%a[3,2] * %y) + (%a[3,3] * %z);

   %pos = %newx SPC %newy SPC %newz;
   return %pos;
}


Also, see that quote for more complex vector math functions. Note that i'm not totally sure that some work. These ones definitely work, and i use them a lot.

protip: blocki == me

Also I realized that function is total bs, you can do the same as a one liner using the c++ matrix functions (like 1000x faster too)

protip: blocki == me

Also I realized that function is total bs, you can do the same as a one liner using the c++ matrix functions (like 1000x faster too)

Such as..?

Such as..?
I can't get the code on my phone so you'll have to wait like 9 hours unless someone else posts it

I can't get the code on my phone so you'll have to wait like 9 hours unless someone else posts it
Alright, thanks.

Well that function be simplified to

function vectorRotateEuler(%vec, %euler)
{
    return MatrixMulPoint(MatrixCreateFromEuler(%euler), %vec);
}

If you just want to rotate it based on the orientation of %col, you can use MatrixMulPoint(%col.gettransform(), %vec)

If you just want to rotate it based on the orientation of %col, you can use MatrixMulPoint(%col.gettransform(), %vec)

And in that is %vec the offset from the point to %col, or the position of the point?