Skip to content
Open
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
89 changes: 46 additions & 43 deletions code/model/modelcollide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ void mc_check_shield()

// This function recursively checks a submodel and its children
// for a collision with a vector.
void mc_check_subobj( int mn )
void mc_check_subobj(int mn, bool in_lod_tree = false)
{
vec3d tempv;
vec3d hitpt; // used in bounding box check
Expand All @@ -937,6 +937,17 @@ void mc_check_subobj( int mn )

sm = &Mc_pm->submodel[mn];
if (sm->flags[Model::Submodel_flags::No_collisions]) return; // don't do collisions

bsp_info* lod_sm = sm;
if (Mc->lod > 0 && sm->num_details > 0) {
for (int lod = Mc->lod - 1; lod >= 0; lod--) {
if (sm->details[lod] != -1) {
lod_sm = &Mc_pm->submodel[sm->details[lod]];
break;
}
}
}

if (sm->flags[Model::Submodel_flags::Nocollide_this_only]) goto NoHit; // Don't collide for this model, but keep checking others

if (Mc->flags & MC_RESPECT_DETAIL_BOX_SPHERE) {
Expand Down Expand Up @@ -1011,23 +1022,7 @@ void mc_check_subobj( int mn )
} else {
// The ray intersects this bounding box, so we have to check all the
// polygons in this submodel.
if (Mc->lod > 0 && sm->num_details > 0) {
bsp_info* lod_sm = sm;

for (i = Mc->lod - 1; i >= 0; i--) {
if (sm->details[i] != -1) {
lod_sm = &Mc_pm->submodel[sm->details[i]];

// mprintf(("Checking %s collision for %s using %s instead\n", Mc_pm->filename, sm->name,
// lod_sm->name));
break;
}
}

model_collide_bsp(model_get_bsp_collision_tree(lod_sm->collision_tree_index), 0);
} else {
model_collide_bsp(model_get_bsp_collision_tree(sm->collision_tree_index), 0);
}
model_collide_bsp(model_get_bsp_collision_tree(lod_sm->collision_tree_index), 0);
}
}

Expand All @@ -1040,42 +1035,50 @@ void mc_check_subobj( int mn )


// If this subobject doesn't have any children, we're done checking it.
if ( sm->num_children < 1 ) return;
if (sm->num_children < 1 && lod_sm->num_children < 1)
return;

// Save instance (Mc_orient, Mc_base, Mc_point_base)
matrix saved_orient = Mc_orient;
vec3d saved_base = Mc_base;

// Check all of this subobject's children
i = sm->first_child;
while ( i >= 0 ) {
auto csm = &Mc_pm->submodel[i];
matrix instance_orient = vmd_identity_matrix;
vec3d instance_offset = csm->offset;
bool blown_off = false;
bool collision_checked = false;

if ( Mc_pmi ) {
auto csmi = &Mc_pmi->submodel[i];
instance_orient = csmi->canonical_orient;
vm_vec_add2(&instance_offset, &csmi->canonical_offset);
// Check all of this subobject's children; the root also checks its collision LOD's unpartnered children
for (auto parent : {sm, lod_sm}) {
bool lod_tree = in_lod_tree || parent != sm;
for (i = parent->first_child; i >= 0; i = Mc_pm->submodel[i].next_sibling) {
auto csm = &Mc_pm->submodel[i];
// A lower-detail partner is checked through its detail0 piece
if (lod_tree && csm->flags[Model::Submodel_flags::Is_lod])
continue;
matrix instance_orient = vmd_identity_matrix;
vec3d instance_offset = csm->offset;
bool blown_off = false;
bool collision_checked = false;

if ( Mc_pmi ) {
auto csmi = &Mc_pmi->submodel[i];
instance_orient = csmi->canonical_orient;
vm_vec_add2(&instance_offset, &csmi->canonical_offset);

blown_off = csmi->blown_off;
collision_checked = !Mc->collision_checked.empty() && Mc->collision_checked[i];
}
blown_off = csmi->blown_off;
collision_checked = !Mc->collision_checked.empty() && Mc->collision_checked[i];
}

// Don't check it or its children if it is destroyed
// or if it's set to no collision
if ( !blown_off && !collision_checked && !csm->flags[Model::Submodel_flags::No_collisions] ) {
vm_vec_unrotate(&Mc_base, &instance_offset, &saved_orient);
vm_vec_add2(&Mc_base, &saved_base);
// Don't check it or its children if it is destroyed
// or if it's set to no collision
if ( !blown_off && !collision_checked && !csm->flags[Model::Submodel_flags::No_collisions] ) {
vm_vec_unrotate(&Mc_base, &instance_offset, &saved_orient);
vm_vec_add2(&Mc_base, &saved_base);

vm_matrix_x_matrix(&Mc_orient, &saved_orient, &instance_orient);
vm_matrix_x_matrix(&Mc_orient, &saved_orient, &instance_orient);

mc_check_subobj( i );
mc_check_subobj(i, lod_tree);
}
}

i = csm->next_sibling;
if (lod_sm == sm || mn != Mc_pm->detail[0]) {
break;
}
}

}
Expand Down
Loading