Files
BT411/engine/MUNGA/RAY.cpp
T
arcattackandClaude Opus 4.8 7b7d465e5e Initial commit: bt411 -- standalone Windows BattleTech (Tesla 4.10 port)
Clean, self-contained extraction of the BattleTech-specific work from the
reverse-engineering workspace -- engine + game + content + build, with nothing
from Red Planet or the raw archive dumps. Builds green (Win32) and runs the
single-player drive->animate->target->fire->damage->destroy loop out of the box.

Layout:
  engine/   MUNGA + MUNGA_L4 shared 2007 engine, carrying our BT render/loader
            work (bgfload/L4D3D/L4VIDEO: BSL bit-slice decode, LOD/ground/shadow
            models) + image codec; the minimal rp/ headers the audio HAL needs
  game/     reconstructed BT logic + surviving-original BT source + fwd shims
            + WinMain launcher
  content/  full runtime tree (BTL4.RES, VIDEO/, GAUGE/, AUDIO/, eggs, BTDPL.INI)
  docs/     format specs + reconstruction ledgers
  reference/ raw Ghidra pseudocode (recon source-of-truth) + decomp exporter
  tools/    MP console emulator + map/resource scanners

One top-level CMake builds munga_engine lib + bt410_l4 game lib + btl4.exe.
All paths relativized (186 fwd shims + ~437 CMake abs paths -> repo-relative);
DXSDK is the one external, overridable via -DDXSDK. Verified: builds to a
byte-identical 2.27MB exe and runs combat (TARGET DESTROYED, 0 crashes) against
the bundled content.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-05 21:03:40 -05:00

169 lines
5.0 KiB
C++

#include "munga.h"
#pragma hdrstop
#include "ray.h"
#include "plane.h"
#include "sphere.h"
//
//#############################################################################
//#############################################################################
//
void Ray::Project(Scalar length, Point3D *result)
{
Check(this);
Check(result);
Vector3D temp;
temp.Multiply(direction,length);
result->Add(origin,temp);
}
//
//#############################################################################
//#############################################################################
//
Scalar Ray::LengthToClosestPointTo(const Point3D &point)
{
Check(this);
Check(&point);
Vector3D temp;
temp.Subtract(point,origin);
return temp*direction;
}
//
//#############################################################################
//#############################################################################
//
Scalar Ray::DistanceTo(const Plane &plane, Scalar *product) const
{
Scalar t;
//
//------------------------------------------------------------------
// Compute the dot product of the ray and plane normal, and find the
// distance from the origin of the ray to the plane
//------------------------------------------------------------------
//
*product = plane.normal * direction;
t = plane.DistanceTo(origin);
//
//----------------------------------------------------------------------
// If the ray is not parallel to the plane, determine how far to proceed
// along the ray until we hit the plane
//----------------------------------------------------------------------
//
if (!Small_Enough(*product))
t /= -*product;
return t;
}
//
//#############################################################################
//#############################################################################
//
Scalar Ray::DistanceTo(const Sphere &sphere, Scalar *penetration) const
{
Scalar b, c;
Vector3D temp;
//
//-------------------------------------------------------------------------
// Set up to solve a quadratic equation for the intersection of the ray and
// sphere. The solution is based on finding the closest point on the line
// to the sphere, and then calculating the interval between the entry and
// exit points of the ray
//-------------------------------------------------------------------------
//
temp.Subtract(origin,sphere.center);
b = 2.0f * (direction * temp);
c = temp.LengthSquared() - sphere.radius*sphere.radius;
//
//--------------------------------------------------------------------------
// Compute the squared interval to use for the solution. If it is negative,
// then the ray misses the sphere
//--------------------------------------------------------------------------
//
*penetration = b*b - 4.0f*c;
if (*penetration<SMALL)
return 0.0f;
else
{
//-------------------------------------------------------------------------
// Otherwise, find the linear distance along the line of the entry point by
// subtracting half the interval between entry and exit points from the
// distance to the closest point on the sphere
//-------------------------------------------------------------------------
*penetration = Sqrt(*penetration);
return -0.5f*(b+*penetration);
}
}
//
//#############################################################################
//#############################################################################
//
Logical Ray::TestInstance() const
{
return true;
}
//
//#############################################################################
//#############################################################################
//
Scalar Find_Closest_Approach(const Point3D& origin1, const Vector3D& velocity1, Point3D *result1, const Point3D& origin2, const Vector3D& velocity2, Point3D *result2, Scalar *time, Logical *constant)
{
Vector3D a,b;
a.Subtract(origin1, origin2);
b.Subtract(velocity1, velocity2);
//
//--------------------------------------------------------------------
// If the velocities are identical, any point will do for the test, so
// simply return the difference between the starting points
//--------------------------------------------------------------------
//
Scalar d = b.LengthSquared();
if (Small_Enough(d))
{
*constant = True;
d = a.Length();
Check_Fpu();
return d;
}
//
//-------------------------------------------------------------------------
// The velocities are not parallel, so figure out when the closest approach
// is via the derivative
//-------------------------------------------------------------------------
//
*constant = False;
*time = (a * b) / -d;
Check_Fpu();
//
//------------------------------------------------------
// Now, plot the resultant points of both line equations
//------------------------------------------------------
//
Vector3D closest;
closest.AddScaled(a, b, *time);
result1->AddScaled(origin1, velocity1, *time);
result2->AddScaled(origin2, velocity2, *time);
d = closest.Length();
Check_Fpu();
return d;
}
#if defined(TEST_CLASS)
# include "ray.tcp"
#endif