Author Topic: [Resource] Advanced Vector Math  (Read 1749 times)

These are some vector functions I've built up over time. I find myself sharing them frequently, so I'm just gonna put them here. They require the stickied euler/axis conversion functions.


"Basic" vector manipulation
Code: [Select]
function vectorLerp(%init, %end, %t) {
return vectorAdd(%init, vectorScale(vectorSub(%end, %init), %t));
}

function vectorSlerp(%init, %end, %t) { //Thanks wikipedia! Returns spherical interpolation of two vectors
%ang = vectorAngleBetween(%init, %end);
//vecA * (sin((1-t)*a)/sin(a)) + vecB * (sin(t*a)/sin(a))
return vectorAdd(vectorScale(%init, mSin((1-%t) * %ang) / mSin(%ang)), vectorScale(%end, mSin(%t * %ang) / mSin(%ang)));
}

function vectorAngleBetween(%vec1, %vec2) { //Returns the angle between two vectors in radians
return mACos(vectorDot(%vec1, %vec2)/(vectorLen(%vec1) * vectorLen(%vec2)));
}

function vectorComponent(%vec1, %vec2) { //Returns the component of vec1 in the direction of vec2. This is a float.
return vectorDot(%vec1, vectorNormalize(%vec2));
}

function vectorProjection(%vec1, %vec2) { //Returns the vector projection, similar to component, but this is a vector
return vectorScale(vectorNormalize(%vec2), vectorComponent(%vec1, %vec2));
}

function vectorRejection(%vec1, %vec2) { //Returns the vector rejection of vec1 from vec2, this is another vector
return vectorSub(%vec1, vectorProjection(%vec1, %vec2));
}



Just a useful vector function ill throw in free of charge
Code: [Select]
function vectorFloatLength(%vec, %float) {
return mFloatLength(getWord(%vec, 0), %float) SPC mFloatLength(getWord(%vec, 1), %float) SPC mFloatLength(getWord(%vec, 2), %float);
}


Angle conversion, use along side Trader's eulerToAxis/axisToEuler
Code: [Select]
function eulerToVector(%ang) {
if(getWordCount(%ang) == 2) //Support for giving pitch and yaw, but not roll.
%ang = getWord(%ang, 0) SPC 0 SPC getWord(%ang, 1);

%yaw = mDegToRad(getWord(%ang, 2));
%pitch = mDegToRad(getWord(%ang, 0));
%x = mSin(%yaw) * mCos(%pitch) * 1;
%y = mCos(%yaw) * mCos(%pitch);
%z = mSin(%pitch);

return %x SPC %y SPC %z;
}

function axisToVector(%ang) {
return eulerToVector(axisToEuler(%ang));
}

function vectorToEuler(%vec) {
%vec = vectorNormalize(%vec);
%yaw   = mRadToDeg(mATan(getWord(%vec, 0), getWord(%vec, 1)));
%pitch = mRadToDeg(mASin(getWord(%vec, 2)));
return %pitch SPC 0 SPC %yaw;
}

function vectorToAxis(%vec) {
return eulerToAxis(vectorToEuler(%vec));
}


Angle rotating
Code: [Select]
function vectorRotateVector(%vec1, %vec2) {
return vectorRotateAxis(%vec1, vectorToAxis(%vec2));
}

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;
}



Its worth noting that there are better ways to do these, mainly the vectorRotateAxis using matrix multiplication, somebody has kept throwing code at me but every time I try it I cant get it to work. These functions are tried and tested (possibly with the exception of vectorRotateVector, but I honestly don't even know why you'll need that, I never have, hence it not being as tested as the rest). But yeah, generally, these functions suck, but they're so damn useful I don't mind.