From 2b973abdcf15d3ee8903d73cc86072728ded8412 Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Thu, 19 Jun 2025 14:32:32 +0100 Subject: [PATCH] physics shape and physics debris --- Engine/source/T3D/physics/physicsDebris.cpp | 30 ++++++++-------- Engine/source/T3D/physics/physicsDebris.h | 13 +++---- Engine/source/T3D/physics/physicsShape.cpp | 39 +++++++++++---------- Engine/source/T3D/physics/physicsShape.h | 8 ++--- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/Engine/source/T3D/physics/physicsDebris.cpp b/Engine/source/T3D/physics/physicsDebris.cpp index d6d5eff8e..7f77f5d0c 100644 --- a/Engine/source/T3D/physics/physicsDebris.cpp +++ b/Engine/source/T3D/physics/physicsDebris.cpp @@ -74,7 +74,12 @@ PhysicsDebrisData::PhysicsDebrisData() lifetime = 5.0f; lifetimeVariance = 0.0f; - INIT_ASSET(Shape); + mShapeAsset.registerRefreshNotify(this); +} + +PhysicsDebrisData::~PhysicsDebrisData() +{ + mShapeAsset.unregisterRefreshNotify(); } bool PhysicsDebrisData::onAdd() @@ -96,12 +101,12 @@ bool PhysicsDebrisData::preload( bool server, String &errorStr ) { // Create a dummy shape to force the generation of shaders and materials // during the level load and not during gameplay. - TSShapeInstance *pDummy = new TSShapeInstance( mShape, !server ); + TSShapeInstance *pDummy = new TSShapeInstance( getShape(), !server); delete pDummy; } else { - errorStr = String::ToString("PhysicsDebrisData::load: Couldn't load shape asset \"%s\"", mShapeAssetId); + errorStr = String::ToString("PhysicsDebrisData::load: Couldn't load shape asset \"%s\"", _getShapeAssetId()); return false; } @@ -113,12 +118,9 @@ void PhysicsDebrisData::initPersistFields() docsURL; addGroup( "Shapes" ); - addProtectedField( "shapeFile", TypeShapeFilename, Offset( mShapeName, PhysicsDebrisData ), &_setShapeData, &defaultProtectedGetFn, - "@brief Path to the .DAE or .DTS file to use for this shape.\n\n" - "Compatable with Live-Asset Reloading.", AbstractClassRep::FIELD_HideInInspectors); + INITPERSISTFIELD_SHAPEASSET_REFACTOR(Shape, PhysicsDebrisData, "@brief Shape to use with this debris.\n\n" + "Compatable with Live-Asset Reloading."); - INITPERSISTFIELD_SHAPEASSET(Shape, PhysicsDebrisData, "@brief Shape to use with this debris.\n\n" - "Compatable with Live-Asset Reloading."); endGroup( "Shapes" ); addGroup("Rendering"); @@ -216,7 +218,7 @@ void PhysicsDebrisData::packData(BitStream* stream) stream->write( waterDampingScale ); stream->write( buoyancyDensity ); - PACKDATA_ASSET(Shape); + PACKDATA_ASSET_REFACTOR(Shape); } void PhysicsDebrisData::unpackData(BitStream* stream) @@ -237,7 +239,7 @@ void PhysicsDebrisData::unpackData(BitStream* stream) stream->read( &waterDampingScale ); stream->read( &buoyancyDensity ); - UNPACKDATA_ASSET(Shape); + UNPACKDATA_ASSET_REFACTOR(Shape); } DefineEngineMethod( PhysicsDebrisData, preload, void, (), , @@ -248,7 +250,7 @@ DefineEngineMethod( PhysicsDebrisData, preload, void, (), , { String errorStr; - object->_setShape(object->getShape()); + object->_setShape(object->_getShapeAssetId()); if( !object->preload( false, errorStr ) ) Con::errorf( "PhsysicsDebrisData::preload - error: %s", errorStr.c_str() ); @@ -362,7 +364,7 @@ bool PhysicsDebris::onAdd() } // Setup our bounding box - mObjBox = mDataBlock->mShape->mBounds; + mObjBox = mDataBlock->getShape()->mBounds; resetWorldBox(); // Add it to the client scene. @@ -625,7 +627,7 @@ void PhysicsDebris::_createFragments() if ( !mWorld ) return; - TSShape *shape = mDataBlock->mShape; + TSShape *shape = mDataBlock->getShape(); mShapeInstance = new TSShapeInstance( shape, true ); mShapeInstance->animate(); @@ -699,7 +701,7 @@ void PhysicsDebris::_findNodes( U32 colNode, Vector &nodeIds ) // 1. Visible mesh nodes are siblings of the collision node under a common parent dummy node // 2. Collision node is a child of its visible mesh node - TSShape *shape = mDataBlock->mShape; + TSShape *shape = mDataBlock->getShape(); S32 itr = shape->nodes[colNode].parentIndex; itr = shape->nodes[itr].firstChild; diff --git a/Engine/source/T3D/physics/physicsDebris.h b/Engine/source/T3D/physics/physicsDebris.h index 9b9ae6861..3b5371647 100644 --- a/Engine/source/T3D/physics/physicsDebris.h +++ b/Engine/source/T3D/physics/physicsDebris.h @@ -42,7 +42,7 @@ class TSShape; //************************************************************************** // Debris Data //************************************************************************** -class PhysicsDebrisData : public GameBaseData +class PhysicsDebrisData : public GameBaseData, protected AssetPtrCallback { typedef GameBaseData Parent; @@ -86,10 +86,10 @@ public: /// Is rendererd during shadow passes. bool castShadows; - DECLARE_SHAPEASSET(PhysicsDebrisData, Shape, onShapeChanged); - DECLARE_ASSET_SETGET(PhysicsDebrisData, Shape); + DECLARE_SHAPEASSET_REFACTOR(PhysicsDebrisData, Shape) PhysicsDebrisData(); + virtual ~PhysicsDebrisData(); bool onAdd() override; bool preload( bool server, String &errorStr ) override; @@ -97,13 +97,14 @@ public: void packData( BitStream *stream ) override; void unpackData( BitStream *stream ) override; - void onShapeChanged() + DECLARE_CONOBJECT( PhysicsDebrisData ); + +protected: + void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override { reloadOnLocalClient(); } - DECLARE_CONOBJECT( PhysicsDebrisData ); - }; diff --git a/Engine/source/T3D/physics/physicsShape.cpp b/Engine/source/T3D/physics/physicsShape.cpp index 805531c8b..09e86c530 100644 --- a/Engine/source/T3D/physics/physicsShape.cpp +++ b/Engine/source/T3D/physics/physicsShape.cpp @@ -78,11 +78,12 @@ PhysicsShapeData::PhysicsShapeData() buoyancyDensity( 0.0f ), simType( SimType_ClientServer ) { - INIT_ASSET(Shape); + mShapeAsset.registerRefreshNotify(this); } PhysicsShapeData::~PhysicsShapeData() { + mShapeAsset.unregisterRefreshNotify(); } void PhysicsShapeData::initPersistFields() @@ -90,7 +91,7 @@ void PhysicsShapeData::initPersistFields() docsURL; addGroup("Shapes"); - INITPERSISTFIELD_SHAPEASSET(Shape, PhysicsShapeData, "@brief Shape asset to be used with this physics object.\n\n" + INITPERSISTFIELD_SHAPEASSET_REFACTOR(Shape, PhysicsShapeData, "@brief Shape asset to be used with this physics object.\n\n" "Compatable with Live-Asset Reloading. ") addField( "debris", TYPEID< SimObjectRef >(), Offset( debris, PhysicsShapeData ), @@ -180,7 +181,7 @@ void PhysicsShapeData::packData( BitStream *stream ) { Parent::packData( stream ); - PACKDATA_ASSET(Shape); + PACKDATA_ASSET_REFACTOR(Shape); stream->write( mass ); stream->write( dynamicFriction ); @@ -204,7 +205,7 @@ void PhysicsShapeData::unpackData( BitStream *stream ) { Parent::unpackData(stream); - UNPACKDATA_ASSET(Shape); + UNPACKDATA_ASSET_REFACTOR(Shape); stream->read( &mass ); stream->read( &dynamicFriction ); @@ -249,19 +250,19 @@ void PhysicsShapeData::_onResourceChanged( const Torque::Path &path ) if ( path != Path(mShapeAsset->getShapeFilePath()) ) return; - _setShape(getShape()); + _setShape(_getShapeAssetId()); // Reload the changed shape. PhysicsCollisionRef reloadcolShape; - if ( !mShape ) + if ( !getShape()) { Con::warnf( ConsoleLogEntry::General, "PhysicsShapeData::_onResourceChanged: Could not reload %s.", path.getFileName().c_str() ); return; } // Reload the collision shape. - reloadcolShape = mShape->buildColShape( false, Point3F::One ); + reloadcolShape = getShape()->buildColShape( false, Point3F::One ); if ( bool(reloadcolShape)) colShape = reloadcolShape; @@ -286,31 +287,31 @@ bool PhysicsShapeData::preload( bool server, String &errorBuffer ) if (mShapeAsset.notNull()) { - if (bool(mShape) == false) + if (bool(getShape()) == false) { - errorBuffer = String::ToString("PhysicsShapeData: Couldn't load shape \"%s\"", mShapeAssetId); + errorBuffer = String::ToString("PhysicsShapeData: Couldn't load shape \"%s\"", _getShapeAssetId()); return false; } - if (!server && !mShape->preloadMaterialList(mShape.getPath()) && NetConnection::filesWereDownloaded()) + if (!server && !getShape()->preloadMaterialList(getShape().getPath()) && NetConnection::filesWereDownloaded()) shapeError = true; } // Prepare the shared physics collision shape. - if ( !colShape && mShape) + if ( !colShape && getShape()) { - colShape = mShape->buildColShape( false, Point3F::One ); + colShape = getShape()->buildColShape( false, Point3F::One ); // If we got here and didn't get a collision shape then // we need to fail... can't have a shape without collision. if ( !colShape ) { //no collision so we create a simple box collision shape from the shapes bounds and alert the user - Con::warnf( "PhysicsShapeData::preload - No collision found for shape '%s', auto-creating one", mShapeAssetId); - Point3F halfWidth = mShape->mBounds.getExtents() * 0.5f; + Con::warnf( "PhysicsShapeData::preload - No collision found for shape '%s', auto-creating one", _getShapeAssetId()); + Point3F halfWidth = getShape()->mBounds.getExtents() * 0.5f; colShape = PHYSICSMGR->createCollision(); MatrixF centerXfm(true); - centerXfm.setPosition(mShape->mBounds.getCenter()); + centerXfm.setPosition(getShape()->mBounds.getCenter()); colShape->addBox(halfWidth, centerXfm); return true; } @@ -703,11 +704,11 @@ bool PhysicsShape::_createShape() mAmbientSeq = -1; PhysicsShapeData *db = getDataBlock(); - if ( !db || !db->mShape) + if ( !db || !db->getShape()) return false; // Set the world box. - mObjBox = db->mShape->mBounds; + mObjBox = db->getShape()->mBounds; resetWorldBox(); // If this is the server and its a client only simulation @@ -721,11 +722,11 @@ bool PhysicsShape::_createShape() } // Create the shape instance. - mShapeInst = new TSShapeInstance( db->mShape, isClientObject() ); + mShapeInst = new TSShapeInstance( db->getShape(), isClientObject() ); if ( isClientObject() ) { - mAmbientSeq = db->mShape->findSequence( "ambient" ); + mAmbientSeq = db->getShape()->findSequence( "ambient" ); _initAmbient(); } diff --git a/Engine/source/T3D/physics/physicsShape.h b/Engine/source/T3D/physics/physicsShape.h index 466e39ebf..7419c2442 100644 --- a/Engine/source/T3D/physics/physicsShape.h +++ b/Engine/source/T3D/physics/physicsShape.h @@ -51,7 +51,7 @@ class PhysicsDebrisData; class ExplosionData; -class PhysicsShapeData : public GameBaseData +class PhysicsShapeData : public GameBaseData, protected AssetPtrCallback { typedef GameBaseData Parent; @@ -74,8 +74,7 @@ public: public: - DECLARE_SHAPEASSET(PhysicsShapeData, Shape, onShapeChanged); - DECLARE_ASSET_SETGET(PhysicsShapeData, Shape); + DECLARE_SHAPEASSET_REFACTOR(PhysicsShapeData, Shape) /// The shared unscaled collision shape. PhysicsCollisionRef colShape; @@ -135,7 +134,8 @@ public: SimObjectRef< ExplosionData > explosion; SimObjectRef< PhysicsShapeData > destroyedShape; - void onShapeChanged() +protected: + void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override { reloadOnLocalClient(); }