Hi Henrik,
thanks for your code, one guy gave me this function:
void MathUtil::CalcBeamsClosestPoint(pfSeg* beams, int numBeams, pfVec3& p)
{
float m00 = 0.0f;
float m01 = 0.0f;
float m02 = 0.0f;
float m11 = 0.0f;
float m12 = 0.0f;
float m22 = 0.0f;
float v0 = 0.0f;
float v1 = 0.0f;
float v2 = 0.0f;
for (int i = 0; i < numBeams; i++) {
pfSeg& beam = beams[i];
pfVec3& dir = beam.dir;
pfVec3& pos = beam.pos;
const float ax = dir[0];
const float ay = dir[1];
const float az = dir[2];
const float bx = pos[0];
const float by = pos[1];
const float bz = pos[2];
m00 += ay * ay + az * az;
m11 += az * az + ax * ax;
m22 += ax * ax + ay * ay;
m01 += -ax * ay;
m02 += -az * ax;
m12 += -ay * az;
v0 += az * (az * bx - ax * bz) - ay * (ax * by - ay * bx);
v1 += ax * (ax * by - ay * bx) - az * (ay * bz - az * by);
v2 += ay * (ay * bz - az * by) - ax * (az * bx - ax * bz);
}
matrix33f m(vector3f(m00, m01, m02), vector3f(m01, m11, m12), vector3f(m02, m12, m22));
float det = m.det();
assert(det != 0.0f);
vector3f pt = inverse(m) * vector3f(v0, v1, v2);
p.set(pt.x, pt.y, pt.z);
}
it works with simple lines such as
new Line(new Vec3(0, 0, 0), new Vec3(1, 1, 1)),
new Line(new Vec3(2, 0, 0), new Vec3(1, 1, 1)),
but not with the ones I gave you previously (I obtain a different result).. I don't know why, I can't get anyway the theory behind his code
By the way, the formula is this one at the end http://math.stackexchange.com/a/61721/64943
I tried to wrote the algorithm, but I didn't make it
I should come to a form such as:
A x = b
but I don't have idea how to deal with sums and dot products..