Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions code/object/collideshipweapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = std::min(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<bool, bool, ship_weapon_collision_data> ship_weapon_check_collision(object *ship_objp, object *weapon_objp, float time_limit = 0.0f, int *next_hit = nullptr)
{
Expand Down Expand Up @@ -262,6 +284,16 @@ static std::tuple<bool, bool, ship_weapon_collision_data> 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,
// 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;
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:
Expand Down Expand Up @@ -391,14 +423,14 @@ static std::tuple<bool, bool, ship_weapon_collision_data> 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;
shield_collision = model_collide(&mc_shield);
mc_shield.flags = shield_flags;
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 = MC_CHECK_MODEL;
shield_collision = model_collide(&mc_shield);
mc_shield.flags = hull_flags;
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
Expand All @@ -409,14 +441,14 @@ static std::tuple<bool, bool, ship_weapon_collision_data> ship_weapon_check_coll
}
} else {
// Normal collision check against a shield mesh
mc_shield.flags = MC_CHECK_SHIELD;
shield_collision = (pm->shield.ntris > 0) ? model_collide(&mc_shield) : 0;
mc_shield.flags = shield_flags;
shield_collision = (pm->shield.ntris > 0) ? model_collide_nearest(&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;

Expand All @@ -426,8 +458,8 @@ static std::tuple<bool, bool, ship_weapon_collision_data> ship_weapon_check_coll
shield_collision = 0;
}
} else {
mc_hull.flags = MC_CHECK_MODEL;
hull_collision = model_collide(&mc_hull);
mc_hull.flags = hull_flags;
hull_collision = model_collide_nearest(&mc_hull);
}

// check if the hit point is beyond the clip plane when warping out.
Expand Down
9 changes: 7 additions & 2 deletions code/object/objcollide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -294,7 +299,7 @@ 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
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;
Expand Down Expand Up @@ -367,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);
Expand Down
1 change: 1 addition & 0 deletions code/weapon/weapon_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
1 change: 1 addition & 0 deletions code/weapon/weapons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ special_flag_def_list_new<Weapon::Info_Flags, weapon_info*, flagset<Weapon::Info
}}, //special case
{"dogfight variant", Weapon::Info_Flags::Dogfight_weapon, true},
{"mine", Weapon::Info_Flags::Mine, true},
{"use radius for ship collisions", Weapon::Info_Flags::Use_radius_for_ship_collisions, true},
};

const size_t num_weapon_info_flags = sizeof(Weapon_Info_Flags) / sizeof(special_flag_def_list_new<Weapon::Info_Flags, weapon_info*, flagset<Weapon::Info_Flags>&>);
Expand Down
Loading