From 0de0587cb86e6ac3bdb4389a0ea41a848fbcf66f Mon Sep 17 00:00:00 2001 From: Dustinspace217 Date: Wed, 18 Nov 2026 01:38:09 -0800 Subject: [PATCH 1/3] Add a weapon flag to use the weapon's radius in ship collisions Ship-weapon collisions trace a thin line, so a weapon's collision radius never matters against ships. The new "use radius for ship collisions" flag makes the hull check and the shield-mesh check (normal and surface shields) sweep a sphere of the weapon's radius instead. The collision pair cull also counts that radius, so a wide shot passing a small ship is still checked. Ships with auto-spread shields keep the line, since their shield is already a sphere sweep and a wider hull check could reach past it. A flagged weapon with a zero radius also keeps the line. Weapons without the flag are unchanged. --- code/object/collideshipweapon.cpp | 19 ++++++++++++++----- code/object/objcollide.cpp | 6 +++++- code/weapon/weapon_flags.h | 1 + code/weapon/weapons.cpp | 1 + 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/code/object/collideshipweapon.cpp b/code/object/collideshipweapon.cpp index c06d08dc192..d8347dadd74 100644 --- a/code/object/collideshipweapon.cpp +++ b/code/object/collideshipweapon.cpp @@ -262,6 +262,15 @@ static std::tuple ship_weapon_check_coll mc_hull.p1 = &weapon_end_pos; mc_hull.lod = sip->collision_lod; + int hull_flags = MC_CHECK_MODEL; + // model_collide rejects a zero radius; auto-spread ships keep the line so their shield sphere stays outermost. + if (wip->wi_flags[Weapon::Info_Flags::Use_radius_for_ship_collisions] && weapon_objp->radius > 0.0f && + !sip->flags[Ship::Info_Flags::Auto_spread_shields]) { + hull_flags |= MC_CHECK_SPHERELINE; + mc_hull.radius = weapon_objp->radius; + } + int shield_flags = MC_CHECK_SHIELD | (hull_flags & MC_CHECK_SPHERELINE); + // (btw, these are leftover comments from below...) // // Note: This code is obviously stupid. We want to add the shield point if there is shield to hit, but: @@ -391,13 +400,13 @@ static std::tuple ship_weapon_check_coll } else if (sip->flags[Ship::Info_Flags::Surface_shields]) { if (pm->shield.ntris > 0) { // If there is a shield mesh, we need to check that first - mc_shield.flags = MC_CHECK_SHIELD; + mc_shield.flags = shield_flags; shield_collision = model_collide(&mc_shield); } if (!shield_collision) { // But if no shield mesh or it was missed, check for a hull collision - mc_shield.flags = MC_CHECK_MODEL; + mc_shield.flags = hull_flags; shield_collision = model_collide(&mc_shield); // Because we used MC_CHECK_MODEL, the returned hit position might be @@ -409,14 +418,14 @@ static std::tuple ship_weapon_check_coll } } else { // Normal collision check against a shield mesh - mc_shield.flags = MC_CHECK_SHIELD; + mc_shield.flags = shield_flags; shield_collision = (pm->shield.ntris > 0) ? model_collide(&mc_shield) : 0; } } // If we found a shield collision but were only checking for a simple model // collision, we can re-use the same collision info for the hull as well - if (shield_collision && mc_shield.flags == MC_CHECK_MODEL) { + if (shield_collision && mc_shield.flags == hull_flags) { mc_hull = mc_shield; hull_collision = shield_collision; @@ -426,7 +435,7 @@ static std::tuple ship_weapon_check_coll shield_collision = 0; } } else { - mc_hull.flags = MC_CHECK_MODEL; + mc_hull.flags = hull_flags; hull_collision = model_collide(&mc_hull); } diff --git a/code/object/objcollide.cpp b/code/object/objcollide.cpp index 5b2ffc575b2..da46f59f49e 100644 --- a/code/object/objcollide.cpp +++ b/code/object/objcollide.cpp @@ -294,7 +294,11 @@ int weapon_will_never_hit( object *obj_weapon, object *other, obj_pair * current } // vm_vec_copy_scale( &laser_vel, &weapon->orient.vec.fvec, max_vel_weapon ); - delta_t = (other->radius + 10.0f) / max_vel_other; // time to get from center to radius of other obj + float reach = other->radius; + if (wip->wi_flags[Weapon::Info_Flags::Use_radius_for_ship_collisions]) { + reach += obj_weapon->radius; + } + delta_t = (reach + 10.0f) / max_vel_other; // time to get from center to radius of other obj delta_x_dot_vl = vm_vec_dot( &delta_x, &obj_weapon->phys_info.vel); a = max_vel_weapon * max_vel_weapon - max_vel_other*max_vel_other; diff --git a/code/weapon/weapon_flags.h b/code/weapon/weapon_flags.h index e86eccb0654..cdac9117cde 100644 --- a/code/weapon/weapon_flags.h +++ b/code/weapon/weapon_flags.h @@ -98,6 +98,7 @@ namespace Weapon { Freespace_1_missile_behavior, // Bundles several observed behaviors missiles had in the freespace 1 release Dogfight_weapon, // Dogfight weapons are intended as balanced variants for multiplayer. This flag can be used to filter them out when necessary. Mine, // weapon is a stationary proximity mine (subset of secondary): zero velocity, infinite lifetime + Use_radius_for_ship_collisions, NUM_VALUES }; diff --git a/code/weapon/weapons.cpp b/code/weapon/weapons.cpp index 4df5ca453a4..32badfe101e 100644 --- a/code/weapon/weapons.cpp +++ b/code/weapon/weapons.cpp @@ -261,6 +261,7 @@ special_flag_def_list_new&>); From 73a2bc9789f4d82fbcc6ad58c4b245e9fbe98bb4 Mon Sep 17 00:00:00 2001 From: Dustinspace217 Date: Wed, 18 Nov 2026 01:51:04 -0800 Subject: [PATCH 2/3] Keep flagged weapon hits no deeper than the line hit A sphere sweep skips faces the sphere already overlaps when the sweep starts, so a flagged weapon fired from close range registered its hit on polygons deeper inside the hull than the plain line would. Flagged weapons now also check the shot's line: when the sphere hits, the face ahead on the line is used if it is nearer (keeping the sphere's hit time); when the sphere misses, a line hit within the frame still counts. This covers the hull and shield meshes. The collision cull now adds the weapon's radius only against ships, and also on the path used by turning and accelerating weapons. The comment at the auto-spread exclusion gives its second reason. --- code/object/collideshipweapon.cpp | 33 ++++++++++++++++++++++++++----- code/object/objcollide.cpp | 11 ++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/code/object/collideshipweapon.cpp b/code/object/collideshipweapon.cpp index d8347dadd74..9c0ec5c781e 100644 --- a/code/object/collideshipweapon.cpp +++ b/code/object/collideshipweapon.cpp @@ -187,6 +187,28 @@ static void ship_weapon_do_hit_stuff(object *pship_obj, object *weapon_obj, cons extern int Framecount; +// A sphere sweep skips faces it already overlaps at the start, so the face ahead on the shot's line wins if nearer. +static int model_collide_nearest(mc_info* mc) +{ + int hit = model_collide(mc); + if (mc->flags & MC_CHECK_SPHERELINE) { + mc_info line = *mc; + line.flags = (mc->flags & ~MC_CHECK_SPHERELINE) | MC_CHECK_RAY; + vec3d dir = *mc->p1 - *mc->p0; + if (model_collide(&line) && + (hit ? vm_vec_dot(&line.hit_point_world, &dir) < vm_vec_dot(&mc->hit_point_world, &dir) + : line.hit_dist <= 1.0f)) { + if (hit) { + line.hit_dist = mc->hit_dist; + } + line.flags = mc->flags; + *mc = line; + hit = line.num_hits; + } + } + return hit; +} + //need_postproc, recheck, do collision? static std::tuple ship_weapon_check_collision(object *ship_objp, object *weapon_objp, float time_limit = 0.0f, int *next_hit = nullptr) { @@ -263,7 +285,8 @@ static std::tuple ship_weapon_check_coll mc_hull.lod = sip->collision_lod; int hull_flags = MC_CHECK_MODEL; - // model_collide rejects a zero radius; auto-spread ships keep the line so their shield sphere stays outermost. + // model_collide rejects a zero radius; auto-spread ships keep the line so their shield sphere stays outermost, + // and so hull_flags stays MC_CHECK_MODEL there for the shield-reuse test below. if (wip->wi_flags[Weapon::Info_Flags::Use_radius_for_ship_collisions] && weapon_objp->radius > 0.0f && !sip->flags[Ship::Info_Flags::Auto_spread_shields]) { hull_flags |= MC_CHECK_SPHERELINE; @@ -401,13 +424,13 @@ static std::tuple ship_weapon_check_coll if (pm->shield.ntris > 0) { // If there is a shield mesh, we need to check that first mc_shield.flags = shield_flags; - shield_collision = model_collide(&mc_shield); + shield_collision = model_collide_nearest(&mc_shield); } if (!shield_collision) { // But if no shield mesh or it was missed, check for a hull collision mc_shield.flags = hull_flags; - shield_collision = model_collide(&mc_shield); + shield_collision = model_collide_nearest(&mc_shield); // Because we used MC_CHECK_MODEL, the returned hit position might be // in a submodel's frame of reference, so we need to ensure we end up @@ -419,7 +442,7 @@ static std::tuple ship_weapon_check_coll } else { // Normal collision check against a shield mesh mc_shield.flags = shield_flags; - shield_collision = (pm->shield.ntris > 0) ? model_collide(&mc_shield) : 0; + shield_collision = (pm->shield.ntris > 0) ? model_collide_nearest(&mc_shield) : 0; } } @@ -436,7 +459,7 @@ static std::tuple ship_weapon_check_coll } } else { mc_hull.flags = hull_flags; - hull_collision = model_collide(&mc_hull); + hull_collision = model_collide_nearest(&mc_hull); } // check if the hit point is beyond the clip plane when warping out. diff --git a/code/object/objcollide.cpp b/code/object/objcollide.cpp index da46f59f49e..e966e88382a 100644 --- a/code/object/objcollide.cpp +++ b/code/object/objcollide.cpp @@ -276,6 +276,11 @@ int weapon_will_never_hit( object *obj_weapon, object *other, obj_pair * current } } + float reach = other->radius; + if (wip->wi_flags[Weapon::Info_Flags::Use_radius_for_ship_collisions] && other->type == OBJ_SHIP) { + reach += obj_weapon->radius; + } + // check weapon that does not turn against sphere expanding at ship maxvel // compare (weapon) ray with expanding sphere (ship) to find earliest possible collision time // look for two time solutions to Xw = Xs, where Xw = Xw0 + Vwt*t Xs = Xs + Vs*(t+dt), where Vs*dt = radius of ship @@ -294,10 +299,6 @@ int weapon_will_never_hit( object *obj_weapon, object *other, obj_pair * current } // vm_vec_copy_scale( &laser_vel, &weapon->orient.vec.fvec, max_vel_weapon ); - float reach = other->radius; - if (wip->wi_flags[Weapon::Info_Flags::Use_radius_for_ship_collisions]) { - reach += obj_weapon->radius; - } delta_t = (reach + 10.0f) / max_vel_other; // time to get from center to radius of other obj delta_x_dot_vl = vm_vec_dot( &delta_x, &obj_weapon->phys_info.vel); @@ -371,7 +372,7 @@ int weapon_will_never_hit( object *obj_weapon, object *other, obj_pair * current const float max_vel = max_vel_weapon + max_vel_other; // suggest that fudge factor for other radius be changed to other_radius + const (~10) - const float dist = vm_vec_dist( &other->pos, &obj_weapon->pos ) - (other->radius + 10.0f); + const float dist = vm_vec_dist(&other->pos, &obj_weapon->pos) - (reach + 10.0f); if ( dist > 0.0f ) { const float time = (dist*1000.0f) / max_vel; int time_ms = fl2i(time); From 9a03cd87dfb18e98eb2c081faa2c7aceeec9ab57 Mon Sep 17 00:00:00 2001 From: Dustinspace217 Date: Wed, 18 Nov 2026 02:17:42 -0800 Subject: [PATCH 3/3] Keep the earlier hit time when the line's face wins When the line's face is used in place of the sphere's hit, keep whichever hit time is earlier, so the big-ship collision path never schedules that hit later than the face it lands on. --- code/object/collideshipweapon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/object/collideshipweapon.cpp b/code/object/collideshipweapon.cpp index 9c0ec5c781e..0317d9f413d 100644 --- a/code/object/collideshipweapon.cpp +++ b/code/object/collideshipweapon.cpp @@ -199,7 +199,7 @@ static int model_collide_nearest(mc_info* mc) (hit ? vm_vec_dot(&line.hit_point_world, &dir) < vm_vec_dot(&mc->hit_point_world, &dir) : line.hit_dist <= 1.0f)) { if (hit) { - line.hit_dist = mc->hit_dist; + line.hit_dist = std::min(line.hit_dist, mc->hit_dist); } line.flags = mc->flags; *mc = line;