| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- /*
- ** Command & Conquer Renegade(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /***********************************************************************************************
- *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
- ***********************************************************************************************
- * *
- * Project Name : WWPhys *
- * *
- * $Archive:: /Commando/Code/wwphys/pscene_collision.cpp $*
- * *
- * Original Author:: Greg Hjelstrom *
- * *
- * $Author:: Greg_h $*
- * *
- * $Modtime:: 2/13/02 2:16p $*
- * *
- * $Revision:: 10 $*
- * *
- *---------------------------------------------------------------------------------------------*
- * Functions: *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #include "pscene.h"
- #include "physcoltest.h"
- #include "physinttest.h"
- #include "staticaabtreecull.h"
- #include "dynamicaabtreecull.h"
- #include "physgridcull.h"
- #include "lightcull.h"
- #include "staticphys.h"
- bool PhysicsSceneClass::Do_Groups_Collide(int group0,int group1)
- {
- int index = group0 | (group1 << COLLISION_FLAG_SHIFT);
- return AllowCollisionFlags[index];
- }
- void PhysicsSceneClass::Enable_Collision_Detection(int group0,int group1)
- {
- assert(group0 >= 0);
- assert(group1 >= 0);
- assert(group0 <= MAX_COLLISION_GROUP);
- assert(group1 <= MAX_COLLISION_GROUP);
- unsigned int index;
- index = group0 | (group1 << COLLISION_FLAG_SHIFT);
- AllowCollisionFlags[index] = 1;
- index = group1 | (group0 << COLLISION_FLAG_SHIFT);
- AllowCollisionFlags[index] = 1;
- }
- void PhysicsSceneClass::Disable_Collision_Detection(int group0,int group1)
- {
- assert(group0 >= 0);
- assert(group1 >= 0);
- assert(group0 <= MAX_COLLISION_GROUP);
- assert(group1 <= MAX_COLLISION_GROUP);
- unsigned int index;
- index = group0 | (group1 << COLLISION_FLAG_SHIFT);
- AllowCollisionFlags[index] = 0;
- index = group1 | (group0 << COLLISION_FLAG_SHIFT);
- AllowCollisionFlags[index] = 0;
- }
- void PhysicsSceneClass::Enable_All_Collision_Detections(int group)
- {
- assert(group >= 0);
- assert(group <= MAX_COLLISION_GROUP);
- for (int i=0; i <= MAX_COLLISION_GROUP; i++) {
- unsigned int index;
- index = group | (i << COLLISION_FLAG_SHIFT);
- AllowCollisionFlags[index] = 1;
- index = i | (group << COLLISION_FLAG_SHIFT);
- AllowCollisionFlags[index] = 1;
- }
- }
- void PhysicsSceneClass::Disable_All_Collision_Detections(int group)
- {
- assert(group >= 0);
- assert(group <= MAX_COLLISION_GROUP);
- for (int i=0; i <= MAX_COLLISION_GROUP; i++) {
- unsigned int index;
- index = group | (i << COLLISION_FLAG_SHIFT);
- AllowCollisionFlags[index] = 0;
- index = i | (group << COLLISION_FLAG_SHIFT);
- AllowCollisionFlags[index] = 0;
- }
- }
- void PhysicsSceneClass::Set_Collision_Region(const AABoxClass & bounds,int colgroup)
- {
- Collect_Collideable_Objects(bounds,colgroup,true,true,&CollisionRegionList);
- }
- void PhysicsSceneClass::Release_Collision_Region(void)
- {
- CollisionRegionList.Reset_List();
- }
- bool PhysicsSceneClass::Cast_Ray(PhysRayCollisionTestClass & raytest,bool use_collision_region)
- {
- /*
- ** Assert that the result structure has been initialized with the
- ** optimistic result that the entire move will be taken. Each call
- ** should whittle down the Fraction variable so that we are left
- ** with the fraction that the closest object allowed. If StartBad
- ** is ever set to true, we can bail out.
- */
- assert(raytest.Result->Fraction == 1.0f);
- assert(raytest.Result->StartBad == false);
- raytest.CollidedPhysObj = NULL;
- /*
- ** Check against physical objects in our vicinity
- */
- bool res = false;
- if (use_collision_region) {
- /*
- ** Use the cached collision region list
- */
- NonRefPhysListIterator it(&CollisionRegionList);
- for ( ; !it.Is_Done(); it.Next()) {
- PhysClass * obj = it.Peek_Obj();
- if ( Do_Groups_Collide(obj->Get_Collision_Group(),raytest.CollisionGroup) &&
- !obj->Is_Ignore_Me() )
- {
- res |= obj->Cast_Ray(raytest);
- }
- }
-
- } else {
- /*
- ** Cull the collision check using the culling systems
- */
- if (raytest.CheckStaticObjs) {
- res |= StaticCullingSystem->Cast_Ray(raytest);
- if (raytest.Result->StartBad) return true;
- }
-
- if (raytest.CheckDynamicObjs) {
- res |= DynamicCullingSystem->Cast_Ray(raytest);
- if (raytest.Result->StartBad) return true;
- }
-
- }
- return res;
- }
- bool PhysicsSceneClass::Cast_AABox(PhysAABoxCollisionTestClass & boxtest,bool use_collision_region)
- {
- /*
- ** Assert that the result structure has been initialized with the
- ** optimistic result that the entire move will be taken. Each call
- ** should whittle down the Fraction variable so that we are left
- ** with the fraction that the closest object allowed. If StartBad
- ** is ever set to true, we can bail out.
- */
- WWASSERT(boxtest.Result->Fraction == 1.0f);
- WWASSERT(boxtest.Result->StartBad == false);
- boxtest.CollidedPhysObj = NULL;
-
- /*
- ** Check against physical objects in our vicinity
- */
- bool res = false;
- if (use_collision_region) {
- /*
- ** Use the cached collision region list
- */
- NonRefPhysListIterator it(&CollisionRegionList);
- for ( ; !it.Is_Done(); it.Next()) {
- PhysClass * obj = it.Peek_Obj();
- if ( Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
- !obj->Is_Ignore_Me() )
- {
- res |= obj->Cast_AABox(boxtest);
- }
- }
-
- } else {
- /*
- ** Cull the collision check using the culling systems
- */
- if (boxtest.CheckStaticObjs) {
- res |= StaticCullingSystem->Cast_AABox(boxtest);
- if (boxtest.Result->StartBad) return true;
- }
-
- if (boxtest.CheckDynamicObjs) {
- res |= DynamicCullingSystem->Cast_AABox(boxtest);
- if (boxtest.Result->StartBad) return true;
- }
- }
- return res;
- }
- bool PhysicsSceneClass::Cast_OBBox(PhysOBBoxCollisionTestClass & boxtest,bool use_collision_region)
- {
- /*
- ** Assert that the result structure has been initialized with the
- ** optimistic result that the entire move will be taken. Each call
- ** should whittle down the Fraction variable so that we are left
- ** with the fraction that the closest object allowed. If StartBad
- ** is ever set to true, we can bail out.
- */
- assert(boxtest.Result->Fraction == 1.0f);
- assert(boxtest.Result->StartBad == false);
- boxtest.CollidedPhysObj = NULL;
-
- /*
- ** Check against physical objects in our vicinity
- */
- bool res = false;
- if (use_collision_region) {
- /*
- ** Use the cached collision region list
- */
- NonRefPhysListIterator it(&CollisionRegionList);
- for ( ; !it.Is_Done(); it.Next()) {
- PhysClass * obj = it.Peek_Obj();
- if ( Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
- !obj->Is_Ignore_Me() )
- {
- res |= obj->Cast_OBBox(boxtest);
- }
- }
-
- } else {
- /*
- ** Cull the collision check using the culling systems
- */
- if (boxtest.CheckStaticObjs) {
- res |= StaticCullingSystem->Cast_OBBox(boxtest);
- if (boxtest.Result->StartBad) return true;
- }
-
- if (boxtest.CheckDynamicObjs) {
- res |= DynamicCullingSystem->Cast_OBBox(boxtest);
- if (boxtest.Result->StartBad) return true;
- }
- }
- return res;
- }
- bool PhysicsSceneClass::Intersection_Test(PhysAABoxIntersectionTestClass & boxtest,bool use_collision_region)
- {
- if (use_collision_region) {
-
- /*
- ** Test for intersection with objects in the cached collision region
- */
- NonRefPhysListIterator it(&CollisionRegionList);
- for ( ; !it.Is_Done(); it.Next()) {
- PhysClass * obj = it.Peek_Obj();
- if ( Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
- !obj->Is_Ignore_Me() )
- {
- if (obj->Intersection_Test(boxtest)) {
- return true;
- }
- }
- }
-
- } else {
- /*
- ** Test for intersection with objects in the static and dynamic culling systems
- */
- if (boxtest.CheckStaticObjs) {
- if (StaticCullingSystem->Intersection_Test(boxtest)) {
- return true;
- }
- }
- if (boxtest.CheckDynamicObjs) {
- if (DynamicCullingSystem->Intersection_Test(boxtest)) {
- return true;
- }
- }
-
- }
- return false;
- }
- bool PhysicsSceneClass::Intersection_Test(PhysOBBoxIntersectionTestClass & boxtest,bool use_collision_region)
- {
- if (use_collision_region) {
-
- /*
- ** Test for intersection with objects in the cached collision region
- */
- NonRefPhysListIterator it(&CollisionRegionList);
- for ( ; !it.Is_Done(); it.Next()) {
- PhysClass * obj = it.Peek_Obj();
- if ( Do_Groups_Collide(obj->Get_Collision_Group(),boxtest.CollisionGroup) &&
- !obj->Is_Ignore_Me() )
- {
- if (obj->Intersection_Test(boxtest)) {
- return true;
- }
- }
- }
-
- } else {
- /*
- ** Test for intersection with objects in the static and dynamic culling systems
- */
- if (boxtest.CheckStaticObjs) {
- if (StaticCullingSystem->Intersection_Test(boxtest)) {
- return true;
- }
- }
- if (boxtest.CheckDynamicObjs) {
- if (DynamicCullingSystem->Intersection_Test(boxtest)) {
- return true;
- }
- }
- }
- return false;
- }
- bool PhysicsSceneClass::Intersection_Test(PhysMeshIntersectionTestClass & meshtest,bool use_collision_region)
- {
- if (use_collision_region) {
-
- /*
- ** Test for intersection with objects in the cached collision region
- */
- NonRefPhysListIterator it(&CollisionRegionList);
- for ( ; !it.Is_Done(); it.Next()) {
- PhysClass * obj = it.Peek_Obj();
- if ( Do_Groups_Collide(obj->Get_Collision_Group(),meshtest.CollisionGroup) &&
- !obj->Is_Ignore_Me() )
- {
- if (obj->Intersection_Test(meshtest)) {
- return true;
- }
- }
- }
-
- } else {
- if (meshtest.CheckStaticObjs) {
- if (StaticCullingSystem->Intersection_Test(meshtest)) {
- return true;
- }
- }
- if (meshtest.CheckDynamicObjs) {
- if (DynamicCullingSystem->Intersection_Test(meshtest)) {
- return true;
- }
- }
- }
- return false;
- }
- bool PhysicsSceneClass::Intersection_Test(const AABoxClass & box,int collision_group,int collision_type,bool use_collision_region)
- {
- NonRefPhysListClass intersect_list;
- PhysAABoxIntersectionTestClass test(box,collision_group,collision_type,&intersect_list);
- return Intersection_Test(test,use_collision_region);
- }
- bool PhysicsSceneClass::Intersection_Test(const OBBoxClass & box,int collision_group,int collision_type,bool use_collision_region)
- {
- NonRefPhysListClass intersect_list;
- PhysOBBoxIntersectionTestClass test(box,collision_group,collision_type,&intersect_list);
- return Intersection_Test(test,use_collision_region);
- }
- void PhysicsSceneClass::Add_Collected_Objects_To_List
- (
- bool static_objs,
- bool dynamic_objs,
- NonRefPhysListClass * list
- )
- {
- // link the static objects
- if (static_objs) {
- StaticPhysClass * obj;
- for ( obj = (StaticPhysClass *)StaticCullingSystem->Get_First_Collected_Object();
- obj != NULL;
- obj = (StaticPhysClass *)StaticCullingSystem->Get_Next_Collected_Object(obj) )
- {
- list->Add(obj);
- }
- }
- // link the dynamic objects
- if (dynamic_objs) {
- PhysClass * obj;
- for ( obj = (PhysClass *)DynamicCullingSystem->Get_First_Collected_Object();
- obj != NULL;
- obj = (PhysClass *)DynamicCullingSystem->Get_Next_Collected_Object(obj) )
- {
- list->Add(obj);
- }
- }
- }
- void PhysicsSceneClass::Add_Collected_Collideable_Objects_To_List
- (
- int colgroup,
- bool static_objs,
- bool dynamic_objs,
- NonRefPhysListClass * list
- )
- {
- // link the static objects
- if (static_objs) {
- StaticPhysClass * obj;
- for ( obj = (StaticPhysClass *)StaticCullingSystem->Get_First_Collected_Object();
- obj != NULL;
- obj = (StaticPhysClass *)StaticCullingSystem->Get_Next_Collected_Object(obj) )
- {
- if ( Do_Groups_Collide(obj->Get_Collision_Group(),colgroup) &&
- !obj->Is_Ignore_Me() )
- {
- list->Add(obj);
- }
- }
- }
- // link the dynamic objects
- if (dynamic_objs) {
- PhysClass * obj;
- for ( obj = (PhysClass *)DynamicCullingSystem->Get_First_Collected_Object();
- obj != NULL;
- obj = (PhysClass *)DynamicCullingSystem->Get_Next_Collected_Object(obj) )
- {
- if ( Do_Groups_Collide(obj->Get_Collision_Group(),colgroup) &&
- !obj->Is_Ignore_Me() )
- {
- list->Add(obj);
- }
- }
- }
- }
- void PhysicsSceneClass::Collect_Objects
- (
- const Vector3 & point,
- bool static_objs,
- bool dynamic_objs,
- NonRefPhysListClass * list
- )
- {
- WWASSERT(list != NULL);
- if (static_objs) {
- StaticCullingSystem->Reset_Collection();
- StaticCullingSystem->Collect_Objects(point);
- }
- if (dynamic_objs) {
- DynamicCullingSystem->Reset_Collection();
- DynamicCullingSystem->Collect_Objects(point);
- }
- Add_Collected_Objects_To_List(static_objs,dynamic_objs,list);
- }
- void PhysicsSceneClass::Collect_Objects
- (
- const AABoxClass & box,
- bool static_objs,
- bool dynamic_objs,
- NonRefPhysListClass * list
- )
- {
- WWASSERT(list != NULL);
- if (static_objs) {
- StaticCullingSystem->Reset_Collection();
- StaticCullingSystem->Collect_Objects(box);
- }
- if (dynamic_objs) {
- DynamicCullingSystem->Reset_Collection();
- DynamicCullingSystem->Collect_Objects(box);
- }
- Add_Collected_Objects_To_List(static_objs,dynamic_objs,list);
- }
- void PhysicsSceneClass::Collect_Objects
- (
- const OBBoxClass & box,
- bool static_objs,
- bool dynamic_objs,
- NonRefPhysListClass * list
- )
- {
- WWASSERT(list != NULL);
- if (static_objs) {
- StaticCullingSystem->Reset_Collection();
- StaticCullingSystem->Collect_Objects(box);
- }
- if (dynamic_objs) {
- DynamicCullingSystem->Reset_Collection();
- DynamicCullingSystem->Collect_Objects(box);
- }
- Add_Collected_Objects_To_List(static_objs,dynamic_objs,list);
- }
- void PhysicsSceneClass::Collect_Objects
- (
- const FrustumClass & frustum,
- bool static_objs,
- bool dynamic_objs,
- NonRefPhysListClass * list
- )
- {
- WWASSERT(list != NULL);
- if (static_objs) {
- StaticCullingSystem->Reset_Collection();
- StaticCullingSystem->Collect_Objects(frustum);
- }
- if (dynamic_objs) {
- DynamicCullingSystem->Reset_Collection();
- DynamicCullingSystem->Collect_Objects(frustum);
- }
- Add_Collected_Objects_To_List(static_objs,dynamic_objs,list);
- }
- void PhysicsSceneClass::Collect_Collideable_Objects
- (
- const AABoxClass & box,
- int colgroup,
- bool static_objs,
- bool dynamic_objs,
- NonRefPhysListClass * list
- )
- {
- WWASSERT(list != NULL);
- if (static_objs) {
- StaticCullingSystem->Reset_Collection();
- StaticCullingSystem->Collect_Objects(box);
- }
- if (dynamic_objs) {
- DynamicCullingSystem->Reset_Collection();
- DynamicCullingSystem->Collect_Objects(box);
- }
- Add_Collected_Collideable_Objects_To_List(colgroup,static_objs,dynamic_objs,list);
- }
- void PhysicsSceneClass::Collect_Collideable_Objects
- (
- const OBBoxClass & box,
- int colgroup,
- bool static_objs,
- bool dynamic_objs,
- NonRefPhysListClass * list
- )
- {
- WWASSERT(list != NULL);
- if (static_objs) {
- StaticCullingSystem->Reset_Collection();
- StaticCullingSystem->Collect_Objects(box);
- }
- if (dynamic_objs) {
- DynamicCullingSystem->Reset_Collection();
- DynamicCullingSystem->Collect_Objects(box);
- }
- Add_Collected_Collideable_Objects_To_List(colgroup,static_objs,dynamic_objs,list);
- }
- void PhysicsSceneClass::Add_Collected_Lights_To_List
- (
- bool static_lights,
- bool dynamic_lights,
- NonRefPhysListClass * list
- )
- {
- // link the static lights
- if (static_lights) {
- LightPhysClass * obj;
- for ( obj = StaticLightingSystem->Get_First_Collected_Object();
- obj != NULL;
- obj = StaticLightingSystem->Get_Next_Collected_Object(obj) )
- {
- list->Add(obj);
- }
- }
- // link the dynamic lights
- // TODO!!
- }
- void PhysicsSceneClass::Collect_Lights
- (
- const Vector3 & point,
- bool static_lights,
- bool dynamic_lights,
- NonRefPhysListClass * list
- )
- {
- WWASSERT(list != NULL);
- if (static_lights) {
- StaticLightingSystem->Reset_Collection();
- StaticLightingSystem->Collect_Objects(point);
- }
- // TODO: Dynamic lights!!
- Add_Collected_Lights_To_List(static_lights,dynamic_lights,list);
- }
- void PhysicsSceneClass::Collect_Lights
- (
- const AABoxClass & bounds,
- bool static_lights,
- bool dynamic_lights,
- NonRefPhysListClass * list
- )
- {
- WWASSERT(list != NULL);
- if (static_lights) {
- StaticLightingSystem->Reset_Collection();
- StaticLightingSystem->Collect_Objects(bounds);
- }
- // TODO: Dynamic lights!!
- Add_Collected_Lights_To_List(static_lights,dynamic_lights,list);
- }
|