【发布时间】:2014-10-25 10:22:55
【问题描述】:
我想将 P3(附近的某个地方)围绕一个向量(与 P1 和 P2 相交)旋转 x 度。
P1 和 P2 是与图像中的矢量(线)e 相交的 2 个点。我研究和搜索了很多,找到了一些很好的材料,但是我的三角函数太差了。我需要这个用于 PAWN(小),这里我有一些代码,但并没有真正按预期工作。如果有人可以帮助我,我将非常感激:)
P1 / P2 / P3 = (x,y,z)
float vec[3];
SubtractVectors(P1, P2, vec);
float newp[3];
float rotation[4][4];
SetupMatrix(90.0, vec, rotation);
MultiplyMatrix(P3, rotation, newp);
//------------------------------------------------
stock void MultiplyMatrix(float input[3], float rotation[4][4], float output[3])
{
float input2[4];
input2[0] = input[0];
input2[1] = input[1];
input2[2] = input[2];
input2[3] = 1.0;
float output2[4];
for(int i = 0 ; i < 4 ; i++)
{
for(int j = 0 ; j < 4 ; j++)
{
output2[i] += rotation[i][j] * input2[j];
}
}
output[0] = output2[0];
output[1] = output2[1];
output[2] = output2[2];
}
stock void SetupMatrix(float angle, float vector[3], float rotation[4][4])
{
float L = (vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
angle = angle * M_PI / 180.0;
float u2 = vector[0] * vector[0];
float v2 = vector[1] * vector[1];
float w2 = vector[2] * vector[2];
rotation[0][0] = (u2 + (v2 + w2) * Cosine(angle)) / L;
rotation[0][1] = (vector[0] * vector[1] * (1 - Cosine(angle)) - vector[2] * SquareRoot(L) * Sine(angle)) / L;
rotation[0][2] = (vector[0] * vector[2] * (1 - Cosine(angle)) + vector[1] * SquareRoot(L) * Sine(angle)) / L;
rotation[0][3] = 0.0;
rotation[1][0] = (vector[0] * vector[1] * (1 - Cosine(angle)) + vector[2] * SquareRoot(L) * Sine(angle)) / L;
rotation[1][1] = (v2 + (u2 + w2) * Cosine(angle)) / L;
rotation[1][2] = (vector[1] * vector[2] * (1 - Cosine(angle)) - vector[0] * SquareRoot(L) * Sine(angle)) / L;
rotation[1][3] = 0.0;
rotation[2][0] = (vector[0] * vector[2] * (1 - Cosine(angle)) - vector[1] * SquareRoot(L) * Sine(angle)) / L;
rotation[2][1] = (vector[1] * vector[2] * (1 - Cosine(angle)) + vector[0] * SquareRoot(L) * Sine(angle)) / L;
rotation[2][2] = (w2 + (u2 + v2) * Cosine(angle)) / L;
rotation[2][3] = 0.0;
rotation[3][0] = 0.0;
rotation[3][1] = 0.0;
rotation[3][2] = 0.0;
rotation[3][3] = 1.0;
}
【问题讨论】:
标签: math matrix vector rotation