it works!

This commit is contained in:
Asuro
2025-03-30 00:05:52 +01:00
parent 3ce1acc633
commit e5076b0c3b
3 changed files with 83 additions and 39 deletions

View File

@@ -127,7 +127,7 @@ Vec3 Transform::GlobalToLocalDirection(Vec3 global)
bx::vec4MulMtx(out, in, MI.Transpose().M);
return {out[0], out[1], out[2]};
}
bx::Vec3 Transform::GlobalToLocalPoint(Vec3 global)
Vec3 Transform::GlobalToLocalPoint(Vec3 global)
{
UpdateMatrix();
float in[4]{global.x, global.y, global.z, 1.0f};
@@ -143,7 +143,7 @@ Vec3 Transform::LocalToGlobalDirection(Vec3 local)
bx::vec4MulMtx(out, in, M.Transpose().M);
return {out[0], out[1], out[2]};
}
bx::Vec3 Transform::LocalToGlobalPoint(Vec3 local)
Vec3 Transform::LocalToGlobalPoint(Vec3 local)
{
UpdateMatrix();
float in[4]{local.x, local.y, local.z, 1.0f};
@@ -177,3 +177,38 @@ Vec4 Mat4::Mul(const Vec4& vec)
bx::vec4MulMtx(&out.x, &vec.x, &M[0]);
return out;
}
float DotProduct(Vec3 a, Vec3 b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
Vec3 CrossProduct(Vec3 a, Vec3 b)
{
float x = a.y * b.z - a.z * b.y;
float y = a.z * b.x - a.x * b.z;
float z = a.x * b.y - a.y * b.x;
return {x, y, z};
}
Vec3 CrossProductFromPlane(Vec3 a, Vec3 b, Vec3 c)
{
// TODO: normalize might not be necessary
Vec3 lineA = (b - a).Normalize();
Vec3 lineB = (c - a).Normalize();
return CrossProduct(lineA, lineB);
}
bool RayPlaneIntersect(Vec3 l1, Vec3 l2, Vec3 p1, Vec3 p2, Vec3 p3, Vec3& out)
{
// thanks to Paul Bourke and Bryan Hanson
// l1,l2 constitute the line. P1,P2,P3 constitute the plane
out = {};
Vec3 N = CrossProductFromPlane(p1, p2, p3); // N is the normal of the plane
float n = DotProduct(N, Vec3{p3.x - l1.x, p3.y - l1.y, p3.z - l1.z});
Vec3 LbMinusLa = Vec3{l2.x - l1.x, l2.y - l1.y, l2.z - l1.z};
float d = DotProduct(N, LbMinusLa);
if (d == 0) return false; // Line is parallel to or in the plane
float u = n / d;
if ((u >= 0.0) && (u <= 1.0))
{ // Plane is between the two points
} // can be used for checking but does not influence the outcome
out = Vec3{l1.x + u * LbMinusLa.x, l1.y + u * LbMinusLa.y, l1.z + u * LbMinusLa.z};
return true;
}