MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / check_vector_to_cylinder

Function check_vector_to_cylinder

physics/findintersection.cpp:1284–1431  ·  view source on GitHub ↗

check if a sphere intersects a face -- this can be optimized (only need 2d stuff after rotation)

Source from the content-addressed store, hash-verified

1282
1283// check if a sphere intersects a face -- this can be optimized (only need 2d stuff after rotation)
1284int check_vector_to_cylinder(vector *colp, vector *intp, float *col_dist, vector *wall_norm, const vector *p0,
1285 const vector *p1, float rad, vector *ep0, vector *ep1) {
1286 matrix edge_orient;
1287 vector po0, po1;
1288 vector edgevec = *ep1 - *ep0;
1289 vector mvec;
1290 vector closest_pnt;
1291
1292 float edge_len;
1293 float dist;
1294 float vector_len;
1295 float dist_from_origin;
1296 float dist_to_intersection;
1297 int i;
1298 int valid_hit = 0;
1299
1300 vector mvec3d;
1301 float vector_len3d;
1302
1303 float t[4];
1304 vector ivertex[4];
1305
1306 int valid_t[4];
1307 float cole_dist[4];
1308 vector inte[4];
1309
1310 mvec3d = *p1 - *p0;
1311 vector_len3d = vm_NormalizeVector(&mvec3d);
1312
1313 edge_len = vm_NormalizeVector(&edgevec);
1314
1315 vector init_normal;
1316 bool f_init_collide;
1317
1318 if (!IsPointInCylinder(&init_normal, ep0, &edgevec, edge_len, rad, p0, &mvec3d, &f_init_collide)) {
1319 vm_VectorToMatrix(&edge_orient, &edgevec, nullptr, nullptr);
1320
1321 po0 = (*p0 - *ep0) * edge_orient;
1322 po1 = (*p1 - *ep0) * edge_orient;
1323
1324 po0.z = po1.z = 0.0;
1325 mvec = po1 - po0;
1326 vector_len = vm_NormalizeVector(&mvec);
1327
1328 dist = -(mvec * po0);
1329
1330 closest_pnt = po0 + dist * mvec;
1331 // ASSERT(!(closest_pnt.x == 0.0 && closest_pnt.y == 0.0 && closest_pnt.z == 0)); -- why does this matter?
1332
1333 dist_from_origin = vm_GetMagnitude(&closest_pnt);
1334 if (dist_from_origin >= rad)
1335 return 0;
1336
1337 dist_to_intersection = sqrt(rad * rad - dist_from_origin * dist_from_origin);
1338
1339 t[0] = (dist + dist_to_intersection) / vector_len; // (0.0 to 1.0) is on line
1340 if (t[0] >= 0.0 && t[0] <= 1.0) {
1341 valid_t[0] = 1;

Callers 1

check_sphere_to_faceFunction · 0.85

Calls 5

vm_NormalizeVectorFunction · 0.85
IsPointInCylinderFunction · 0.85
check_vector_to_sphere_1Function · 0.85
vm_VectorToMatrixFunction · 0.50
vm_GetMagnitudeFunction · 0.50

Tested by

no test coverage detected